Ack*_*man 1 xcode objective-c hyperlink nsstring uilabel
我从文本形式的api中得到了回复,我希望扫描该文本中的"Planned Parenthood health center",并在该词上插入一个超链接,重定向到plannedparenthood.org/health-center门户.
我的方法:
NSString *text = response.content;
text = [text stringByReplacingOccurrencesOfString:@"Planned Parenthood health center"
withString:@"<a href=\"http://www.plannedparenthood.org/health-center\">Planned Parenthood health center</a>"];
Run Code Online (Sandbox Code Playgroud)
虽然文本上的链接正在被替换.它正在被取代
<a href=\"http://www.plannedparenthood.org/health-center\">Planned Parenthood health center </a>
Run Code Online (Sandbox Code Playgroud)
有什么不对,为什么我没有在那里得到任何链接?我做错了什么我还将用户启用的交互设置为YES
iOS实际上并不能很好地处理这个问题,因为它打算使用UIButtons,它更易于用户点击.
要实现您想要的功能,您不能使用UILabel - 您需要使用UITextView并设置其attributedText属性:
NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:@"Planned Parenthood health center"];
[attributedString addAttribute: NSLinkAttributeName value: @"http://www.plannedparenthood.com" range: NSMakeRange(0, str.length)];
textView.attributedText = attributedString;
Run Code Online (Sandbox Code Playgroud)
请注意,UITextView必须是可选择的,但不可编辑.
如果您的属性字符串中有其他未链接的文本,则此方法有一些缺点.例如,如果您有"请选择此处以转到计划生育的网站",其中"此处"是您要链接的文本的唯一部分.在这种情况下,如果您点击"here"旁边的文本中的任何其他位置,您会注意到用户可以选择它并具有蓝色突出显示.
如果不需要使用UITextView,则需要使用自定义内容,例如TTTAttributedLabel
| 归档时间: |
|
| 查看次数: |
6315 次 |
| 最近记录: |