【问题标题】:Specify multiple/conditional link colors in TTTAttributedLabel在 TTTAttributedLabel 中指定多个/条件链接颜色
【发布时间】:2014-03-10 22:41:14
【问题描述】:

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

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

但这会改变每个链接属性的颜色 - 包括网络链接、主题标签和提及。有没有办法使用正则表达式或范围创建不同的链接颜色?假设我希望@mentions 为灰色,@hashtags 为红色,网络链接为蓝色?

【问题讨论】:

  • 更简单的解决方案是:self.attributedLabel.linkAttributes = @{ NSForegroundColorAttributeName: [UIColor darkGrayColor], NSUnderlineStyleAttributeName:[NSNumber numberWithInt:NSUnderlineStyleSingle]}; NSRange r1 = [labelText rangeOfString:@"隐私政策"]; [self.attributedLabel addLinkToURL:[NSURL URLWithString:@"action://PrivacyPolicy"] withRange:r1];

标签: ios objective-c tttattributedlabel


【解决方案1】:

我刚刚研究了一个类似的问题,遇到了你的问题。我不确切知道如何注入某种不同的表达式来匹配我标签中的其他类型的东西,所以你的第一段代码清除了这一点。

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

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

在我的例子中,这将显示 # 和 @ 在烤橙色。

然后我的 TTTAttributedLabelDelegate 中有- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithTextCheckingResult:(NSTextCheckingResult *)result TTTAttributedLabelDelegate 方法。当你点击 # 或 @ 文本时,会使用 NSTextCheckingResult 调用它。

这就是你要找的吗?

【讨论】:

    【解决方案2】:

    由于尚未回答有关突出显示链接状态的问题,这里有一个简单的解决方案:

    var attrs = [NSFontAttributeName : UIFont.systemFontOfSize(14.0), NSForegroundColorAttributeName: UIColor.blackColor()]
    label.activeLinkAttributes = attrs
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      • 2011-08-10
      • 2020-10-05
      • 1970-01-01
      • 2016-04-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多