Hello I am using the following code in order to convert html to NSAttributtedString. My problem is that it takes long time the first time that I executed it:
var html = "<b>Whatever...</b>"
var attributedText = try! NSMutableAttributedString(
data: html.data(using: String.Encoding.unicode, allowLossyConversion: true)!,
options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: nil)
When I perform the conversion for the first time, it takes long time executing. The followed executions takes less time. There is any way to decrease this long first execution? I thought about execute this code in background at the begining of my app execution but I want to know if there is other smart solution or library the I should import.
I did not find any way to reduce the conversion time. But there is a way for "hack" the time of load.
Seems that the process of conversion take a long time the first time it is called but less in the next calls. In this way, I solved the problem loading a piece of "html spam" in my AppDelegate. So, the user will not get noticed the slow loading during the use of the application.
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
}
In the ViewControllers you can call as normal:
var html = "<b>Whatever...</b>"
var attributedText = try! NSMutableAttributedString(
data: html.data(using: String.Encoding.unicode, allowLossyConversion: true)!,
options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: nil)