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?

  • What to use if not “IPHONE UDID”?
  • Which way to store data(image)? NSData, String or Transformable
  • Cannot print the FBSDKAccessToken.currentAccessToken().tokenString in my iOS9 app
  • How to load .dll file which returns assembly in Objective-C?
  • Incompatible pointer types Xcode
  • Method swizzling in Swift
  • 5 Solutions Collect From Internet About “Link tap color for TTTAttributedLabel”

    You will like to look at TTTAttributedLabel documentation, specifically at activeLinkAttributes

    activeLinkAttributes

    @property (nonatomic, strong) NSDictionary *activeLinkAttributes
    Discussion

    A dictionary containing the NSAttributedString attributes to be
    applied to links when they are in the active state. If nil or an empty
    NSDictionary, active links will not be styled. The default active link
    style is red and underlined.

    Declared In

    TTTAttributedLabel.h

    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
    }
    

    You should do something like this

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

    You can use attribute “activeLinkAttributes”

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

    For Swift 3:

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