How to display html formatted text in ios label

前端 未结 7 1384
逝去的感伤
逝去的感伤 2020-12-24 00:55

I would like to display html formatted text on a UILabel in IOS.

In Android, it has api like this .setText(Html.fromHtml(somestri

7条回答
  •  悲哀的现实
    2020-12-24 01:48

    Try this:
    
    let label : UILable! = String.stringFromHTML("html String")
    
    func stringFromHTML( string: String?) -> String
        {
            do{
                let str = try NSAttributedString(data:string!.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true
                    )!, options:[NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: NSNumber(unsignedLong: NSUTF8StringEncoding)], documentAttributes: nil)
                return str.string
            } catch
            {
                print("html error\n",error)
            }
            return ""
        }
    Hope its helpful.
    

提交回复
热议问题