2015-02-10 59 views
3

我可能是错的,但它似乎很多慢于它在iOS的7这一切都无法使用,但在生产与iOS 8有没有办法在iOS 8中加速HTML到NSAttributedString的渲染?

以下行:

NSString *htmlString = @"<em>emphasis</em> with some <strong>bolder text</strong>"; 
[[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)} documentAttributes:nil error:nil]; 

注意到0.48秒执行。半秒钟来解析两个简单的HTML标签。

但我不知道是否有一些奇怪的事情,因为如果我使用的HTML字符串:

NSString *htmlString = @"<em>emphasis</em> with some <strong>bolder text</strong> and <a href='http://google.ca'>google</a> with some <sup>superscript</sup> and some <sub>subscript</sub> and a <h1>header</h1> followed by some <strong><em>nested bold and italics</em></strong> plus some <em>neato</em> and some more <a href='#'>well then</a> with a bit of <del>strikethrough</del> and other text <em>emphasis</em> with some <strong>bolder text</strong> and <a href='http://google.ca'>google</a> with some <sup>superscript</sup> and some <sub>subscript</sub> and a <h1>header</h1> followed by some <strong><em>nested bold and italics</em></strong> plus some <em>neato</em> and some more <a href='#'>well then</a> with a bit of <del>strikethrough</del> and other text <em>emphasis</em> with some <strong>bolder text</strong> and <a href='http://google.ca'>google</a> with some <sup>superscript</sup> and some <sub>subscript</sub> and a <h1>header</h1> followed by some <strong><em>nested bold and italics</em></strong> plus some <em>neato</em> and some more <a href='#'>well then</a> with a bit of <del>strikethrough</del> and other text <em>emphasis</em> with some <strong>bolder text</strong> and <a href='http://google.ca'>google</a> with some <sup>superscript</sup> and some <sub>subscript</sub> and a <h1>header</h1> followed by some <strong><em>nested bold and italics</em></strong> plus some <em>neato</em> and some more <a href='#'>well then</a> with a bit of <del>strikethrough</del> and other text <em>emphasis</em> with some <strong>bolder text</strong> and <a href='http://google.ca'>google</a> with some <sup>superscript</sup> and some <sub>subscript</sub> and a <h1>header</h1> followed by some <strong><em>nested bold and italics</em></strong> plus some <em>neato</em> and some more <a href='#'>well then</a> with a bit of <del>strikethrough</del> and other text"; 

这是一个HTML字符串33X长,它需要0.51秒

因此字符串是33X长,但需要较长的时间只有1.06倍(即0.03秒的时间)来渲染。

仅仅是一个荒谬的启动时间,我可以减轻不知何故?这看起来好像渲染时间非常不合理,而在iOS 7中,我几乎可以记住它是瞬时的。

如果没有,对NSAttributedString解决方案的替代HTML的任何建议?

+2

目前还不清楚我从文档:'的HTML进口商不应该从后台线程调用(也就是选择字典包括NSDocumentTypeDocumentAttribute与NSHTMLTextDocumentType值).'是否指定这些字典中的值把它放在后台线程或主线程上? – 2015-02-10 00:11:53

回答

0

不幸的是,使用内置框架将文本从HTML转换为NSAttributedText具有不可接受的性能。你应该考虑使用第三方库,比如DCCoreText。

https://github.com/Cocoanetics/DTCoreText

+0

似乎是这样,'DTCoreText'在** 0.0285秒**中做了更长的例子,大约快了18倍。本来喜欢使用系统功能,但是很好。 – 2015-02-10 02:43:54

+0

我们为我们的项目做了性能比较,并最终使用了DCCoreText。 – sha 2015-02-10 02:46:34

+0

不得不使用HTML标记渲染500多个tableview单元格,就像它需要显示一样,这会破坏滚动。切换到DTCoreText,解决了这个问题。 WWDC 2017的苹果工程师几乎告诉我“是的,没有人再使用这个HTML,不知道它为什么坏了” – Sirens 2017-06-07 03:05:07

相关问题