前两天看搜狐家明哥写的《TextKit实现图文混排》 今晚回家看了下API发现了一个更加取巧的实现方式.可以直接将后台返回的html富文本用textView显示出来.
记得两年前当时做这个的时候还是借助了笨重的webview.
>
Textkit是iOS7新推出的类库,其实是在之前推出的CoreText上的封装,有了这个TextKit,以后不用再拿着CoreText来做累活了,
下面是我分别用UITextView 和UIWebView 显示一段图文混合的文字
- (id)initWithFileURL:(NSURL *)url options:(NSDictionary *)options documentAttributes:(NSDictionary **)dict error:(NSError **)error
Initializes a new attributed string object from the data at the given URL.
The HTML importer should not be called from a background thread (that is, the options dictionary includes NSDocumentTypeDocumentAttribute with a value of NSHTMLTextDocumentType). It will try to synchronize with the main thread, fail, and time out. Calling it from the main thread works (but can still time out if the HTML contains references to external resources, which should be avoided at all costs). The HTML import mechanism is meant for implementing something like markdown (that is, text styles, colors, and so on), not for general HTML import.
<meta charset="UTF-8"> <div style="background-color:lightgrey; font-size:14px; color:#304182; text-align:center; margin-left:5px; padding-right:5px"> <p>Hi <span style="font-size:18px; color:#E88834;"> Taobao </span> <img src="hufeng.png" height="32" width="32" /> <p> 静态图片 <img src="taobao.gif" height="32" width="52"> <p> 动态图片 </p> </div>
如下图去掉<meta charset="UTF-8">的显示效果
- (void)loadHtmlToWebView
{
NSURL *url = [[NSBundle mainBundle] URLForResource:@"text" withExtension:@"html"];
[_webView loadRequest:[NSURLRequest requestWithURL:url]];
}
- (void)loadHtmlToTextView { // Create attributed string from HTML NSURL *url = [[NSBundle mainBundle] URLForResource:@"text" withExtension:@"html"]; NSAttributedString *attrStr = [[NSAttributedString alloc] initWithFileURL:url options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil]; [_textView setAttributedText:attrStr]; }
演示代码下载
http://vdisk.weibo.com/s/aOcPgYpXA04g2