Ich benutze TextView, um ein Foto anzuzeigen, alles funktioniert gut, aber warum, auf dem iPhone, plus die Version zeigt keinen Text mit Fotos, wer weiß, was könnte der Grund sein?TextView zeigt keinen Text an
override func viewDidLoad() {
super.viewDidLoad()
textView.attributedText = detail?.text?.html2AttributedString
textView.attributedText.correctimage(textView: text, SV: view)
}
Downloads Fotos und verarbeitet HTML-Text
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 ?? ""
}
}
Fotos im Text finden und legen Sie sie die Größe
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)
}
}
})
}
EDIT:
mein Code funktioniert alles auf Iphone-Modelle, aber nicht auf dem Emulator mit Plus-Modell s
Vielleicht hilfreiche Links: https://stackoverflow.com/a/17744938/3883492; https://stackoverflow.com/a/46184150/3883492. Sind Sie sicher, dass der Text nicht zu lang ist und textView ihn nicht rendern konnte? –
@ dvp.petrov auf iphone5,6,7 alle angezeigt – Vasya2014