【发布时间】:2017-06-22 23:41:15
【问题描述】:
我收到来自API 的文本,其中包含html tags 和pictures。如何显示它,以及我需要显示的位置,我的意思是UILabel 或WebView?
我尝试使用NSAttributedString,但不是我需要的。
平台 iOS 9、Swift 3
【问题讨论】:
我收到来自API 的文本,其中包含html tags 和pictures。如何显示它,以及我需要显示的位置,我的意思是UILabel 或WebView?
我尝试使用NSAttributedString,但不是我需要的。
平台 iOS 9、Swift 3
【问题讨论】:
我找到方法了。
我在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()
【讨论】:
你可以在webView中加载html标签
func loadHtmlCode() {
let htmlCode = "<html><head><title>Wonderful web</title></head> <body><p>wonderful web. loading html code in <strong>UIWebView</strong></></body>"
webView.loadHTMLString(htmlCode, baseURL: nil)
}
【讨论】: