Ios 在TTTAttributedLabel中指定多个/条件链接颜色
我已经在TTtatAttributedLabel中添加了一个链接检测器,用于识别@提及和#hashtags,并在我的TTtatAttributedLabel实例中的该位置创建一个链接:Ios 在TTTAttributedLabel中指定多个/条件链接颜色,ios,objective-c,tttattributedlabel,Ios,Objective C,Tttattributedlabel,我已经在TTtatAttributedLabel中添加了一个链接检测器,用于识别@提及和#hashtags,并在我的TTtatAttributedLabel实例中的该位置创建一个链接: - (void)highlightMentionsInString:(NSString *)text withColor:(UIColor *)color isBold:(BOOL)bold isUnderlined:(BOOL)underlined { NSRegularExpression *ment
- (void)highlightMentionsInString:(NSString *)text withColor:(UIColor *)color isBold:(BOOL)bold isUnderlined:(BOOL)underlined
{
NSRegularExpression *mentionExpression = [NSRegularExpression regularExpressionWithPattern:@"(?:^|\\s)(@\\w+)" options:NO error:nil];
NSArray *matches = [mentionExpression matchesInString:text
options:0
range:NSMakeRange(0, [text length])];
for (NSTextCheckingResult *match in matches) {
NSRange matchRange = [match rangeAtIndex:1];
NSString *mentionString = [text substringWithRange:matchRange];
NSRange linkRange = [text rangeOfString:mentionString];
NSString* user = [mentionString substringFromIndex:1];
NSString* linkURLString = [NSString stringWithFormat:@"user:%@", user];
[self.attributedLabel addLinkToURL:[NSURL URLWithString:linkURLString] withRange:linkRange];
}
}
我还发现,我可以这样做来轻松更改链接颜色和属性:
NSArray *keys = [[NSArray alloc] initWithObjects:(id)kCTForegroundColorAttributeName,(id)kCTUnderlineStyleAttributeName
, nil];
NSArray *objects = [[NSArray alloc] initWithObjects:color,[NSNumber numberWithInt:kCTUnderlineStyleNone], nil];
NSDictionary *linkAttributes = [[NSDictionary alloc] initWithObjects:objects forKeys:keys];
self.attributedLabel.linkAttributes = linkAttributes;
但这会改变每个链接属性的颜色,包括web链接、哈希标记和提及。有没有办法使用正则表达式或范围创建不同的链接颜色?比如说,如果我想让@提及的内容变成灰色,@标签变成红色,网络链接变成蓝色?我刚刚在处理一个类似的问题,遇到了你的问题。我不知道如何精确地注入一些不同的表达式来匹配标签中的其他类型的内容,所以您的第一段代码澄清了这一点 不过,对于您的问题,我所做的是将TTtatAttributedLabel方法更改为添加NSTextCheckingResult的方法。因此,如果我在该方法中对您的
for
循环进行一些更改,并使用[self.label addLinkWithTextCheckingResult:attributes://code>并按照您的建议设置属性,现在该循环看起来如下所示:
for (NSTextCheckingResult *match in matches) {
NSRange matchRange = [match rangeAtIndex:1];
NSString *mentionString = [text substringWithRange:matchRange];
NSString* user = [mentionString substringFromIndex:1];
NSString* linkURLString = [NSString stringWithFormat:@"user:%@", user];
NSArray *keys = [[NSArray alloc] initWithObjects:(id)kCTForegroundColorAttributeName, (id)kCTUnderlineStyleAttributeName, nil];
NSArray *objects = [[NSArray alloc] initWithObjects:color,[NSNumber numberWithInt:kCTUnderlineStyleNone], nil];
NSDictionary *linkAttributes = [[NSDictionary alloc] initWithObjects:objects forKeys:keys];
[self.label addLinkWithTextCheckingResult:match attributes:linkAttributes];
}
在我的例子中,这将在烤橘子中显示#和@
然后我有-(void)attributedLabel:(tttatAttributedLabel*)标签
didSelectLinkWithTextCheckingResult:(NSTextCheckingResult*)结果
TTStatAttributedLabelDelegate中的TTStatAttributedLabelDelegate方法。当点击#或@text时,将使用NSTextCheckingResult调用该函数
这就是您想要的吗?由于有关链接高亮显示状态的问题尚未得到回答,下面是一个简单的解决方案:
var attrs = [NSFontAttributeName : UIFont.systemFontOfSize(14.0), NSForegroundColorAttributeName: UIColor.blackColor()]
label.activeLinkAttributes = attrs
更简单的解决方案是:self.AttributeLabel.linkAttributes=@{NSForegroundColorAttributeName:[UIColor darkGrayColor],NSUnderlineStyleAttributeName:[NSNumber numberWithInt:NSUnderlineStyleSingle]};NSRange r1=[labelText rangeOfString:@“隐私策略”];[self.attributedLabel addLinkToURL:[NSURL URLWithString:@”action://PrivacyPolicy“]范围:r1];