首页 > 解决方案 > NSAttributedString 添加自定义属性会丢失 html 标签

问题描述

假设我有一个来自 web 服务的字符串,它返回基本的 html。我想将它插入到具有自定义字体的 UITextView 中,但是在设置之后我丢失了例如标签。

let htmlString = "<b>This is a title</b><br/>Another line."

let attributedString = try? NSMutableAttributedString(
        data: htmlString.data(using: String.Encoding.utf8) ?? Data(),
        options: [
            .documentType : NSAttributedString.DocumentType.html,
            .characterEncoding : String.Encoding.utf8.rawValue
        ],
        documentAttributes: nil
    )

尽管字体超级小而不是我的字体,但这会呈现“好的”。设置属性后...

let attributes = [ NSAttributedStringKey.font: Fonts.raleway(ofSize: 17) ]
attributedString?.addAttributes(attributes, range: NSMakeRange(0, attributedString?.string.count ?? 0))

我失去了大胆。

有任何想法吗?

标签: iosswiftnsattributedstring

解决方案


推荐阅读