有时候需要显示一小段html代码,不想用UIWebView,想用Lable或者TextView直接显示。
NSString *htmlString = @"<h1>Header</h1><h2>Subheader</h2><p>Some <em>text</em></p><img src='http://120.24.42.51:8080/BibiGoddess/UploadImages/admin/avatar/20160128c387f5bc-7a0e-4608-a90a-41312b4fa3b7.jpg' width=70 height=100 />";
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
self.textView.attributedText = attributedString;
self.label.attributedText = attributedString;
显示效果