swift html 转换,在Swift 4中将属性文本转换为HTML

我正在尝试将属性文本转换为

Swift 4中的HTML,以便它可以存储在Firebase / Firestore中并在不同的设备/平台上同步.我已经阅读了我在Stackoverflow上可以找到的每篇文章,包括

Convert attributed string, to, “simple” tagged html,

Convert attributed text to HTML和这篇博客文章:

http://sketchytech.blogspot.com/2016/02/swift-from-html-to-nsattributedtext-and.html.我发现Swift 4.x在各种代码示例中抛出了NSDocumentTypeDocumentAttribute和NSHTMLTextDocumentType的未解决的标识符错误(例如下面的例子). Swift 3.x中没有抛出这些错误. Xcode建议我可能意味着使用NSDocumentTypeDocumentOption但不提供修复,我不确定这是否是我想要的或如果它是如何使用它.

let myAttributes = [ NSAttributedStringKey.foregroundColor: UIColor.green ]

let attrString = NSAttributedString(string: "Hello.", attributes: myAttributes)

let x = attrString

var resultHtmlText = ""

do {

let r = NSRange(location: 0, length: x.length)

let att = [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType]

let d = try x.data(from: r, documentAttributes: att)

if let h = String(data: d, encoding: .utf8) {

resultHtmlText = h

}

}

catch {

print("utterly failed to convert to html!!! \n>\(x)

}

print(resultHtmlText)

谢谢你的帮助

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值