Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Link tap color for TTTAttributedLabel

I am using a TTTAttributedLabel in my project. I have managed to change the default color and underlining for any link that I create by modifying the link attributes.

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;

However, I have noticed that when I tap on the link, it turns red momentarily as any other link does when tapped. I need to change this color. Any clues to how that might be done?

like image 942
StudentX Avatar asked Feb 09 '15 14:02

StudentX


1 Answers

Swift 2 Solution:

enter image description here

Specifically, need to set activeLinkAttributes, see below example:

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
}
like image 81
Zorayr Avatar answered Sep 23 '22 14:09

Zorayr