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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With