使用UIWebView中html标签显示富文本

使用UIWebView中html标签显示富文本

用UIWebView来渲染文本并期望达到富文本的效果开销很大哦!

 

Work

本人此处直接加载自定义字体"新蒂小丸子体",源码不公开,望见谅.

代码如下:

渲染后效果如下图所示,效果是不是挺不错的呢.

我们再把尽头两个字渲染成红色试试.

源码:

- (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:

当下,我有两个思路来显示这些信息:

  1. 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.
  2. 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 attributedText property on a standard UILabel. If you need to target earlier iOS versions you can again use DTCoreText, specifically the DTAttributedLabel or DTAttributedTextCell components.

也许你需要 DTCoreText 来帮助你将HTML标签解析成富文本.

 

The parsing and NSAttributedString rendering 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 UIImageView alongside your text.

注意:你提到了一个要素就是想支持<img/>.DTCoreText 支持几种基本的image,但是如果你想用 CoreText 来实现这种图文混排的工作会让你非常蛋疼.如果你仅仅是展示一张图片到一个cell当中.我建议你直接用UIImageView来显示图片算了.

时间: 2024-07-28 16:25:21

使用UIWebView中html标签显示富文本的相关文章

代码-如何获取日常richtextbox中的全部的富文本,richtextbox中文本有加粗或者颜色

问题描述 如何获取日常richtextbox中的全部的富文本,richtextbox中文本有加粗或者颜色 一般都是.text获取普通文本,但是richtextbox中文本加粗或者颜色变了,我该如何获取这些全部的富文本用什么代码 解决方案 那你只能一个字一个字去循环判断该字是否是颜色加粗等样式了

Qt之QTableView显示富文本

简述 对于QTableView中的显示,我们前面介绍过很多种,其中包括:文本.进度条.复选框等,今天我们介绍一下关于富文本的显示. 可能绝大多数小伙伴会通过QAbstractTableModel中的data来实现,可是现实告诉我们,那是行不通的,那么我们如何去显示呢?请看正文. 简述 效果 源码 分析 效果 源码 下面我们使用QAbstractItemDelegate来进行富文本的显示,主要通过QTextDocument设置HTML代码来实现. void CheckBoxDelegate::pa

java怎么把数据库中的值显示到文本框中

问题描述 用的是Jbuilder!!谢谢!! 解决方案 解决方案二:我举个例子,你一看就懂.下面是查询语句.你然后到你要显示数据的text框那边.添加以下代码:finalJTextFieldscorefield=newJTextField();finalGetscoregetscore=newGetscore();List<String>list=getscore.selectscour(sname,cname);for(Stringitem:list){scorefield.setText(

[翻译] DTCoreText 从HTML文档中创建富文本

DTCoreText 从HTML文档中创建富文本 https://github.com/Cocoanetics/DTCoreText   注意哦亲,DTRichTextEditor 这个组件是收费的,不贵,才650美元而已^_^.   DTCoreText This project aims to duplicate the methods present on Mac OSX which allow creation of NSAttributedString from HTML code o

iOS文本布局探讨之三——使用TextKit框架进行富文本布局

iOS文本布局探讨之三--使用TextKit框架进行富文本布局 一.引言         关于图文混排,其实以前的博客已经讨论很多,在实际开发中,经常使用第三方的框架来完成排版的需求,其中RCLabel和RTLabel是两个比较好用的第三方库,他们的实现都是基于UIView的,通过更底层的CoreText相关API来进行图文处理.相关介绍博客地址如下: iOS中支持HTML标签渲染的MDHTMLLaebl:http://my.oschina.net/u/2340880/blog/703254.

ThinkPHP自动转义存储富文本编辑器内容导致读取出错的解决方法_php技巧

ThinkPHP的conf文件中的Convention.php有一个配置选项 'DEFAULT_FILTER'        =>  'htmlspecialchars', // 默认参数过滤方法 用于I函数... 默认这个方法是开启的.也就是说,我们往数据库里面存储的数据中都会经过htmlspecialchars这个函数的转义处理. 我在我的项目中使用了Kindeditor富文本编辑器(或许你使用的是Ueditor\ckeditor),通过富文本编辑器编辑文章的内容然后存储到数据库中,再从数据

asp.net mvc2.0富文本kindeditor问题

问题描述 KindEditor.ready(function(K){//实现显示富文本的代码,第一个方法,实现添加的框架window.editorAdd=K.create('#txtMainContent',{cssPath:'../../../Content/kindeditor-4.1.10/plugins/code/prettify.css',uploadJson:'upload_json.ashx',fileManagerJson:'file_manager_json.ashx',all

asp.net中WPF自定义富文本显示控件

RichTextBox比较的强大,支持富文本和简单文本等,可以实现出类似Word的那样的效果. 今天自定义一个支持富文本显示的RichTextBox控件.  代码如下 复制代码 XAML代码: <UserControl x:Class="Kaitone.DetectiveHelper.UI.Controls.RichTextBox.RichboxTextShow"      xmlns="http://schemas.microsoft.com/winfx/2006/x

向Excel工作表中添加标签或文本框

  了解标签和文本框 标签是具有以下用途的文本:标识单元格或文本框的作用或者显示说明性文本,例如标题.题注或简短说明.此外,标签还可以显示描述性图片.为了强调文本以及在合并单元格或特定单元格位置不是可行的解决方案时,可以使用标签作为灵活的布局说明. 文本框是一个矩形框,在其中可以查看.输入或编辑绑定到单元格的文本或数据.文本框也可以是显示只读信息的静态文本字段.在需要显示自由浮动的对象时,可以使用文本框来替代在单元格中输入文本.还可以使用文本框显示或查看不受行和列边界约束的文本,并保留工作表上网