gpt4 book ai didi

ios - 也从 UITextView 获取属性化的 html 字符串

转载 作者:行者123 更新时间:2023-11-28 19:59:04 25 4
gpt4 key购买 nike

我使用下面给出的代码在 TextView 中添加属性文本

NSString *string=[NSString stringWithFormat:@"<img src=\"%@\">",sticker.image_url];
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[string dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
self.textView.attributedText=attrStr;

TextView 有文本 testing <img src="www.google.com">

以上代码在 TextView 中显示图像。但现在的问题是我想得到字符串,需要做更多的工作。这就是为什么当我尝试使用下面给出的代码从 TextView 中获取属性字符串时,它只提供文本而不是我在 TextView 中添加的 html 代码。

[self.textview.attributedString string]
return text = testing

请帮忙。实际上我想在文本中使用 html 显示一些图像,然后需要将该代码上传到服务器。

我无法理解该 html 文本从哪里消失了,它应该保留在文本中的某个位置,但我无法取回该文本提前致谢

最佳答案

您不能使用 self.textView.text 因为您将拥有的只是纯文本,同时您不能使用 self.textView.attributedText 因为你将拥有的只是 NSAttributedString 对象,它是你使用

构建的内容的表示
[[NSAttributedString alloc] initWithData:yourData options:yourOptions documentAttributes:nil error:nil];`

NSAttributedString 并不是为 html 表示构建的,您使用的只是一种从 html 开始轻松创建 NSAttributedString 的方法,但是这两种表示之间没有完全支持。

您需要稍微复杂一些的东西来完成这件事,您可能需要检查这个 repo https://github.com/IdeasOnCanvas/Ashton

特别是我指的是类别 NSAttributedString+Ashton.h 有这个很酷的方法

// Attributed String with CT Attributes
- (NSString *)mn_HTMLRepresentationFromCoreTextAttributes;

我不确定实现支持的 html 标签,但无论如何这是一个开始。

如果您的意图只是保存您设置的 html 供以后使用,您可以子类化 NSAttributedString,添加一个新属性 NSString *html,您将设置覆盖initWithData:options:documentAttributes:error: 显然它的扩展性不好,但在短期内它是一个“肮脏”的解决方案。

关于ios - 也从 UITextView 获取属性化的 html 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24970597/

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