How to parse the Array from local JSON file which has html formatted text (description) and show it in SwiftUI detail view or WKWebView?

Solution 1:

to "...show exactly html formatted text with html properties applied...", try this function that uses AttributedString: (note, this is particular for this text type, Gujarati?)

func attributedString(from str: String) -> AttributedString {
    if let theData = str.data(using: .utf16) {
        do {
            let theString = try NSAttributedString(data: theData, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil)
            return AttributedString(theString)
        } catch {  print("\(error)")  }
    }
    return AttributedString(str)
}

and replace Text(item.title) with Text(attributedString(from: item.title))

You can also look at this SO post/anwser: Convert html links inside text to clickable links - SwiftUI