作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有一个 NSTextAttachment 子类,它被覆盖了
attachmentBoundsForTextContainer:proposedLineFragment:glyphPosition:characterIndex:
imageForBounds:textContainer:characterIndex:
。
我需要在某个时候重新绘制附件。在 UITextView 上调用 setNeedsDisplay
无效。
有什么想法吗?我想避免重新创建附件和/或属性字符串。
最佳答案
您需要使用 textView.layoutManager
的方法之一。
invalidateDisplayCharacterRange:
imageForBounds:textContainer:characterIndex:
将再次调用。attachmentBoundsForTextContainer:[...]Index:
将不会再次调用。image
已更改为另一张相同 大小,则很好。invalidateLayoutForCharacterRange:actualCharacterRange:
imageForBounds:textContainer:characterIndex:
将再次调用。attachmentBoundsForTextContainer:[...]Index:
将再次调用。image
已更改为另一张不同 大小,则很好。如果您只想更新单个附件,您可能会发现我写的这个帮助方法很有用:
- (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
传递给那些 invalidate
方法中的任何一个的第一个参数。对于第二种方法的 actualCharacterRange:
参数,我一直在传递 NULL
没有任何问题。
关于ios - 使用属性文本在 UITextView 中重绘 NSTextAttachments,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24509660/
我是一名优秀的程序员,十分优秀!