我使用以格式返回超链接的 api。
我使用正则表达式来获取标签之间的文本,并使用 href 之间的文本来获取 url。我想在标签之间显示文本并链接到 href 中的 url。
我创建这样的标签:
TTTAttributedLabel * attributedLabel = [[TTTAttributedLabel alloc] init];
[attributedLabel setFont:font];
[attributedLabel setTextColor:[UIColor colorFromHex:0x4b4b4b]];
[attributedLabel setLineBreakMode:NSLineBreakByWordWrapping];
[attributedLabel setNumberOfLines:0];
attributedLabel.userInteractionEnabled = YES;
[attributedLabel setEnabledTextCheckingTypes:NSTextCheckingTypeLink];
[attributedLabel setDelegate:delegate];
attributedLabel.linkAttributes = @{ (id)kCTForegroundColorAttributeName: [UIColor colorFromHex:0x0080be],
(id)kCTUnderlineStyleAttributeName : [NSNumber numberWithInt:NSUnderlineStyleNone] };
[attributedLabel setText:string];
我像这样设置 url 和文本,我会省去你的正则表达式(url 设置在不同的函数中,所以属性标签现在是标签):
NSString *replacementString = [label.text substringWithRange:[match rangeAtIndex:2]];
NSString *linkString = [label.text substringWithRange:[match rangeAtIndex:1]];
label.text = [label.text stringByReplacingCharactersInRange:[match rangeAtIndex:0] withString:replacementString];
NSRange replacementRange = NSMakeRange([match rangeAtIndex:0].location, [replacementString length]);
[label addLinkToURL:[NSURL URLWithString:linkString] withRange:replacementRange];
即使我记录[标签链接],我也会得到以下结果:
(
"<NSLinkCheckingResult: 0x7b9f2780>{454, 4} {http://mysuperawesomesite.com}"
)
有谁知道我可能做错了什么?