TTTAttributedLabel 的链接点击颜色

Posted

技术标签:

【中文标题】TTTAttributedLabel 的链接点击颜色【英文标题】:Link tap color for TTTAttributedLabel 【发布时间】:2015-02-09 14:16:56 【问题描述】:

我在我的项目中使用了 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;

但是,我注意到当我点击链接时,它会像点击任何其他链接一样瞬间变成红色。我需要改变这个颜色。有什么线索可以说明如何做到这一点?

【问题讨论】:

【参考方案1】:

Swift 2 解决方案:

具体需要设置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

【讨论】:

【参考方案2】:

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

活动链接属性

@property (nonatomic, strong) NSDictionary *activeLinkAttributes 讨论

包含 NSAttributedString 属性的字典 当它们处于活动状态时应用于链接。如果 nil 或为空 NSDictionary,活动链接不会被样式化。默认活动链接 样式为红色并带有下划线。

声明于

TTTAttributedLabel.h

【讨论】:

谢谢大家.. 我也为 activeLinkAttributes 设置了相同的链接属性并且它有效。 (基本上我不希望链接颜色在点击时改变)。我看过 activeLinkAttributes 但不知道这会帮助我。【参考方案3】:

你应该这样做

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

【讨论】:

【参考方案4】:

对于 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]

【讨论】:

【参考方案5】:

在 Objective-C 中设置 TTTAttributedLabel 的完整代码

#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");
    

注意:安装 Pod 文件:pod 'TTTAttributedLabel'

【讨论】:

【参考方案6】:

您可以使用属性“activeLinkAttributes”

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

【讨论】:

以上是关于TTTAttributedLabel 的链接点击颜色的主要内容,如果未能解决你的问题,请参考以下文章

TTTAttributedLabel 可点击链接在 Swift 3 中不起作用

如何在 TTTAttributedLabel 中将 HTML 锚点设为可点击链接?

TTTAttributedLabel 和链接弹出框

TTTAttributedLabel : 检测链接问题。

TTTAttributedLabel 可以检测链接,但不能正确按下

TTTAttributedLabel 链接检测无法使用 Storyboard