ios textkit html,iOS系列教程之TextKit实现图文混排读后记

前两天看搜狐家明哥写的《TextKit实现图文混排》 今晚回家看了下API发现了一个更加取巧的实现方式.可以直接将后台返回的html富文本用textView显示出来.

记得两年前当时做这个的时候还是借助了笨重的webview.

>

Textkit是iOS7新推出的类库,其实是在之前推出的CoreText上的封装,有了这个TextKit,以后不用再拿着CoreText来做累活了,

下面是我分别用UITextView 和UIWebView 显示一段图文混合的文字

26b3dc04e96c1369775d7720c63fccd6.gif

API

- (id)initWithFileURL:(NSURL *)url options:(NSDictionary *)options documentAttributes:(NSDictionary **)dict error:(NSError **)error

Description

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.

测试用的html

Hi

Taobao

hufeng.png

静态图片

taobao.gif

动态图片

需要注意的是第一行需要指明编码格式 否则的话 中文会显示乱码

gif图片textView会自动转换成静态图片进行显示

如下图是去掉meta charset="UTF-8"的显示效果

5d86289ba88b8b10c2e1254d2246b0bc.png

功能函数

- (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];

}

W

本站文章如果没有特殊说明,均为原创,转载请以链接方式注明本文地址:http://hufeng825.github.com/2014/01/21/ios37/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值