TTTAttributedLabel 过滤html标签 并且识别内部的链接电话等

从服务器获取一段文本并将其显示在 TTTAttributedLabel 的 ios 应用程序。显示的文本是从 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];
但是,如果这个词"世界"不止一次出现在文本中,上面的代码会错了。

任何一个可以提出一个更好的方法来处理这种情况吗?谢谢你


    _hideContenLabel.text = [[NSAttributedString alloc] initWithData:[[NSString stringWithFormat:@"%@ewqewqewqehwqi我表哥万泉河i饿还未i漆黑我去黑u黄婉秋嗯哼i武器黑u万泉河iu饿还未iu漆黑u我去黑u黄婉秋文档黄婉秋鹅湖万泉河i黑 18236979641",serviceModel.content]
                                                                   dataUsingEncoding:NSUnicodeStringEncoding]
                                                          options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType }
                                               documentAttributes:nil
                                                        error:nil];
   CGFloat hideLabelHeight =  ([TTTAttributedLabel sizeThatFitsAttributedString: [[NSAttributedString alloc] initWithData:[[NSString stringWithFormat:@"%@ewqewqewqehwqi我表哥万泉河i饿还未i漆黑我去黑u黄婉秋嗯哼i武器黑u万泉河iu饿还未iu漆黑u我去黑u黄婉秋文档黄婉秋鹅湖万泉河i黑 18236979641",serviceModel.content]
                                                                                                dataUsingEncoding:NSUnicodeStringEncoding]
                                                                                       options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType }
                                                                            documentAttributes:nil
                                                                                         error:nil] withConstraints:CGSizeMake(SCREEN_width - 2 *UMMagin, MAXFLOAT) limitedToNumberOfLines:0]).height;
    UMLog(@"%lf",hideLabelHeight

解决方法 1:
最后告终使用 NSAttributedString 来处理这个问题。这里是我的代码。

TTTAttributedLabel *_contentLabel = [[TTTAttributedLabel alloc] init];
_contentLabel.backgroundColor = [UIColor clearColor];
_contentLabel.numberOfLines = 0;
_contentLabel.enabledTextCheckingTypes = NSTextCheckingTypeLink;
_contentLabel.delegate = self;

_contentLabel.text = [[NSAttributedString alloc] initWithData:[[_model.content trimString]
                                                               dataUsingEncoding:NSUnicodeStringEncoding]
                                                      options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType }
                                           documentAttributes:nil
                                                        error:nil];

我也在我的应用程序需要的是更新的字体大小 _contentLabel 上飞。而这里是代码。

NSFont *newFont = ...; // new font

NSMutableAttributedString* attributedString = [_contentLabel.attributedText mutableCopy];

[attributedString beginEditing];
[attributedString enumerateAttribute:NSFontAttributeName inRange:NSMakeRange(0, attributedString.length) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) {
    [attributedString removeAttribute:NSFontAttributeName range:range];
    [attributedString addAttribute:NSFontAttributeName value:newFont range:range];
}];
[attributedString endEditing];

_contentLabel.text = [attributedString copy];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容