天天看點

使用UIWebView中html标簽顯示富文本

使用uiwebview中html标簽顯示富文本

使用UIWebView中html标簽顯示富文本

用uiwebview來渲染文本并期望達到富文本的效果開銷很大哦!

work

本人此處直接加載自定義字型"新蒂小丸子體",源碼不公開,望見諒.

使用UIWebView中html标簽顯示富文本

代碼如下:

使用UIWebView中html标簽顯示富文本

渲染後效果如下圖所示,效果是不是挺不錯的呢.

使用UIWebView中html标簽顯示富文本

我們再把盡頭兩個字渲染成紅色試試.

源碼:

使用UIWebView中html标簽顯示富文本

效果如下:

使用UIWebView中html标簽顯示富文本

question

<a href="http://stackoverflow.com/questions/15872921/best-way-to-display-text-with-html-tags-in-ios-application" target="_blank">http://stackoverflow.com/questions/15872921/best-way-to-display-text-with-html-tags-in-ios-application</a>

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.),例如:

<code>&lt;b&gt;bold&lt;/b&gt; &lt;i&gt;italic&lt;/i&gt; &lt;!--smile:bird--&gt;&lt;img style="vertical-align: middle;border: none;" alt="bird" src="http://www.site.com/engine/data/emo_chat/bird.gif" /&gt;&lt;!--/smile--&gt; ordinaty text</code>

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控件.

也許你需要 dtcoretext 來幫助你将html标簽解析成富文本.

the parsing and <code>nsattributedstring</code> 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 <code>&lt;img/&gt;</code>. 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 <code>uiimageview</code> alongside your text.

注意:你提到了一個要素就是想支援&lt;img/&gt;.dtcoretext 支援幾種基本的image,但是如果你想用 coretext 來實作這種圖文混排的工作會讓你非常蛋疼.如果你僅僅是展示一張圖檔到一個cell當中.我建議你直接用uiimageview來顯示圖檔算了.

繼續閱讀