SwiftUI attributed string from HTML crashes the app

前端 未结 1 1309
广开言路
广开言路 2021-01-13 23:51

I\'m trying to convert HTML formatted text into an attributed string, and insert it into a SwiftUI view.

Firstly I have a String

相关标签:
1条回答
  • 2021-01-14 00:07

    Use This:-

    struct HTMLLabel: UIViewRepresentable {
    
    let html: String
    
    func makeUIView(context: UIViewRepresentableContext<Self>) -> UILabel {
        let label = UILabel()
        DispatchQueue.main.async {
            if let attributedText = self.html.convertHtml() {
                   label.attributedText = attributedText
                }
        }
    
        return label
    }
    
    func updateUIView(_ uiView: UILabel, context: UIViewRepresentableContext<Self>) {}
    
    }
    

    NSAttributedString.DocumentType.html is Only Work with Main Thread That's why you are getting crash

    0 讨论(0)
提交回复
热议问题