Skip to content

Instantly share code, notes, and snippets.

@ycui1
Last active December 28, 2019 16:07
Show Gist options
  • Save ycui1/80cf3e2355cfcdd13e96ffa729ad177b to your computer and use it in GitHub Desktop.
Save ycui1/80cf3e2355cfcdd13e96ffa729ad177b to your computer and use it in GitHub Desktop.
extension String {
func convertToAttributedFromHTML() -> NSAttributedString? {
var attributedText: NSAttributedString?
let options: [NSAttributedString.DocumentReadingOptionKey: Any] = [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue]
if let data = data(using: .unicode, allowLossyConversion: true), let attrStr = try? NSAttributedString(data: data, options: options, documentAttributes: nil) {
attributedText = attrStr
}
return attributedText
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment