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

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

I am getting a title in HTML format as

Example

I

相关标签:
7条回答
  • 2020-12-12 18:55

    Swift 2

    let htmlText = "<p>etc</p>"
    
    if let htmlData = htmlText.dataUsingEncoding(NSUnicodeStringEncoding) {
        do {
            someLabel.attributedText = try NSAttributedString(data: htmlData,
                options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
                documentAttributes: nil)
        } catch let e as NSError {
            print("Couldn't translate \(htmlText): \(e.localizedDescription) ")
        }
    }
    

    Swift 3

    let htmlText = "<p>etc</p>"
    
    if let htmlData = htmlText.data(using: String.Encoding.unicode) {
        do {
            let attributedText = try NSAttributedString(data: htmlData, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
        } catch let e as NSError {
            print("Couldn't translate \(htmlText): \(e.localizedDescription) ")
        }
    }
    
    0 讨论(0)
提交回复
热议问题