Live editing an NSTextView
Live editing an NSTextView
- Subject: Live editing an NSTextView
- From: Arved von Brasch <email@hidden>
- Date: Wed, 26 Nov 2008 23:14:53 +1100
Dear Cocoa Dev List,
I am attempting to add live editing to a NSTextView. This means that
when the user types some specific text, the text view notices and
converts it into a different substring, with possibly a different
length. The idea is similar to an auto-correcting spell checker.
When a new character completes a special character sequence,
replaceCharactersInRange:withString: is called to make the change, and
I adjust the selection range to compensate for the change in length.
I have this working almost perfectly by listening for the
NSTextDidChangeNotification and searching the text for the special
sequence. I've found this to be the best place to handle it, so that
cut and paste operations containing the special sequence are converted
properly as well. The only problem with this is that it completely
screws up the Undo / Redo of the text view. It doesn't seem to be
possible to use the standard shouldChangeTextInRange: and
textDidChange pair, because my parser acts in response to an edit, and
this would set up an infinite loop.
I found I could manually add my changes to the undoManager, and that
would allow undos to function as expected, but redos won't work. ('As
expected' means restoring the modified substring with the originally
typed sequence minus the final character that triggered the change.
Redo should restore the modified string.) I don't have much
experience with the NSUndoManager, but I understand I could possibly
put the reciprocal action in a NSTextView subclass for
replaceCharactersInRange:, but as this is a special case, I don't see
any way to tell that the particular undo operation is in response to
my modification, and not something more generic.
I'm hoping someone has done something similar, and has an elegant
solution, or at least a different direction I could take.
Thanks,
Arved von Brasch
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden