3

我正在尝试在要显示在 textView 上的属性字符串中获取自定义视图。我可以添加带有 NSTextAttachment 的图像,但这不是我想要的。我有一个自定义视图,它支持我想在文本之间显示的 Gif 和动画 PNG。

例子:

文本 文本 文本 [customView] 文本 [customView] 文本。<- 在文本视图中,最好在属性字符串中

我希望得到一些关于我应该在哪里专门搜索的指导。到目前为止,我已经看到了相关的问题......

  • 子类 NSTextAttachment:如何继承 NSTextAttachment?
  • 使用 NSTextAttachmentContainer..?
  • NSTextAttachmentCell - 仅 OSX
  • 在文本视图中进行操作
4

1 回答 1

0

首先,使用NSAttributedStringorNSMutableAttributedString在子视图中显示您的 RichText(例如 UITextView/UILabel)

然后,用于NSTextAttachment替换文本中的图像脚本。

NSString *egText = @"hello [this_is_an_img_script]";

NSMutableAttributedString * destStr = [[NSMutableAttributedString alloc] initWithString:egText];

NSTextAttachment *attachment = [[NSTextAttachment alloc] initWithData:nil ofType:nil];

attachment.image = [UIImage imageNamed:[this_is_an_img_script]];

NSAttributedString *textAttachmentString = [NSAttributedString attributedStringWithAttachment:attachment]; //make your image to an attributedString

[destStr replaceCharactersInRange:range withAttributedString:textAttachmentString];

at last: [YourLabel(or YourTextView) setAttributedString:destStr];

顺便说一句:如果您将YYkit用于 RichText,则不能使用YYAttachMentString替换NSAttachMentString,这些是不同的东西,UITextView(UILabel)无法加载YYAttachMentString. 我正在寻找某种方式来显示我的 gif UITextView(因为YYKit无法加载和预览带有 url 的网络图像,所以YYKit总是显示空的应该是 a netImage,cripes!)

于 2018-05-09T06:55:26.547 回答