【问题标题】:How can I do NSAttributed words bold in Swift?如何在 Swift 中将 NSAttributed 单词加粗?
【发布时间】:2020-07-25 21:29:50
【问题描述】:

我的文字风格如下:

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

我想用“b 和 /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 ?? ""
    }
}

我该怎么做?

【问题讨论】:

    标签: html swift xcode encode


    【解决方案1】:

    数据类型 String 没有任何格式化文本样式或字体的元数据。然而,NSAttributedString 确实包含此类信息。您想要做的是使用 NSAttributedString 并将其分配给 TextView 或您想要显示的任何内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-28
      • 2017-06-12
      • 2012-02-21
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 2019-12-30
      相关资源
      最近更新 更多