Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save shakemno/d4cccb167e8f65901ab61e7ed8f8783b to your computer and use it in GitHub Desktop.
Save shakemno/d4cccb167e8f65901ab61e7ed8f8783b to your computer and use it in GitHub Desktop.
converting HTML to NSAttributedString "pseudo-asynchronously"
import UIKit
extension NSAttributedString {
static func fromHTML(_ html: String, execute: @escaping (NSAttributedString?) -> Void) {
DispatchQueue.main.async {
let options = [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html]
// only run on main queue
let result = try? NSAttributedString(data: Data(html.utf8), options: options, documentAttributes: nil)
execute(result)
}
}
}
NSAttributedString.fromHTML("hello <u>world</u>") { attrStr in
// attrStr is an NSAttributedString?
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment