如何在swift 3 ios中使用html标签和图像显示内容文本

Zha*_*rik 0 html webview ios swift

我得到API包含html tags和的文本pictures.如何显示它,在这里我需要显示,我的意思UILabel还是WebView?我尝试着NSAttributedString,但不是我需要的.

平台iOS 9,Swift 3

Zha*_*rik 5

我找到了方法.我做的Info.plist

应用程序传输安全设置 - >允许任意加载 - >是

图像正在显示.

和代码:

DispatchQueue.main.async {
    let attrStr = try! NSAttributedString(
                data: (self.detailLabelText?.data(using: String.Encoding.unicode, allowLossyConversion: true)!)!,
                options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
                documentAttributes: nil)

   self.detailLabel.attributedText = attrStr
   self.detailLabel.textAlignment = NSTextAlignment.justified
   self.detailLabel.contentMode = .scaleToFill
   self.detailLabel.font = UIFont(name: "PTSans-Narrow", size: 18.0)
}

detailLabel.adjustsFontSizeToFitWidth = true
detailLabel.sizeToFit()
Run Code Online (Sandbox Code Playgroud)