Ios TTTAttributedLabel自定义添加链接ToURL不工作
我使用了一个api,它以格式返回超链接 我使用正则表达式获取标记之间的文本,使用href之间的文本获取url。我想在标签之间显示文本,并在href中链接到url 我创建的标签如下所示:Ios TTTAttributedLabel自定义添加链接ToURL不工作,ios,objective-c,tttattributedlabel,Ios,Objective C,Tttattributedlabel,我使用了一个api,它以格式返回超链接 我使用正则表达式获取标记之间的文本,使用href之间的文本获取url。我想在标签之间显示文本,并在href中链接到url 我创建的标签如下所示: TTTAttributedLabel * attributedLabel = [[TTTAttributedLabel alloc] init]; [attributedLabel setFont:font]; [attributedLabel setTextColor:[UIColor colorFromHex
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是在不同的函数中设置的,因此attributedLabel现在是label):
即使我记录了[label links],我也会得到以下结果:
(
"<NSLinkCheckingResult: 0x7b9f2780>{454, 4} {http://mysuperawesomesite.com}"
)
(
"{454, 4} {http://mysuperawesomesite.com}"
)
有人知道我可能做错了什么吗?您必须使用
TTTAttributedLabel
的指定初始值设定项initWithFrame:
,即使您将cRectZero
作为帧传递。您在这里使用的是init
,它不会初始化链接数组和其他各种内部属性。您希望得到什么结果?
(
"<NSLinkCheckingResult: 0x7b9f2780>{454, 4} {http://mysuperawesomesite.com}"
)