我有通过JSON获得的wordpress文章的正文(带图像的HTML),我使用UITextView`来显示它并且一切正常,除了包含HTML的图像只显示一部分。
要格式化HTML内容,我使用以下扩展名:
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)
}
}
并在UITextView中显示它,我这样做:
myTextView.attributedText = NSAttributedString(html: htmlString!)
在var htmlString
中:存储了HTML。
一切都很好,除了在加载HTML内容的图像时它不会缩放。
如何让HTML内容图像显示自动缩放?
图像的html代码类似于:
<img class="alignnone size-full wp-image-9667" src="https://.../wp-content/uploads/2016/02/image.jpg" alt="image" width="612" height="894">
可以添加CSS吗?