我如何知道附件属性何时添加到我的 NSTextView 中?

发布于 2024-08-08 22:34:22 字数 436 浏览 3 评论 0原文

由于我的应用程序中某些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

稀香 2024-08-15 22:34:23

我会看一下 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.

反话 2024-08-15 22:34:23

您可能想看一下两个 NSTextStorage 委托方法:

- (void)textStorageWillProcessEditing:(NSNotification *)notification;
/* Delegate can change the characters or attributes */

- (void)textStorageDidProcessEditing:(NSNotification *)notification;
/* Delegate can change the attributes */

textStorageWill/DidProcessEditing 内,您可以调用 -[NSTextStorageeditMask]>-[NSTextStorageeditedRange] 找出发生了什么变化,然后采取相应的措施。

You might want to take a look at two NSTextStorage delegate methods:

- (void)textStorageWillProcessEditing:(NSNotification *)notification;
/* Delegate can change the characters or attributes */

- (void)textStorageDidProcessEditing:(NSNotification *)notification;
/* Delegate can change the attributes */

Inside textStorageWill/DidProcessEditing, you can call -[NSTextStorage editedMask] and -[NSTextStorage editedRange] to find out what changed and then take action accordingly.

还在原地等你 2024-08-15 22:34:23

委托方法有各种极端情况。最好子类NSTextStorage

Delegate methods have various corner cases. It's better to subclass NSTextStorage class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文