For Swift 4, Swift 4.2: and Swift 5
let htmlString = """
body {
background-color : rgb(230, 230, 230);
font-family : 'Arial';
text-decoration : none;
}
A title
A paragraph
bold text
"""
let htmlData = NSString(string: htmlString).data(using: String.Encoding.unicode.rawValue)
let options = [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html]
let attributedString = try! NSAttributedString(data: htmlData!, options: options, documentAttributes: nil)
textView.attributedText = attributedString
For Swift 3:
let htmlString = """
body {
background-color : rgb(230, 230, 230);
font-family : 'Arial';
text-decoration : none;
}
A title
A paragraph
bold text
"""
let htmlData = NSString(string: htmlString).data(using: String.Encoding.unicode.rawValue)
let attributedString = try! NSAttributedString(data: htmlData!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
textView.attributedText = attributedString