我如何知道附件属性何时添加到我的 NSTextView 中?
由于我的应用程序中某些 NSTextView 附件的语义,我想知道它们何时从我的文本存储中插入或删除。
我的 NSTextView 子类实现了 shouldChangeTextInRange:replacementString: 方法,该方法使我可以轻松查看附件何时将被替换(我可以在指定范围内搜索文本存储)。
因为替换字符串只是一个 NSString 而不是 NSAttributedString,所以我无法通过此方法查看是否插入了附件。文档甚至指出,如果“仅编辑属性”,则字符串可能为零。
所以问题是,插入附件时查看的最佳覆盖点是什么?或者也许同样有用:修改属性时查看的最佳覆盖点是什么?
更新:我上面说过我无法知道附件是否被插入。有人向我指出,我可以看出涉及“an”附件,因为该字符串将包含神奇的 NSattachmentCharacter。但在编辑完成之前我不会获得有关附件的具体信息。
Because of the semantics of certain NSTextView attachments in my application, I want to know when they are inserted or deleted from my text storage.
My subclass of NSTextView implements the shouldChangeTextInRange:replacementString: method, which allows me to easily see when an attachment is about to be replaced (I can search the text storage at the specified range).
Because the replacement string is just an NSString and not an NSAttributedString, I have no way of seeing from this method whether an attachment is being inserted. The documentation even goes so far as to say that the string may be nil if "only attributes" are being edited.
So the question is, what's the best override point to see when an attachment is being inserted? Or perhaps as useful: what's the best override point to see when attributes are being modified?
Update: I said above I had no way of knowing whether an attachment is being inserted. It's pointed out to me that I can tell that "an" attachment is involved, because the string will contain the magic NSAttachmentCharacter. But I won't have specific information about the attachment until after the edit is complete.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会看一下 NSTextStorage 委托方法 -textStorageDidProcessEditing:,每次对底层文本存储进行更改时都应该调用该方法。然后,您可以使用 -editedRange、-editedMask 和 -changeInLength 方法来确定文本存储的哪个部分已更改,并在该范围中查找您可能感兴趣的任何附件。
I would take a look at the NSTextStorage delegate method -textStorageDidProcessEditing:, which should be called each time a change is made to the underlying text storage. You can then use the -editedRange, -editedMask, and -changeInLength methods to determine what section of the text storage was changed, and look in that range for any attachments that might be of interest to you.
您可能想看一下两个
NSTextStorage
委托方法:在
textStorageWill/DidProcessEditing
内,您可以调用-[NSTextStorageeditMask]
和>-[NSTextStorageeditedRange]
找出发生了什么变化,然后采取相应的措施。You might want to take a look at two
NSTextStorage
delegate methods:Inside
textStorageWill/DidProcessEditing
, you can call-[NSTextStorage editedMask]
and-[NSTextStorage editedRange]
to find out what changed and then take action accordingly.委托方法有各种极端情况。最好子类
NSTextStorage
类。Delegate methods have various corner cases. It's better to subclass
NSTextStorage
class.