Saturday, November 7, 2026
HomeiOS Developmentios - Picture loading difficulty from HTML in UITextView

ios – Picture loading difficulty from HTML in UITextView

[ad_1]

I am engaged on undertaking the place requirement is to point out html file contained in the UITextView. there are some picture in HTML file as effectively. HTML is loading appropriately in UITextView however picture is damaged whereas loading. as a substitute of precise picture, folder/file icon is seen inside UITextView. right here is HTML I’m loading in UITextView –

let htmlString = "<html><physique><h1>That is the title</h1><p>That is the primary paragraph.</p><img src="https://miro.medium.com/max/9216/1*QzxcfBpKn5oNM09-vxG_Tw.jpeg" width="360" top="240"><p>That is the second paragraph.</p><p>That is the third paragraph.</p><p>That is the fourth paragraph.</p><p>That is the final paragraph.</p></physique></html>"

right here is code to transform the HTMl string to NSAttributedString–

extension String {
    func convertToAttributedFromHTML() -> NSAttributedString? {
        var attributedText: NSAttributedString?
        let choices: [NSAttributedString.DocumentReadingOptionKey: Any] = [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue]
        if let information = information(utilizing: .unicode, allowLossyConversion: true), let attrStr = attempt? NSAttributedString(information: information, choices: choices, documentAttributes: nil) {
            attributedText = attrStr
        }
        return attributedText
    }
}

right here I’m setting the attribute String to textView-

label.attributedText = htmlString.convertToAttributedFromHTML()

Lastly whereas rendering the above attribute String on UITextView ,damaged picture is seen in connected screenshot-
you can see folder icon is visible instead of actual image

Please let me know what I’m lacking right here so picture could be displayed correctly

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments