如何在Swift中将NSAttributed单词加粗?

时间:2020-04-13 10:29:08

标签: html swift xcode encode

我具有以下文字样式:

let text = "Hello <b>World</b>"

我想用“ b and / b”参数使“ World”加粗。以下代码对我没有帮助:

    var htmlToAttributedString: NSAttributedString? {
        guard let data = data(using: .utf8) else { return NSAttributedString() }
        do {
            return try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding:String.Encoding.utf8.rawValue], documentAttributes: nil)
        } catch {
            return NSAttributedString()
        }
    }
    var htmlToString: String {
        return htmlToAttributedString?.string ?? ""
    }
}

我该怎么做?

1 个答案:

答案 0 :(得分:0)

数据类型字符串没有任何格式化文本样式或字体的元数据。但是,NSAttributedString确实包含此类信息。您想要做的就是使用NSAttributedString并将其分配给TextView或您想要与之一起显示的任何对象。

相关问题