使用属性文本在UITextView中重绘NSTextAttachments [英] Redrawing NSTextAttachments in an UITextView with attributed text
问题描述
我有一个覆盖的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 $ c这个方法的$ c>到这些
无效
方法中的任何一个的第一个参数。对于第二种方法的actualCharacterRange:
参数,我一直在传递NULL
而没有任何问题。You can pass the resulting
NSRange
of this method to the first argument of either of thoseinvalidate
methods. For theactualCharacterRange:
argument of the second method, I've been passing inNULL
without any problems.这篇关于使用属性文本在UITextView中重绘NSTextAttachments的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
-