Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios TTTAttributedLabel的链接点击颜色_Ios_Objective C_Tttattributedlabel - Fatal编程技术网

Ios TTTAttributedLabel的链接点击颜色

Ios TTTAttributedLabel的链接点击颜色,ios,objective-c,tttattributedlabel,Ios,Objective C,Tttattributedlabel,我正在项目中使用TTtatAttributedLabel。通过修改链接属性,我成功地更改了我创建的任何链接的默认颜色和下划线 NSArray *pKeys = [[NSArray alloc] initWithObjects:(id)kCTForegroundColorAttributeName, (id)kCTUnderlineStyleAttributeName , nil]; NSArray *pObj

我正在项目中使用TTtatAttributedLabel。通过修改链接属性,我成功地更改了我创建的任何链接的默认颜色和下划线

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;
然而,我注意到,当我点击链接时,它会像点击其他链接一样瞬间变为红色。我需要换一下颜色。有没有关于如何实现的线索?

您可以使用属性“activeLinkAttributes”

您将要查看,特别是activeLinkAttributes

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

@属性(非原子,强)NSDictionary*activeLinkAttributes 讨论

包含要删除的NSAttributedString属性的字典 应用于处于活动状态的链接。如果为零或为空 NSDictionary,将不会设置活动链接的样式。默认的活动链接 风格是红色和下划线

声明于

TTTAttributedLabel.h


你应该这样做

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

Swift 2解决方案:

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

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

对于Swift 4:

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

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

在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'tttatAttributedLabel'

谢谢各位。。我也为activeLinkAttributes设置了相同的链接属性,而且效果很好。(基本上我不希望点击时链接颜色发生变化)。我看过activeLinkAttributes,但不知道这会对我有所帮助。