optonaut / ActiveLabel.swift

UILabel drop-in replacement supporting Hashtags (#), Mentions (@) and URLs (http://) written in Swift
MIT License
4.47k stars 685 forks source link

HTML tags not working properly #410

Open zubingala opened 2 years ago

zubingala commented 2 years ago
func htmlAttributed(size: CGFloat) -> NSAttributedString? {
            do {
                let htmlCSSString = "<style>" +
                    "html *" +
                    "{" +
                    "font-size: \(size)pt !important;" +
                "}</style> \(self)"

                guard let data = htmlCSSString.data(using: String.Encoding.utf8) else {
                    return nil
                }

                return try NSAttributedString(data: data,
                                              options: [.documentType: NSAttributedString.DocumentType.html,
                                                        .characterEncoding: String.Encoding.utf8.rawValue],
                                              documentAttributes: nil)
            } catch {
                print("error: ", error)
                return nil
            }
        }

let text = "<b> Bold</b><i> Italic</i> <u>Underline</u>"
 self.activeLabel.attributedText = text.htmlAttributed(size: 15)

Output Bold Italic Underline

Its returning everything in bold instead of desired output.

zubingala commented 2 years ago

@schickling, @maziyarpanahi , @polqf please look into this.