我试图使用NSAttributedString从示例HTML中获取颜色属性。下面是我试图创建它的方式:
func toHTMLAttributedString() -> NSAttributedString? {
    if let data = self.data(using: .unicode),
       let nsAttrString = try? NSAttributedString(data: data,
                                                  options: [.documentType: NSAttributedString.DocumentType.html],
                                                  documentAttributes: nil) {
            return nsAttrString
        }
        return nil
    }下面是我尝试使用的字符串:
<p>My <b> cat </b> has <span style=\"color:#i00008B\">blue</span> eyes.</p>然而,“蓝色”这个词总是黑色的。我发现这是因为它是一个十六进制值。如何将十六进制值转换为NSAttributedString知道如何使用的内容。
发布于 2022-09-27 20:58:39
您需要删除字母i
"color:#00008B\然后是红色的正确的e.x
let str = """
<p>My <b> cat </b> has <span style=\"color:#ff0000\">blue</span> eyes.</p>
"""

https://stackoverflow.com/questions/73873600
复制相似问题