【发布时间】:2019-12-11 08:28:07
【问题描述】:
所以我试图让下面的代码工作
return try NSAttributedString(data: data, attributes:[ NSFontAttributeName: UIFont(name: "Chalkduster", size: 18.0) ], options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding:String.Encoding.utf8.rawValue], documentAttributes: nil)
我也尝试了以下方法,但失败了
extension String {
var htmlToAttributedString: NSAttributedString? {
guard let data = data(using: .utf8) else { return NSAttributedString() }
do {
let font = UIFont.systemFont(ofSize: 72)
let attributes = [NSAttributedString.Key.font: font]
return try NSAttributedString(data: data, attributes: attributes, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding:String.Encoding.utf8.rawValue], documentAttributes: nil)
} catch {
return NSAttributedString()
}
}
var htmlToString: String {
return htmlToAttributedString?.string ?? ""
}
}
【问题讨论】:
-
您首先将 HTML 代码转换为 NSAttributedString,然后添加字体,或者在此之前添加 HTML 标记。
-
@Larme html 只有
标签和 标签而已
标签: ios swift fonts nsattributedstring