简体   繁体   English

在iOS应用程序中使用HTML标签显示文本的最佳方法?

[英]Best way to display text with HTML tags in iOS application?

Currently I am working on iOS client for web-chat. 目前我正在iOS客户端上进行网络聊天。 Thus chat messages have HTML tags (bold, italic, underlined, font color, images (smiles), etc.). 因此,聊天消息具有HTML标签(粗体,斜体,下划线,字体颜色,图像(微笑)等)。 For example: 例如:

<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: 目前我有2个想法如何显示消息:

  1. Add UIWebView to tables cell. 将UIWebView添加到表格单元格。 But I think that it's not an option, because we will have a lot of messages and a lot of WebViews. 但我认为这不是一个选项,因为我们会有很多消息和很多WebView。
  2. Add UITextView to tables cell, parse HTML tags and make necessary changes to attributed string. 将UITextView添加到表格单元格,解析HTML标记并对属性字符串进行必要的更改。 But UITextView doesn't support images (if I am right). 但是UITextView不支持图像(如果我是对的)。

Is there any other (better) way to display such data (styled text + images)? 有没有其他(更好的)方式来显示这样的数据(样式文本+图像)?

Using a webview per-cell is not going to work as you suspect. 使用每个单元格的webview不会像你怀疑的那样工作。 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需要花费大量时间进行渲染,这意味着您可能会在重新使用的单元格中暂时显示旧的webview内容,直到新内容呈现为止。 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也是一个非常重量级的UI元素,你会遇到性能问题,它们会在屏幕上同时更新很多。

You will want to parse the HTML text you are given into an attributed string using a library like DTCoreText . 您将需要使用像DTCoreText这样的库将您给出的HTML文本解析为属性字符串。 From here, if you can target iOS 6 or later you can set the attributedText property on a standard UILabel . 从这里开始,如果您可以定位iOS 6或更高版本,则可以在标准UILabel上设置attributedText属性。 If you need to target earlier iOS versions you can again use DTCoreText, specifically the DTAttributedLabel or DTAttributedTextCell components. 如果您需要定位早期iOS版本,则可以再次使用DTCoreText,特别是DTAttributedLabelDTAttributedTextCell组件。

The parsing and NSAttributedString rendering can all be done manually using an XML parser and CoreText, but DTCoreText will make your life much easier. 解析和NSAttributedString呈现都可以使用XML解析器和CoreText手动完成,但DTCoreText将使您的生活更轻松。

Update: You mentioned in a comment that you want support for <img/> . 更新:您在评论中提到您希望支持<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. DTCoreText确实有一些基本的图像支持,但如果您使用CoreText渲染文本,这是一个非常难的问题,因为您必须正确地在图像周围流动文本,并在核心文本渲染器中保留一些空间以放置图像。 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. 如果每个单元格只有一个图像,我建议您手动提取图像路径/网址,并在文本旁边使用UIImageView布局。

You can get idea from RTLabel stuff. 你可以从RTLabel的东西中获得想法。 It is doing same thing which you want. 它正在做你想要的同样的事情。

You can also convert HTML to NSAttributedString with native iOS classes. 您还可以使用本机iOS类将HTML转换为NSAttributedString。 Look the following post https://stackoverflow.com/a/18886718/1760527 请查看以下帖子https://stackoverflow.com/a/18886718/1760527

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM