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

前端 未结 7 1920
心在旅途
心在旅途 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 = "

    etc

    " 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 = "

    etc

    " 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) ") } }

提交回复
热议问题