如何最小化将HTML转换为NSAttributedString所需的时间

4

你好,我正在使用以下代码将HTML转换为NSAttributtedString。我的问题是第一次执行它需要很长时间:

var html = "<b>Whatever...</b>"    
var attributedText = try! NSMutableAttributedString(
        data: html.data(using: String.Encoding.unicode, allowLossyConversion: true)!,
        options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
        documentAttributes: nil)

当我第一次执行转换时,它需要很长时间才能执行。后续的执行时间会减少。有没有办法减少这个长时间的第一次执行? 我考虑在我的应用程序开始时在后台执行此代码,但我想知道是否有其他聪明的解决方案或库可以导入。


从您的潜在解决方案中,请注意文档中提到的内容:HTML 导入器不应该从后台线程调用(也就是说,选项字典包括 NSDocumentTypeDocumentAttribute 并且其值为 NSHTMLTextDocumentType)。 - Larme
这是一个非常好的问题,Fernando。我们发现没有办法让它更快。我所做的就是像你说的那样在另一个线程上运行。当然,这会导致很多用户体验问题。当然,你必须要缓存它:/ - Fattie
请注意,这个库可能会有所帮助 https://github.com/malcommac/SwiftRichString 相关文章请参考 https://medium.com/breakfastcode/attributed-strings-in-swift-6d4b37db59a5 - Fattie
你好@ƒernando Valle,你找到解决这个问题的方法了吗?我也遇到了同样的问题。 - Mehul Thakkar
你好 @MehulThakkar,我没有找到任何减少时间的解决方案,但我在我的appdelegate类中添加了一个假的html加载来“黑客”它。我会将其发布为答案,希望能帮到你。 - ƒernando Valle
感谢@ƒernando Valle的帮助。 - Mehul Thakkar
2个回答

0

我没有找到任何减少转换时间的方法。但是有一种方法可以“黑客”加载时间。

似乎第一次调用转换过程需要很长时间,但在下一次调用时需要的时间较少。通过这种方式,我解决了在我的AppDelegate中加载一小段“html垃圾邮件”的问题。因此,在使用应用程序期间,用户将不会注意到缓慢的加载。

AppDelegate:

override func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {

    super.application(application, willFinishLaunchingWithOptions: launchOptions)
    try! NSMutableAttributedString(
        data: "<a>asdasd</a>".data(using: String.Encoding.unicode, allowLossyConversion: true)!,
        options: [
            NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType,
            NSCharacterEncodingDocumentAttribute: String.Encoding.unicode.rawValue
        ],
        documentAttributes: nil)

    return true
}

在 ViewControllers 中,您可以像平常一样调用:
var html = "<b>Whatever...</b>"    
var attributedText = try! NSMutableAttributedString(
    data: html.data(using: String.Encoding.unicode, allowLossyConversion: true)!,
    options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
    documentAttributes: nil)

@Mehul Thakkar 请试一下并告诉我 ;) - ƒernando Valle
我尝试了这个,它确实减少了一些时间,但仍然不够流畅。 - Mehul Thakkar
对我来说,它没有帮助 :( - Tung Fam

-1
使用 "String.Encoding.utf8" 替代 "String.Encoding.unicode",这样可以减少转换时间。
var html = "<bold>Wow!</bold> Now <em>iOS</em> can create <h3>NSAttributedString</h3> from HTMLs!"

let attributedOptions: [String: Any] = [
                    NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType,
                    NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue
                ]

var attrString = try! NSAttributedString(data: html.data(using: String.Encoding.utf8)!, options: attributedOptions, documentAttributes: nil)
YOUR_TEXT_VIEW.attributedText = attrString

我看到了你的问题。你在HTML转换中遇到了一些延迟,我认为这是因为你使用了"String.Encoding.unicode"而不是"String.Encoding.utf8"。我认为这样做会减少你的转换时间。 - Milap Kundalia
1
值得指出的是,如果您认为这是问题的原因,那么您的答案中应该有所不同。 - Larme
抱歉,我没有注意到这个变化。看起来这个解决方案确实能够稍微减少转换时间,谢谢。 - ƒernando Valle
似乎它不能正常工作,我不知道是否有什么保存在缓存中,但今天(也许是因为重新启动)它没有最小化时间。 - ƒernando Valle
这个是错误的,这里没有时间差异。 - Fattie

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接