malcommac / SwiftRichString

👩‍🎨 Elegant Attributed String composition in Swift sauce
MIT License
3.1k stars 211 forks source link

HTML string not render #113

Open TrungKhanhNguyen opened 4 years ago

TrungKhanhNguyen commented 4 years ago

I want display some HTML String for UITextView but its only display raw text. I dont know why

Screenshot at Apr 09 16-01-39 Screenshot at Apr 09 16-03-02

Can you help me? Thanks I'm using xcode 11.4 & catalina 10.15.4

petkrein commented 4 years ago
extension String {
    func convertHtml() -> AttributedString {
        guard let data = data(using: .utf8) else { return AttributedString() }

        if let attributedString = try? AttributedString(data: data, options: [.documentType: AttributedString.DocumentType.html,
        .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil) {
            return attributedString
        } else {
            return AttributedString()
        }
    }
}

example: self.textView.attributedText = tempText.convertHtml().set(style: style)

rizwan95 commented 4 years ago

@petkrein Why would you want to convert HTML to NSAttributedString using AttributedString's method? Doesn't the library do automatically?

altagir commented 4 years ago

using convertHtml is extremely slow I have a page with hundred html titles, and loading time went from 2s to 100s I was hoping this library was doing just that

screenworker commented 4 years ago

@altagir altagir:

using convertHtml is extremely slow I have a page with hundred html titles, and loading time went from 2s to 100s I was hoping this library was doing just that

As @TrungKhanhNguyen reported, the library does not appear to support this. I ran into the same problem and helped myself with the extension. And yes, it's very slow.

screenworker commented 4 years ago

@rizwan9 because of: https://github.com/malcommac/SwiftRichString/blob/a24bdce43b8d77eb845d0a3c605faf88e411eb8b/Sources/SwiftRichString/Style/StyleProtocol.swift#L33