Skip to content

Instantly share code, notes, and snippets.

@masamichiueta
Last active December 19, 2016 09:40
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 masamichiueta/67d21931ffa504514adfe7b11a2c8b32 to your computer and use it in GitHub Desktop.
Save masamichiueta/67d21931ffa504514adfe7b11a2c8b32 to your computer and use it in GitHub Desktop.
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
do {
let htmlText: String =
"<html><head></head><body><h1>Hello World</h1><a href=\"https://www.apple.com\">link</a><p>This is a sample text</p><pre>This is also sample pre text</pre></body></html>"
let encodedData = htmlText.data(using: String.Encoding.utf8)!
let attributedOptions : [String : Any] = [
NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType as Any,
NSCharacterEncodingDocumentAttribute: NSNumber(value: String.Encoding.utf8.rawValue) as Any,
]
let attributedString = try NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil)
self.textView.attributedText = attributedString
} catch {
fatalError("Unhandled error: \(error)")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment