如何最小化时间转换 html 到 NSAttributedString
How to minimize time conversion html to NSAttributedString
您好,我正在使用以下代码将 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)
当我第一次执行转换时,需要很长时间才能执行。接下来的执行花费的时间更少。有什么办法可以减少第一次执行的时间?
我考虑过在我的应用程序执行开始时在后台执行此代码,但我想知道是否还有其他我应该导入的智能解决方案或库。
use "String.Encoding.utf8" instead of "String.Encoding.unicode"
this might reduce your conversion time
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
我没有找到任何减少转换时间的方法。但是有一种方法"hack"加载时间。
第一次调用转换的过程似乎需要很长时间,但在接下来的调用中会花费更少的时间。这样,我就解决了在我的AppDelegate中加载一段"html spam"的问题。因此,用户在使用应用程序期间不会注意到加载缓慢。
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
}
在 ViewController 中您可以正常调用:
var html = "<b>Whatever...</b>"
var attributedText = try! NSMutableAttributedString(
data: html.data(using: String.Encoding.unicode, allowLossyConversion: true)!,
options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: nil)
您好,我正在使用以下代码将 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)
当我第一次执行转换时,需要很长时间才能执行。接下来的执行花费的时间更少。有什么办法可以减少第一次执行的时间? 我考虑过在我的应用程序执行开始时在后台执行此代码,但我想知道是否还有其他我应该导入的智能解决方案或库。
use "String.Encoding.utf8" instead of "String.Encoding.unicode" this might reduce your conversion time
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
我没有找到任何减少转换时间的方法。但是有一种方法"hack"加载时间。
第一次调用转换的过程似乎需要很长时间,但在接下来的调用中会花费更少的时间。这样,我就解决了在我的AppDelegate中加载一段"html spam"的问题。因此,用户在使用应用程序期间不会注意到加载缓慢。
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
}
在 ViewController 中您可以正常调用:
var html = "<b>Whatever...</b>"
var attributedText = try! NSMutableAttributedString(
data: html.data(using: String.Encoding.unicode, allowLossyConversion: true)!,
options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: nil)