链接TTTAttributedLabel的tap颜色

我在我的项目中使用TTTAttributedLabel。 我已经设法改变默认的颜色,并通过修改链接属性来创build任何链接。

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; 

不过,我注意到,当我点击链接时,它会随着任何其他链接点击时瞬间变红。 我需要改变这种颜色。 任何线索怎么可能做?

您将喜欢查看TTTAttributedLabel文档 ,特别是在activeLinkAttributes

activeLinkAttributes

@property(nonatomic,strong)NSDictionary * activeLinkAttributes Discussion

包含NSAttributedString属性的字典,当它们处于活动状态时,将应用于链接。 如果没有或一个空的NSDictionary,活动链接将不会被设置。 默认的活动链接样式是红色并加下划线。

宣布在

TTTAttributedLabel.h

Swift 2解决scheme:

在这里输入图像说明

具体来说,需要设置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 } 

你应该这样做

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

您可以使用属性“activeLinkAttributes”

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

对于Swift 3:

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