iOS系列教程之TextKit实现图文混排读后记
来源:互联网 发布:机器视觉算法工程师 编辑:程序博客网 时间:2024/06/05 03:46
前两天看搜狐家明哥写的《TextKit实现图文混排》 今晚回家看了下API发现了一个更加取巧的实现方式.可以直接将后台返回的html富文本用textView显示出来.
记得两年前当时做这个的时候还是借助了笨重的webview.
>
Textkit是iOS7新推出的类库,其实是在之前推出的CoreText上的封装,有了这个TextKit,以后不用再拿着CoreText来做累活了,
下面是我分别用UITextView 和UIWebView 显示一段图文混合的文字
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
<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=
""
height=
"32"
width=
"32"
style=
"display: none;"
><img alt=
"加载中..."
title=
"图片加载中..."
src=
"http://www.2cto.com/statics/images/s_nopic.gif"
>
</p><p> 静态图片
<img src=
""
height=
"32"
width=
"52"
style=
"display: none;"
><img alt=
"加载中..."
title=
"图片加载中..."
src=
"http://www.2cto.com/statics/images/s_nopic.gif"
>
</p><p> 动态图片
</p>
</div>
- 需要注意的是第一行需要指明编码格式 否则的话 中文会显示乱码gif图片textView会自动转换成静态图片进行显示
如下图去掉的显示效果
功能函数
- (
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
原文链接:http://www.2cto.com/kf/201401/274438.html
- iOS系列教程之TextKit实现图文混排读后记
- iOS系列教程之TextKit实现图文混排读后记
- TextKit实现图文混排
- TextKit实现图文混排
- TextKit实现图文混排
- 用textkit实现图文混排
- iOS 7系列译文:认识 TextKit
- iOS 7系列译文:认识 TextKit
- iOS 7系列译文:认识 TextKit
- iOS 7系列译文:认识 TextKit
- iOS 7系列译文:认识 TextKit
- iOS 7系列译文:认识 TextKit
- iOS-------------TextKit
- iOS-TextKit
- [iOS开发]TextKit之动态改变样式
- iOS之文本处理框架TextKit介绍
- iOS系列教程之 快捷键
- 图文混排(TextKit)
- Android tabhost+侧滑菜单
- 如何在liunx下编译一个调用到c语言开发的动态库(so文件)的fortran程序。
- SVN图标含义
- CMD运行指令
- 站长必会数据统计工具教程:百度统计 VS GA
- iOS系列教程之TextKit实现图文混排读后记
- TCP/IP各层的数据包长度
- Open gop and Closed gop of H264
- 输出A幂集
- 霍尼韦尔的基于ACTIVEX的SDK的用法
- Android与linux的区别与联系
- OpenCV学习笔记(一):读取、显示、保存图片
- 开源 Android pdf 阅读器开发总结
- WPF 布局控件 之 WrapPanel