Link tap color for TTTAttributedLabel
Asked Answered
B

7

18

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?

Bugs answered 9/2, 2015 at 14:16 Comment(0)
P
17

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

Phenolic answered 9/2, 2015 at 14:23 Comment(1)
Thanks guys.. I set the same link attributes for activeLinkAttributes as well and it worked. (Basically I did not want the link color to change on tap). I had seen the activeLinkAttributes but did not know that would help me out.Bugs
K
20

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
}
Kwiatkowski answered 17/8, 2015 at 4:18 Comment(0)
P
17

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

Phenolic answered 9/2, 2015 at 14:23 Comment(1)
Thanks guys.. I set the same link attributes for activeLinkAttributes as well and it worked. (Basically I did not want the link color to change on tap). I had seen the activeLinkAttributes but did not know that would help me out.Bugs
E
6

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];
Eggett answered 9/2, 2015 at 14:43 Comment(0)
D
4

For Swift 4:

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

For Swift 3:

let activeLinkAttributes = NSMutableDictionary(dictionary: attributedLabel.activeLinkAttributes)
activeLinkAttributes[NSForegroundColorAttributeName] = UIColor.blue
attributedLabel.activeLinkAttributes = activeLinkAttributes as NSDictionary as! [AnyHashable: Any]
Deter answered 8/3, 2017 at 22:29 Comment(0)
W
3

Full code to set TTTAttributedLabel in Objective-C

#import "TTTAttributedLabel.h"

@property (weak, nonatomic) IBOutlet TTTAttributedLabel *attributedLable;

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setup];
}

- (void)setup {
    _attributedLable.numberOfLines = 0;

    NSString *strTC = @"Terms and Condition";
    NSString *strPP = @"Privacy Policy";

    NSString *string = [NSString stringWithFormat:@"By click continue I agree to %@ and %@.",strTC,strPP];

    NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle alloc];
    paragraphStyle.lineHeightMultiple = 1.2;

    NSAttributedString *fullAttributedString = [[NSAttributedString alloc] initWithString:string attributes:@{
                                                                                                              NSFontAttributeName : [UIFont fontWithName:IZFontNameLatoRegular size:15.0],
                                                                                                              NSParagraphStyleAttributeName : paragraphStyle
                                                                                                              }];
    [_attributedLable setTextAlignment:NSTextAlignmentCenter];
    [_attributedLable setAttributedText:fullAttributedString];

    NSRange rangeTC = [string rangeOfString:strTC];
    NSRange rangePP = [string rangeOfString:strPP];

    NSDictionary *ppActiveLinkAttributes = @{NSForegroundColorAttributeName : [UIColor blueColor], NSUnderlineStyleAttributeName: @(NSUnderlineStyleNone)};
    NSDictionary *ppLinkAttributes = @{NSForegroundColorAttributeName : [UIColor blueColor], NSUnderlineStyleAttributeName: @(NSUnderlineStyleNone)};

    _attributedLable.activeLinkAttributes = ppActiveLinkAttributes;
    _attributedLable.linkAttributes = ppLinkAttributes;

    NSURL *urlTC = [NSURL URLWithString:@"action://TC"];
    NSURL *urlPP = [NSURL URLWithString:@"action://PP"];

    [_attributedLable addLinkToURL:urlTC withRange:rangeTC];
    [_attributedLable addLinkToURL:urlPP withRange:rangePP];

    _attributedLable.textColor = [UIColor blackColor];
    _attributedLable.delegate = self;
}

//Delegate Method
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url {
    if ([url.absoluteString isEqualToString:@"action://TC"]) {
        NSLog(@"terms and conditions click");
    }
    else if ([url.absoluteString isEqualToString:@"action://PP"]){
        NSLog(@"privacy policy click");
    }
}

Note : Install Pod file : pod 'TTTAttributedLabel'

Weathertight answered 23/7, 2018 at 11:49 Comment(1)
This is helpful to see it in Objective-C. One thing to change in this example: @bug Setting 'attributedText' directly is not recommended, as it may cause a crash when attempting to access any links previously set. Instead, call 'setText:', passing an 'NSAttributedString'.Whaler
L
1

You can use an attribute "activeLinkAttributes"

NSMutableDictionary* attributes = [NSMutableDictionary dictionaryWithDictionary:self.attributedLabel.activeLinkAttributes];
[attributes setObject:(__bridge id)[UIColor blueColor].CGColor forKey:(NSString*)kCTForegroundColorAttributeName];
self.attributedLabel.activeLinkAttributes = attributes;
Larkin answered 9/2, 2015 at 14:22 Comment(0)
W
0
For reference, Not changing link color when tapping.

Just disable active link color by code below.

self.tttAttributedLabel.inactiveLinkAttributes = nil;
Wallin answered 14/6, 2022 at 8:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.