我在我的项目中使用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;
但是,我注意到当我点击链接时,它会随着任何其他链接在点击时暂时变为红色。我需要改变这种颜色。有关如何做到这一点的任何线索?
答案 0 :(得分:20)
答案 1 :(得分:17)
您希望查看TTTAttributedLabel documentation,特别是在activeLinkAttributes
activeLinkAttributes
@property(非原子,强)NSDictionary * activeLinkAttributes 讨论
包含NSAttributedString属性的字典 应用于处于活动状态的链接。如果为零或空 NSDictionary,活动链接不会被设置样式。默认的活动链接 风格是红色和下划线。
声明
TTTAttributedLabel.h
答案 2 :(得分:5)
你应该做这样的事情
NSMutableDictionary *mutableActiveLinkAttributes = [NSMutableDictionary dictionary];
[mutableActiveLinkAttributes setObject:[NSNumber numberWithBool:NO] forKey:(NSString *)kCTUnderlineStyleAttributeName];
[mutableActiveLinkAttributes setObject:[UIColor greenColor] forKey:(NSString *)kCTForegroundColorAttributeName];
label.activeLinkAttributes = [NSDictionary dictionaryWithDictionary:mutableActiveLinkAttributes];
答案 3 :(得分: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]
答案 4 :(得分:1)
您可以使用属性“activeLinkAttributes”
NSMutableDictionary* attributes = [NSMutableDictionary dictionaryWithDictionary:self.attributedLabel.activeLinkAttributes];
[attributes setObject:(__bridge id)[UIColor blueColor].CGColor forKey:(NSString*)kCTForegroundColorAttributeName];
self.attributedLabel.activeLinkAttributes = attributes;
答案 5 :(得分:1)
完整代码以在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'