How to show an HTML string on a UILabel in iOS?

前端 未结 7 1929
心在旅途
心在旅途 2020-12-12 18:25

I am getting a title in HTML format as

Example

I

7条回答
  •  我在风中等你
    2020-12-12 18:45

    If Html Text is like

    var planeText = "Unseen and not Purchased!"
    //setting color to the text
    var htmlText = "" + planeText + ""  
    

    We can set the text to UILabel like this

    if let htmlData = htmlText.data(using: String.Encoding.unicode) {
        do {
             let attributedText = try NSAttributedString(data: htmlData,
                 options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], 
                 documentAttributes: nil)   
             //Setting htmltext to uilable 
             uilable.attributedText = attributedText
    
           } catch let e as NSError {
             //setting plane text to uilable cause of err
             uilable.text = planeText
             print("Couldn't translate \(htmlText): \(e.localizedDescription) ")
           }
    }
    

提交回复
热议问题