HTML Format in UITextView

The problem there is that you have to change the Character Encoding options from NSUnicodeStringEncoding to NSUTF8StringEncoding to load your of your html the proper way. I think you should create a string extension read-only computed property to convert your html code to attributed string:

Xcode 8.3.1 • Swift 3.1

extension Data {
    var attributedString: NSAttributedString? {
        do {
            return try NSAttributedString(data: self, options:[NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue], documentAttributes: nil)
        } catch {
            print(error)
        }
        return nil
    }
}
extension String {
    var data: Data {
        return Data(utf8)
    }
}

let htmlStringCode = "F&uuml;r mehr Informationen klicken sie <a href=\"http://www.samplelink.com/subpage.php?id=8\">here</a>"

htmlStringCode.data.attributedString?.string ?? ""  // "Für mehr Informationen klicken sie here"

in your case

yourTextView.attributedText = htmlStringCode.data.attributedString

Leave a Comment