使用属性文本在UITextView中重绘NSTextAttachments [英] Redrawing NSTextAttachments in an UITextView with attributed text

查看:487
本文介绍了使用属性文本在UITextView中重绘NSTextAttachments的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个覆盖的NSTextAttachment子类
attachmentBoundsForTextContainer:proposedLineFragment:glyphPosition:characterIndex:
imageForBounds:textContainer:characterIndex :

I have a NSTextAttachment subclass with overridden
attachmentBoundsForTextContainer:proposedLineFragment:glyphPosition:characterIndex:
imageForBounds:textContainer:characterIndex:.

我需要在某些时候重新绘制附件。在UITextView上调用 setNeedsDisplay 不起作用。

I need to redraw the attachment(s) at some point. Calling setNeedsDisplay on the UITextView doesn't work.

任何想法?我想避免重新创建附件和/或属性字符串。

Any ideas? I'd like to avoid recreating attachments and/or the attributed string.

推荐答案

你会想要使用 textView.layoutManager 的方法。


  • invalidateDisplayCharacterRange:

    • imageForBounds:textContainer:characterIndex:将再次被调用。

    • attachmentBoundsForTextContainer:[...]索引:不再再次调用。

    • 如果图片已使用另一个相同尺寸更改,那就太好了。

    • invalidateDisplayCharacterRange:
      • imageForBounds:textContainer:characterIndex: will be called again.
      • attachmentBoundsForTextContainer:[...]Index: will not be called again.
      • Good if the image has been changed with another one of the same size.

      • imageForBounds:textContainer:characterIndex :将再次被调用。

      • attachmentBoundsForTextContainer:[...] index:将再次被调用。

      • 如果图像已被另一个不同 strong> size。

      • imageForBounds:textContainer:characterIndex: will be called again.
      • attachmentBoundsForTextContainer:[...]Index: will be called again.
      • Good if the image has been changed with another one of a different size.

      如果您只想更新单个附件,可能会发现我写的这个帮助方法很有用:

      If you just want to update a single attachment, you may find this helper method I wrote helpful:

      - (NSRange)rangeOfAttachment:(NSTextAttachment *)attachment {
          __block NSRange ret;
          [self.textStorage enumerateAttribute:NSAttachmentAttributeName
                                       inRange:NSMakeRange(0, self.textStorage.length)
                                       options:0
                                    usingBlock:^(id value, NSRange range, BOOL *stop) {
                                        if (attachment == value) {
                                            ret = range;
                                            *stop = YES;
                                        }
                                    }];
          return ret;
      }
      

      您可以传递结果 NSRange 到这些无效方法中的任何一个的第一个参数。对于第二种方法的 actualCharacterRange:参数,我一直在传递 NULL 而没有任何问题。

      You can pass the resulting NSRange of this method to the first argument of either of those invalidate methods. For the actualCharacterRange: argument of the second method, I've been passing in NULL without any problems.

      这篇关于使用属性文本在UITextView中重绘NSTextAttachments的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆