我有一个 ios 应用程序,它从服务器获取一段文本并将其显示在 TTTAttributedLabel 中。显示的文本从 HTML 中剥离。
例如
原始 HTML
<p>
Hello <a href="http://www.google.com">World!</a>
</p>
TTTAttributedLabel 中的文本显示
Hello World!
但是,我希望“世界”这个词在 HTML 中是可点击的。我知道 TTTAttributedLabel 可以像这样使用
TTTAttributedLabel *tttLabel = <# create the label here #>;
NSString *labelText = @"Hello World!";
tttLabel.text = labelText;
NSRange r = [labelText rangeOfString:@"World"];
[tttLabel addLinkToURL:[NSURL URLWithString:@"http://www.google.com"] withRange:r];
但是如果“世界”这个词在文本中出现了不止一次,上面的代码就会出错。
有人可以提出一种更好的方法来处理这种情况吗?谢谢