0

我使用以格式返回超链接的 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}"
)

有谁知道我可能做错了什么?

4

1 回答 1

0

即使您作为框架传递,您也必须使用TTTAttributedLabel' 指定的初始化程序。您在此处使用,它不会初始化链接数组和各种其他内部属性。initWithFrame:CGRectZeroinit

于 2015-06-15T17:05:22.583 回答