美文网首页
swift html字符串转富文本

swift html字符串转富文本

作者: 轻轻语焉 | 来源:发表于2022-01-06 20:35 被阅读0次

func attributedString(font: UIFont = UIFont.systemFont(ofSize: 14)) -> NSMutableAttributedString? {

        guard let data = self.data(using: String.Encoding.utf8,

            allowLossyConversion: false) else { return nil }

        let options: [NSAttributedString.DocumentReadingOptionKey : Any] = [

            NSAttributedString.DocumentReadingOptionKey.characterEncoding : String.Encoding.utf8.rawValue,

            NSAttributedString.DocumentReadingOptionKey.documentType : NSAttributedString.DocumentType.html,

        ]

        let htmlString = try? NSMutableAttributedString(data: data, options: options, documentAttributes: nil)

        // Removing this line makes the bug reappear

        htmlString?.addAttribute(NSAttributedString.Key.backgroundColor, value: UIColor.clear, range: NSMakeRange(0, 1))

        htmlString?.addAttributes([NSAttributedString.Key.font : font], range: NSMakeRange(0, htmlString?.length ?? 0 ))

        return htmlString

    }

相关文章

网友评论

      本文标题:swift html字符串转富文本

      本文链接:https://www.haomeiwen.com/subject/bgxdcrtx.html