Skip to content

Instantly share code, notes, and snippets.

@sooch
Created October 27, 2019 09:31
Show Gist options
  • Save sooch/0a8f2d08f521f8275f7fa80274d914d5 to your computer and use it in GitHub Desktop.
Save sooch/0a8f2d08f521f8275f7fa80274d914d5 to your computer and use it in GitHub Desktop.
extension String {
func asHtmlAttributedString() -> NSAttributedString? {
guard let htmlData = self.data(using: String.Encoding.utf8, allowLossyConversion: true) else { return nil }
let options: [NSAttributedString.DocumentReadingOptionKey: Any] = [
.documentType: NSAttributedString.DocumentType.html,
.characterEncoding: String.Encoding.utf8.rawValue
]
return try? NSAttributedString(data: htmlData, options: options, documentAttributes: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment