问题
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