在UILabel中使用HTML标签

时间:2019-06-08 15:09:54

标签: ios json swift

我有一个带有一些HTML标记的JSON。

"activities": [
        {
            "message": "<strong>Henrik</strong> didn't resist a guilty pleasure at <strong>Starbucks</strong>."

        },
        {
            "message": "<strong>Johan</strong> made a roundup."
        },
        {
            "message": "<strong>You</strong> didn't resist a guilty pleasure at <strong>Starbucks</strong>."
        },
        {
            "message": "<strong>Daniel</strong> made a roundup."
        },
]

我想使用消息字段显示在UILabel上,例如:

cell.descriptionLabel.text = activity.message

我该怎么做?

更新,我用过:

let messageString = activity.message
        let htmlData = NSString(string: messageString).data(using: String.Encoding.utf8.rawValue)
        let options = [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html]
        let attributedString = try! NSAttributedString(data: htmlData!,
                                                       options: options,
                                                       documentAttributes: nil)
        cell.descriptionLabel.attributedText = attributedString

但是现在UILabel都在Times New Roman中了,不知道为什么。如何将其更改回默认字体?

0 个答案:

没有答案