Tuesday, July 2, 2024
HomeiOS Developmentios - SwiftUI : Font of Html in a UITextView

ios – SwiftUI : Font of Html in a UITextView


Please do not make this duplicate, as a result of I’ve been looking for the reply and I did not discover it.

I’ve a String from API that incorporates HTML.
On this HTML, there’s textual content with the font and it is type, a hypertext or many, and pictures.

I did present all of them in a UITextView however the issue is I did not discover find out how to get the textual content with the identical type and the font within the HTML.

Here is my code:

struct HTMLFormattedText: UIViewRepresentable {

   let textual content: String
   personal let textView = UITextView()
   
   init(_ content material: String) {
        self.textual content = content material
    }
    
    func makeUIView(context: UIViewRepresentableContext<Self>) -> UITextView {
        textView.dataDetectorTypes = UIDataDetectorTypes.all
        textView.textColor = .white
        textView.backgroundColor = .clear
        textView.isSelectable = true
        textView.isEditable = false
        textView.isUserInteractionEnabled = true
        textView.translatesAutoresizingMaskIntoConstraints = false
        textView.linkTextAttributes = [.foregroundColor: UIColor.white]
        textView.isScrollEnabled = false
        self.updateUIView(textView, context: context)
        return textView
    }


    personal func converHTML(textual content: String) -> NSAttributedString?{
        guard let knowledge = textual content.knowledge(utilizing: String.Encoding.utf16, allowLossyConversion: false) else {
            return nil
        }
        
        if let attributedString = attempt? NSAttributedString(knowledge: knowledge, choices: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) {
            textView.textColor = .white
            return attributedString
        } else{
            return nil
        }
    }

}



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments