我有一些HTML数据,其中包含标题,段落,图像和列表标签。
是否有一个显示该数据的方式UITextView还是UILabel?
UITextView
UILabel
对于Swift 4:
extension String { var htmlToAttributedString: NSAttributedString? { guard let data = data(using: .utf8) else { return NSAttributedString() } do { return try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding:String.Encoding.utf8.rawValue], documentAttributes: nil) } catch { return NSAttributedString() } } var htmlToString: String { return htmlToAttributedString?.string ?? "" } }
然后,每当您要将HTML文本放入UITextView时,请使用:
textView.attributedText = htmlText.htmlToAttributedString