问题
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 </font>
<font color="#00feff">asdasdas 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