2015-02-09 68 views
14

我在我的项目中使用TTTAttributedLabel。我已设法更改默认颜色并为我通过修改链接属性创建的任何链接加下划线。TTTAttributedLabel的链接龙头颜色

NSArray *pKeys = [[NSArray alloc] initWithObjects:(id)kCTForegroundColorAttributeName, 
         (id)kCTUnderlineStyleAttributeName 
        , nil]; 

NSArray *pObjects = [[NSArray alloc] initWithObjects:pAlertColor,[NSNumber numberWithInt: 
                      kCTUnderlineStyleNone], nil]; 

NSDictionary *pLinkAttributes = [[NSDictionary alloc] initWithObjects:pObjects 
                    forKeys:pKeys]; 

self.alertMessage.linkAttributes = pLinkAttributes; 
self.alertMessage.activeLinkAttributes = pLinkAttributes; 

但是,我已经注意到,当我点击链接时,它会随着任何其他链接点击时瞬间变成红色。我需要改变这种颜色。任何线索可以做到这一点?

回答

14

你会喜欢看TTTAttributedLabel documentation,特别是在activeLinkAttributes

activeLinkAttributes

@属性(非原子,强)的NSDictionary * activeLinkAttributes 讨论

包含NSAttributedString属性的字典为 ,当它们处于活动状态时应用于链接。如果为零或为空,则不会对活动链接进行样式设置。默认活动链接 样式为红色并加下划线。

宣布

TTTAttributedLabel.h

+0

由于球员..我为activeLinkAttributes设置相同的链接属性,它就起作用了。 (基本上我不希望链接颜色改变)。我曾看到activeLinkAttributes,但不知道这会帮助我。 – StudentX 2015-02-09 14:34:02

1

您可以使用属性 “activeLinkAttributes”

NSMutableDictionary* attributes = [NSMutableDictionary dictionaryWithDictionary:self.attributedLabel.activeLinkAttributes]; 
[attributes setObject:(__bridge id)[UIColor blueColor].CGColor forKey:(NSString*)kCTForegroundColorAttributeName]; 
self.attributedLabel.activeLinkAttributes = attributes; 
5

你应该做这样的事情

NSMutableDictionary *mutableActiveLinkAttributes = [NSMutableDictionary dictionary]; 
    [mutableActiveLinkAttributes setObject:[NSNumber numberWithBool:NO] forKey:(NSString *)kCTUnderlineStyleAttributeName]; 
    [mutableActiveLinkAttributes setObject:[UIColor greenColor] forKey:(NSString *)kCTForegroundColorAttributeName]; 
    label.activeLinkAttributes = [NSDictionary dictionaryWithDictionary:mutableActiveLinkAttributes]; 
18

斯威夫特2解决方法:

enter image description here

具体而言,需要设置activeLinkAttributes,见下面的例子:

private func subscriptionNoticeWithDelegate(delegate:TTTAttributedLabelDelegate) -> TTTAttributedLabel { 
    let subscriptionNotice:String = "To turn on all notifications, subscribe to our monthly " + 
    "service ($0.99/month). If you have already subscribed, please restore your purchase." 

    let paragraphStyle = NSMutableParagraphStyle() 
    paragraphStyle.lineHeightMultiple = 1.2 

    let subscriptionNoticeAttributedString = NSAttributedString(string:subscriptionNotice, attributes: [ 
    NSFontAttributeName: UIFont(name:"HelveticaNeue-Light", size:15)!, 
    NSParagraphStyleAttributeName: paragraphStyle, 
    NSForegroundColorAttributeName: UIColor.grayColor().CGColor, 
    ]) 
    let subscriptionNoticeLinkAttributes = [ 
    NSForegroundColorAttributeName: UIColor.grayColor(), 
    NSUnderlineStyleAttributeName: NSNumber(bool:true), 
    ] 
    let subscriptionNoticeActiveLinkAttributes = [ 
    NSForegroundColorAttributeName: UIColor.grayColor().colorWithAlphaComponent(0.80), 
    NSUnderlineStyleAttributeName: NSNumber(bool:true), 
    ] 

    let subscriptionNoticeLabel:TTTAttributedLabel = TTTAttributedLabel(frame:CGRectZero) 
    subscriptionNoticeLabel.delegate = delegate 
    subscriptionNoticeLabel.numberOfLines = 0 
    subscriptionNoticeLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping 
    subscriptionNoticeLabel.textInsets = UIEdgeInsets(top:10, left:15, bottom:0, right:15) 
    subscriptionNoticeLabel.setText(subscriptionNoticeAttributedString) 
    subscriptionNoticeLabel.linkAttributes = subscriptionNoticeLinkAttributes 
    subscriptionNoticeLabel.activeLinkAttributes = subscriptionNoticeActiveLinkAttributes 

    let subscribeLinkRange = (subscriptionNotice as NSString).rangeOfString("subscribe") 
    let subscribeURL = NSURL(string:kSubscriptionNoticeSubscribeURL)! 
    subscriptionNoticeLabel.addLinkToURL(subscribeURL, withRange:subscribeLinkRange) 

    let restoreLinkRange = (subscriptionNotice as NSString).rangeOfString("restore") 
    let restoreURL = NSURL(string:kSubscriptionNoticeRestoreURL)! 
    subscriptionNoticeLabel.addLinkToURL(restoreURL, withRange:restoreLinkRange) 

    return subscriptionNoticeLabel 
} 
+1

感谢Swift解决方案! – StudentX 2015-08-19 14:14:39

2

的SWIFT 4:

let activeLinkAttributes = NSMutableDictionary(dictionary: attributedLabel.activeLinkAttributes) 
activeLinkAttributes[NSAttributedStringKey.foregroundColor] = UIColor.blue 
attributedLabel.activeLinkAttributes = activeLinkAttributes as NSDictionary as! [AnyHashable: Any] 

的SWIFT 3:

let activeLinkAttributes = NSMutableDictionary(dictionary: attributedLabel.activeLinkAttributes) 
activeLinkAttributes[NSForegroundColorAttributeName] = UIColor.blue 
attributedLabel.activeLinkAttributes = activeLinkAttributes as NSDictionary as! [AnyHashable: Any]