Skip to content

Instantly share code, notes, and snippets.

@marciogranzotto
Created October 3, 2016 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marciogranzotto/dc858564f182ec3c8a864a24584f8db7 to your computer and use it in GitHub Desktop.
Save marciogranzotto/dc858564f182ec3c8a864a24584f8db7 to your computer and use it in GitHub Desktop.
// From http://stackoverflow.com/a/39344394/3984316
import UIKit
extension String {
init(htmlEncodedString: String) {
self.init()
guard let encodedData = htmlEncodedString.data(using: .utf8) else {
self = htmlEncodedString
return
}
let attributedOptions: [String : Any] = [
NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue
]
do {
let attributedString = try NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil)
self = attributedString.string
} catch {
print("Error: \(error)")
self = htmlEncodedString
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment