将属性文本转换为HTML

时间:2014-11-28 06:15:12

标签: ios nsattributedstring

在我的情况下,我必须将NSHTMLTextDocumentType文档转换为NSMutableAttributedString我在下面使用的代码段

[[NSMutableAttributedString alloc] initWithData:[text dataUsingEncoding:NSUTF8StringEncoding]
                                                   options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                                                             NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)}
                                        documentAttributes:nil error:nil];

当我记录我的text时,我会在下面找到附加屏幕截图

<font color="#ff66b2">dasdasd asd asd asd sa asda&nbsp;</font>
<font color="#00feff">asdasdas &nbsp;asd </font>
<font color="#0000ff">sa asd asdasdsad</font> 

enter image description here

因为它是UITextView我还需要在iPhone中提供编辑支持。那么如何将NSMutableAttributedString转换为记录的字符串?

任何帮助将不胜感激..

1 个答案:

答案 0 :(得分:0)

部分同意的答案

var htmlEncoding: String? {
do {

    let attrubutedString = NSAttributedString(string: self)

    let htmlData = try attrubutedString.data(from:NSMakeRange(0, attrubutedString.length), documentAttributes:[.documentType: NSAttributedString.DocumentType.html]);

    return String.init(data: htmlData, encoding: String.Encoding.utf8)

} catch {

    return nil

}

}