Created
May 16, 2019 16:23
-
-
Save maltalef/f5b95beda1acee8a1278454f2a24216f to your computer and use it in GitHub Desktop.
converting HTML to NSAttributedString "pseudo-asynchronously"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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