【发布时间】:2013-12-17 01:23:18
【问题描述】:
我为UITextView 设置了一个NSAttributed 字符串(来自HTML)。
- (void)setHtml:(NSString *)html {
NSData *htmlData = [html dataUsingEncoding:NSUTF8StringEncoding];
// Create the HTML string
NSDictionary *importParams = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};
NSError *error = nil;
self.htmlString = [[NSAttributedString alloc] initWithData:htmlData options:importParams documentAttributes:NULL error:&error];
self.editorView.attributedText = self.htmlString;
}
然后我让用户编辑他们想要的内容,然后我想再次将其转换为 HTML,所以我使用:
- (NSString *)getHTML {
NSDictionary *exportParams = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};
NSData *htmlData = [self.editorView.attributedText dataFromRange:NSMakeRange(0, self.editorView.attributedText.length) documentAttributes:exportParams error:nil];
return [[NSString alloc] initWithData:htmlData encoding:NSUTF8StringEncoding];
}
它确实返回 HTML,但这不是我想要的。一切都被赋予了一个类属性,以及它放在文档顶部的 CSS。图像和链接之类的内容甚至不包含在返回的 HTML 中,而且可能还有更多问题。
有没有更好的方法从NSAttributedString 获取 HTML?或者,有没有办法可以解析NSAttributedString 并编写自己的 HTML?
【问题讨论】:
标签: ios objective-c uitextview nsattributedstring textkit