uitextview显示html

有时候需要显示一串HTML的字符串,用UILabel UITextViewattributedText属性都可以展示,但是如果HTML中有链接,要实现点击的话建议还是使用UITextView

代码示例

1
2
3
4
5
6
7
NSData *data = [htmlString dataUsingEncoding:NSUnicodeStringEncoding];
NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};
NSAttributedString *html = [[NSAttributedString alloc]initWithData:data
options:options
documentAttributes:nil
error:nil];
textView.attributedText = html;

还需要实现UITextView的代理

1
2
3
4
5
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {


return YES;
}