有什么办法可以迅速将NSAttributedString转换为HTML? [英] Is there any way to convert NSAttributedString to HTML in swift?

查看:74
本文介绍了有什么办法可以迅速将NSAttributedString转换为HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将NSAttributedString转换为HTML字符串.我一直在寻找解决方法,但是还没有结果.

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

任何想法我该怎么做?

推荐答案

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

这篇关于有什么办法可以迅速将NSAttributedString转换为HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆