开发者社区> 问答> 正文

NSAttributedString设置字体大小-swift

所以我试着让下面的代码起作用

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 ?? ""
    }
}

展开
收起
游客5akardh5cojhg 2019-12-11 21:39:22 1212 0
1 条回答
写回答
取消 提交回答
  • 您要将html转换为NSAttributedString吗?可以将样式附加到字符串源。

    2019-12-11 21:39:45
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关课程

更多

相关电子书

更多
OpenStack Swift 海量小文件优化之路 立即下载
From Java/Android to Swift iOS 立即下载
Swift在Airbnb的应用实践 立即下载