Display HTML text in UILabel iphone

前端 未结 11 1947
借酒劲吻你
借酒劲吻你 2020-12-02 14:39

I am getting a HTML Response from a webservice Below is the HTML I am getting in response

TopicGud mrng.

\\n
11条回答
  •  抹茶落季
    2020-12-02 15:04

    Swift 4

    I would rather suggest to extend NSAttributedString with failable convenience init. String is not responsible for making NSAttributedString by it's nature.

    extension NSAttributedString {
         convenience init?(html: String) {
            guard let data = html.data(using: String.Encoding.unicode, allowLossyConversion: false) else {
                return nil
            }
            guard let attributedString = try? NSAttributedString(data: data, options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) else {
                return nil
            }
            self.init(attributedString: attributedString)
        }
    }
    label.attributedText = NSAttributedString(html: " Some bold and  Hyperlink  and so on ")
    

提交回复
热议问题