【问题标题】:How can i show gif in UITextView with NSTextAttachment?如何使用 NSTextAttachment 在 UITextView 中显示 gif?
【发布时间】:2018-10-19 05:22:26
【问题描述】:

我使用 SD_WebImage 来显示我的 gif,如下所示:

代码:

    UIImage *gifImg = [UIImage sd_animatedGIFWithData:[NSData dataWithContentsOfFile:imagePath]];


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

    atc.image = image;

    NSAttributedString *textAttachmentString = [NSAttributedString attributedStringWithAttachment:atc];

 [emojiText replaceCharactersInRange:range withAttributedString:textAttachmentString];

MyTextView.attributedString = emojiText;

结果: 我的 UITextView 上的图像显示成功,但它不播放,它只是静态的

为什么?我怎样才能激活这个 gif 图像?

(顺便说一句:由于某些原因我不能在我的项目中使用 YYkit,所以我必须使用 UITextView 或 UILabel) 我知道 imageView 可以播放带有图像数组的动画。但我没有找到在 textView 中显示 imageView 的正确方法。

帮帮我~????

【问题讨论】:

标签: ios uitextview gif nstextattachment


【解决方案1】:

我终于解决了他们的问题

我使用空附件替换我的 gif-image_script

然后我找到这个附件,创建一个 yyanimatedimage 并将其放在我的 textview 上

[mAttributedString enumerateAttribute:NSAttachmentAttributeName inRange:NSMakeRange(0, mAttributedString.length) options:0 usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
    if ([value isKindOfClass:[NSTextAttachment class]]) {
        NSTextAttachment * attachment = value;
        if ([attachment.tagExtra isEqualToString:@"gif"]) {
            label.selectedRange = range;
            CGRect rect = [label firstRectForRange:label.selectedTextRange];
            rect.origin.y += (rect.size.height - 20);
            rect.size = CGSizeMake(20, 20);

            YYAnimatedImageView *imageView = [[YYAnimatedImageView alloc] initWithFrame:rect];
            [label addSubview:imageView];
            imageView.image = [YYImage imageNamed:attachment.tagName];
        }
    }
}];
label.selectedRange = NSMakeRange(0, 0);

【讨论】:

  • 嗨,我看到你将 uiimageview 子视图添加到 gif 的位置,我认为这不是一个好的解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多