Aug 9, 2018

Xcode - Rendering HTML code to your UILabel View

Im have been search on the net but most I see was still on Objective-c Language. Here what I discovered solution.
let htmlText = "<ul><li><b>Heloo</b></li><li>World</li></ul><hr>
<ol><li>Im <b>Heloo</b></li><li>World</li></ol>"
if let htmlData = htmlText.data(using: String.Encoding.unicode) {
  do {

    let attributedText = try NSAttributedString(data: htmlData,
    options: [
NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html
],
    documentAttributes: nil)

    //Setting htmltext to uilable
    detailLabel.attributedText = attributedText

  } catch let e as NSError {
    //setting plane text to uilable cause of err
    detailLabel.text = htmlText
    print("Couldn't translate \(htmlText): \(e.localizedDescription) ")
  }
}

Update:
* Some reported that it will affect the scroll ability of table view. I never tested yet since I didn't work on tableview for this purpose.