Created
October 3, 2016 14:38
-
-
Save marciogranzotto/dc858564f182ec3c8a864a24584f8db7 to your computer and use it in GitHub Desktop.
This file contains 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
// 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