Created
July 29, 2023 13:13
-
-
Save foxicode/cc6c5d6a427a8f917e1fb4899ca7f8ae to your computer and use it in GitHub Desktop.
HTML to Attributed String conversion in Swift
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
| public extension String { | |
| var html: NSAttributedString { | |
| guard let data = data(using: .utf8) else { return NSAttributedString() } | |
| do { | |
| return try NSAttributedString( | |
| data: data, | |
| options: [ | |
| NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html, | |
| NSAttributedString.DocumentReadingOptionKey.characterEncoding: String.Encoding.utf8.rawValue | |
| ], | |
| documentAttributes: nil | |
| ) | |
| } catch { | |
| return NSAttributedString() | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment