Попытка преобразовать HTML в AttributedString в UIViewRepresentable в SwiftUI - PullRequest
2 голосов
/ 07 октября 2019

Я пытаюсь создать это представление UIViewRepresentable с помощью UILabel, чтобы я мог представить NSAttributedString с помощью SwiftUI.

Я пытался создать функцию, которая преобразует это, но это не работает.

extension Data {
    var html2AttributedString: NSAttributedString? {
        do {
            return try NSAttributedString(data: self, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil)
        } catch {
            print("error:", error)
            return  nil
        }
    }
    var html2String: String {
        return html2AttributedString?.string ?? ""
    }
}

extension String {
    var html2AttributedString: NSAttributedString? {
        return Data(utf8).html2AttributedString
    }
    var html2String: String {
        return html2AttributedString?.string ?? ""
    }

    var noHTML: String {
        let str = self.replacingOccurrences(of: "<[^>]+>", with: "", options: .regularExpression, range: nil)
        return str
    }
}

Это то, что я получаю, когда у меня есть список, идущий через записи, предоставленные JSON.

Как только я не использую html2AttributedString, все прекрасно работает (отображается текст HTML).

Кажется, я не могу заставить работать NSAttributedString(data: self, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil) часть.

=== AttributeGraph: cycle detected through attribute 38 ===  
=== AttributeGraph: cycle detected through attribute 38 ===  
=== AttributeGraph: cycle detected through attribute 41 ===  
=== AttributeGraph: cycle detected through attribute 19 ===  
=== AttributeGraph: cycle detected through attribute 33 ===  
=== AttributeGraph: cycle detected through attribute 38 ===  
=== AttributeGraph: cycle detected through attribute 19 ===
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...