Skip to content

Instantly share code, notes, and snippets.

@raberm
Created January 21, 2023 10:44
Show Gist options
  • Save raberm/a8097399f37eee604e98b4b1e50202dc to your computer and use it in GitHub Desktop.
Save raberm/a8097399f37eee604e98b4b1e50202dc to your computer and use it in GitHub Desktop.
html to NSAttributedString
extension String {
func htmlAttributedString() -> NSAttributedString? {
guard let data = self.data(using: String.Encoding.utf16, allowLossyConversion: false) else { return nil }
guard let html = try? NSMutableAttributedString(
data: data,
options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html],
documentAttributes: nil) else { return nil }
return html
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment