gpt4 book ai didi

iOS NSAttributedString 到 HTML

转载 作者:IT王子 更新时间:2023-10-29 07:59:26 25 4
gpt4 key购买 nike

我为 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?

最佳答案

或许您可以查看该存储库: https://github.com/IdeasOnCanvas/Ashton

有两个有趣的类:

AshtonHTMLReader.h

- (NSAttributedString *)attributedStringFromHTMLString:(NSString *)htmlString;

作者:

AshtonHTMLWriter.h

- (NSString *)HTMLStringFromAttributedString:(NSAttributedString *)input;

生成的 html 不是很好,但是如果您尝试在 uiwebview 中显示它,看起来不错。

图像的简单想法:用 base64 编码并直接将其放在具有正确框架的 < img > 标签中。

虽然丑陋但有效 => 几个月前我已经使用这个过程创建和编辑了一些 html 文件

关于iOS NSAttributedString 到 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20309606/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com