加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 05:30:43 所属栏目:百科 来源:网络整理
导读:我正在尝试将属性文本转换为 Swift 4中的HTML,以便它可以存储在Firebase / Firestore中并在不同的设备/平台上同步.我已经阅读了我在Stackoverflow上可以找到的每篇文章,包括 Convert attributed string,to,“simple” tagged html,Convert attributed text t
我正在尝试将属性文本转换为 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)<n")
}
print(resultHtmlText)

谢谢你的帮助

对于Swift 4.x,该行:
let att = [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType]

应该:

let att = [NSAttributedString.DocumentAttributeKey.documentType: NSAttributedString.DocumentType.html]

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读