我使用 TextView 显示照片,一切正常,但是为什么在 iPhone 上,加上版本不显示带有照片的文字,谁知道可能是什么原因?
override func viewDidLoad() {
super.viewDidLoad()
textView.attributedText = detail?.text?.html2AttributedString
textView.attributedText.correctimage(textView: text, SV: view)
}
下载照片并处理 html 文本
extension String {
var html2AttributedString: NSAttributedString? {
do {
return try NSAttributedString(data: Data(utf8), options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil)
} catch {
print("error:", error)
return nil
}
}
var html2String: String {
return html2AttributedString?.string ?? ""
}
}
在文本中查找照片并设置它们的大小
extension NSAttributedString {
func correctimage(textView:UITextView, SV:Any){
textView.font = UIFont(name: "roboto-light", size: 19)
textView.attributedText.enumerateAttribute(NSAttributedStringKey.attachment, in: NSRange(location: 0, length: textView.attributedText.length), options: [], using: {(value,range,stop) -> Void in
if (value is NSTextAttachment) {
let attachment: NSTextAttachment? = (value as? NSTextAttachment)
if ((attachment?.image) != nil) {
attachment?.bounds.origin = CGPoint(x: (SV as AnyObject).frame.size.width - 20, y: 20)
attachment?.bounds.size = CGSize(width: (SV as AnyObject).frame.size.width - 25, height: 250)
}
}
})
}
编辑:
我的代码适用于所有 iphone 型号,但不适用于带有 plus 型号的模拟器