Is there any way to convert NSAttributedString to HTML in swift?

泄露秘密 提交于 2019-12-07 17:20:25

问题


I want to convert a NSAttributedString to HTML string. I've been searching how to solve it, but I have no results yet.

Any idea how can I do it?


回答1:


let attrStr = NSAttributedString(string: "Hello World")
let documentAttributes = [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType]
do {
    let htmlData = try attrStr.dataFromRange(NSMakeRange(0, attrStr.length), documentAttributes:documentAttributes)
    if let htmlString = String(data:htmlData, encoding:NSUTF8StringEncoding) {
        print(htmlString)
    }
}
catch {
    print("error creating HTML from Attributed String")
}

you can use this code .it's swift example



来源:https://stackoverflow.com/questions/32783875/is-there-any-way-to-convert-nsattributedstring-to-html-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!