在TTTAttributedLabel中指定多个/条件链接颜色 [英] Specify multiple/conditional link colors in TTTAttributedLabel

查看:244
本文介绍了在TTTAttributedLabel中指定多个/条件链接颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在TTTAttributedLabel中添加了一个链接检测器,该链接检测器标识@mentions和#hashtags并在我的TTTAttributedLabel实例中的该位置处创建一个链接:

I've added a link detector to TTTAttributedLabel that identifies @mentions and #hashtags and creates a link at that position in my TTTAttributedLabel instance:

- (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];
    }
}

我还发现我可以这样做以轻松更改链接的颜色和属性:

I've also discovered that I can do this to easily change the link colors and attributes:

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链接,主题标签和提及.有没有办法使用正则表达式或范围创建不同的链接颜色?假设我要让@mentions为灰色,@ hashtags为红色,Web链接为蓝色?

But this changes the colors for every link attribute - including web links, hashtags, and mentions. Is there a way to create different link colors using a regular expression or range? Say if I wanted @mentions to be gray, @hashtags to be red, and web links to be blue?

推荐答案

我正在研究一个类似的问题,遇到了您的问题.我不确切地知道如何注入某种不同的表达式来匹配我标签中的其他事物,因此您的第一部分代码将其清除了.

I was just working on a similar problem and came across your question. I didn't know precisely how to inject some sort of different expressions to match other sorts of things in my label, so your first bit of code cleared that up.

不过,对于您的问题-我所做的是将TTTAttributedLabel方法更改为添加NSTextCheckingResult的方法.因此,如果我在该方法中对您的for循环进行了一些更改并使用[self.label addLinkWithTextCheckingResult: attributes: ]并按照您的建议设置了属性,那么现在该循环如下所示:

For your question though — what I did was change the TTTAttributedLabel method to one that adds the NSTextCheckingResult. So, if I make a few changes to your for loop in that method and use [self.label addLinkWithTextCheckingResult: attributes: ] and set the attributes as you suggest, now that loop looks like this:

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];
}

在我的情况下,它将以烤橙显示#和@.

In my case, this will show # and @ in a toasted orange.

然后在我的TTTAttributedLabelDelegate中有- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithTextCheckingResult:(NSTextCheckingResult *)result TTTAttributedLabelDelegate方法.当一个人点击#或@文本时,就会用NSTextCheckingResult调用该函数.

And then I have the - (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithTextCheckingResult:(NSTextCheckingResult *)result TTTAttributedLabelDelegate method in my TTTAttributedLabelDelegate. That gets called with a NSTextCheckingResult when one taps on the # or @ text.

这是您要找的吗?

这篇关于在TTTAttributedLabel中指定多个/条件链接颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆