首页 > 解决方案 > 在 UILabel 中使用 HTML 标签

问题描述

我有这个 JSON,里面有一些 HTML 标签。

"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 的,不知道为什么。如何将其更改回默认字体?

标签: iosjsonswift

解决方案


推荐阅读