Convert attributed text to HTML

空扰寡人 提交于 2019-12-11 02:03:43

问题


In my case I have to convert NSHTMLTextDocumentType document to NSMutableAttributedString for which I use below snippet

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

When I log my text I get below for attached screenshot

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

Since it's a UITextView I need to give edit support also in iPhone. So how can I convert NSMutableAttributedString into the formate of string logged?

Any help would be greatly appreciated..


回答1:


Partially agreed answer

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

}

}


来源:https://stackoverflow.com/questions/27182935/convert-attributed-text-to-html

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