使用UIWebView中html标签显示富文本
2014-05-12 10:44
411 查看
使用UIWebView中html标签显示富文本
viewDidLoad
效果如下:
Question
http://stackoverflow.com/questions/15872921/best-way-to-display-text-with-html-tags-in-ios-application
Currently I am working on iOS client for web-chat. Thus chat messages have HTML tags (bold, italic, underlined, font color, images (smiles), etc.). For example:
最近,我要给一个网页聊天的网站做一个客户端.这些聊天信息包含了很多HTML标签(bold, italic, underlined, font color, images (smiles), etc.),例如:
For the moment I have 2 ideas how to display messages:
当下,我有两个思路来显示这些信息:
Add UIWebView to tables cell. But I think that it's not an option, because we will have a lot of messages and a lot of WebViews. 将UIWebView添加到cell中,但这个备选方案问题多多,因为我有很火很多消息需要显示,同时也需要很多很多的WebView.
Add UITextView to tables cell, parse HTML tags and make necessary changes to attributed string. But UITextView doesn't support images (if I am right). 将UITextView添加到cell中,解析HTML标签并转换成富文本标签.但是,UITextView不支持图片显示(如果我没猜错的话).
Is there any other (better) way to display such data (styled text + images)?
是否有其他更好的方式来显示富文本加图片呢?
Answer
Using a webview per-cell is not going to work as you suspect. Webviews take a noticeable time to render which means you will likely end up with old webview content being momentarily displayed in reused cells until the new content renders. Webview is also a pretty heavy-weight UI element and you will encounter performance issues with having many of them on the screen updating at once.
将webView添加到cell中绝对达不到你的预期.WebView需要很长很长时间来渲染,在cell中重用你就会死.WebView是开销非常大的UI控件.
You will want to parse the HTML text you are given into an attributed string using a library like DTCoreText. From here, if you can target iOS 6 or later you can set the
也许你需要 DTCoreText 来帮助你将HTML标签解析成富文本.
The parsing and
虽然解析HTML以及富文本的渲染都可以通过手动的XML解析器以及CoreText来实现, 只不过 DTCoreText 让你更轻松而已.
Update: You mentioned in a comment that you want support for
注意:你提到了一个要素就是想支持<img/>.DTCoreText 支持几种基本的image,但是如果你想用 CoreText 来实现这种图文混排的工作会让你非常蛋疼.如果你仅仅是展示一张图片到一个cell当中.我建议你直接用UIImageView来显示图片算了.
- (void)viewDidLoad { [super viewDidLoad]; // 注册字体 REGISTER_FONT(bundleFont(@"XinDiXiaoWanZi.ttf"), @"新蒂小丸子体"); NSString *jsString = [NSString stringWithFormat: @"<html> \n" // 设置字体的css样式 "<head> \n" "<style type=\"text/css\"> \n" "body {font-size: %f; font-family: \"%@\"; color: %@;}\n" "</style> \n" "</head> \n" // 字符串显示部分 "<body>%@</body> \n" "</html>", 37.f, // 字体大小 CUSTOM_FONT(@"新蒂小丸子体", 0), // 字体类型 @"#000000", // 字体颜色 @"走向世界的<span style=\"color:red;\">尽头</span>!"]; UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 100, 320, 70)]; [webView loadHTMLString:jsString baseURL:nil]; [self.view addSubview:webView]; }
viewDidLoad
效果如下:
Question
http://stackoverflow.com/questions/15872921/best-way-to-display-text-with-html-tags-in-ios-application
Currently I am working on iOS client for web-chat. Thus chat messages have HTML tags (bold, italic, underlined, font color, images (smiles), etc.). For example:
最近,我要给一个网页聊天的网站做一个客户端.这些聊天信息包含了很多HTML标签(bold, italic, underlined, font color, images (smiles), etc.),例如:
<b>bold</b> <i>italic</i> <!--smile:bird--><img style="vertical-align: middle;border: none;" alt="bird" src="http://www.site.com/engine/data/emo_chat/bird.gif" /><!--/smile--> ordinaty text
For the moment I have 2 ideas how to display messages:
当下,我有两个思路来显示这些信息:
Add UIWebView to tables cell. But I think that it's not an option, because we will have a lot of messages and a lot of WebViews. 将UIWebView添加到cell中,但这个备选方案问题多多,因为我有很火很多消息需要显示,同时也需要很多很多的WebView.
Add UITextView to tables cell, parse HTML tags and make necessary changes to attributed string. But UITextView doesn't support images (if I am right). 将UITextView添加到cell中,解析HTML标签并转换成富文本标签.但是,UITextView不支持图片显示(如果我没猜错的话).
Is there any other (better) way to display such data (styled text + images)?
是否有其他更好的方式来显示富文本加图片呢?
Answer
Using a webview per-cell is not going to work as you suspect. Webviews take a noticeable time to render which means you will likely end up with old webview content being momentarily displayed in reused cells until the new content renders. Webview is also a pretty heavy-weight UI element and you will encounter performance issues with having many of them on the screen updating at once.
将webView添加到cell中绝对达不到你的预期.WebView需要很长很长时间来渲染,在cell中重用你就会死.WebView是开销非常大的UI控件.
You will want to parse the HTML text you are given into an attributed string using a library like DTCoreText. From here, if you can target iOS 6 or later you can set the
attributedTextproperty on a standard
UILabel. If you need to target earlier iOS versions you can again use DTCoreText, specifically the
DTAttributedLabelor
DTAttributedTextCellcomponents.
也许你需要 DTCoreText 来帮助你将HTML标签解析成富文本.
The parsing and
NSAttributedStringrendering can all be done manually using an XML parser and CoreText, but DTCoreText will make your life much easier.
虽然解析HTML以及富文本的渲染都可以通过手动的XML解析器以及CoreText来实现, 只不过 DTCoreText 让你更轻松而已.
Update: You mentioned in a comment that you want support for
<img/>. DTCoreText does have some basic image support, but this is a really hard problem if you are rendering text with CoreText because you have to make text flow around the image correctly, and reserve some space in the core text renderer to put your image into. If there is just a single image for each cell, I would suggest you manually extract the image path/url and lay it out with a
UIImageViewalongside your text.
注意:你提到了一个要素就是想支持<img/>.DTCoreText 支持几种基本的image,但是如果你想用 CoreText 来实现这种图文混排的工作会让你非常蛋疼.如果你仅仅是展示一张图片到一个cell当中.我建议你直接用UIImageView来显示图片算了.
相关文章推荐
- 使用UIWebView中html标签显示富文本
- 【HTML】使用Iframe标签显示目标网页(内容)的某区域
- IOS使用正则表达式去掉html中的标签元素,获得纯文本
- HTML语言基础之——使用标签制作文本内容(1)
- IOS使用正则表达式去掉html中的标签元素,获得纯文本
- iOS UILabel 显示带有html标签的富文本内容
- js使用html()或text()方法获取设置p标签的显示的值
- wordpress使用pre标签来显示你的HTML
- html使用display:inline-block实现标签右对齐,值左对齐效果。和设置div宽度,并居中显示。嵌套div的里层div文字居中显示
- [置顶] ajax请求得到后台数据,前台页面,使用table模板然后clone,显示表格,,不用append拼接 “字符串和HTML表格标签”
- iOS中使用正则表达式去掉HTML中的标签元素获得纯文本的方法
- 如何使用JS脚本从HTML中分离图片标签与文本,替换文本中指定的内容并加粗(原创)
- 怎么将html标签当做文本显示?
- 【HTML】使用iframe标签显示目标网页(内容)的指定区域
- HTML学习---------1.14 在文本中插入一行或多行代码使用的标签<code> <pre>
- 解决在使用flask时向网页中插入一段包含html标签的内容正常显示的问题
- Android中Textview显示带html文本一-------【HTML标签】
- iphone开发之使用UIWebView显示html内容
- 关于使用bean:write标签显示html内容
- 关于使用bean:write标签显示html内容