Subject: Re: Best way to modify user input into NSTextView

On Mar 31, 2009, at 8:12 AM, cocoa-dev-request@xxxxxxxxxxxxxxx wrote:

From: Zac Samuel <happyzac@xxxxxxxxx>
Date: March 31, 2009 3:47:26 AM PDT
To: cocoa-dev@xxxxxxxxxxxxxxx
Subject: Best way to modify user input into NSTextView

I'd like to override the behavior of an NSTextView so that when a certain
character is typed, a different character is inserted. For example I'd like
"×" to be inserted when the user types "*". This can be done really easily
using the shouldChangeTextInRanges:replacementStrings delegate method:
However I don't feel like this is the way to go. It seems to be that these
delegate methods should only be for preventing user input in certain
circumstances, not for actually inserting or adding text. For a start this
approach seems to disrupt the way NSTextView handles undos, and if an
NSTextAttachment were to be inserted instead of plain text, the layout of
the view will not update.

Can anyone suggest the "correct" way of doing such modify user input into

I don't know about "correct," but I've done something similar in a derived class of NSTextField by overriding insertText:, testing for the string to be replaced, and then passing the replacement string to [super insertText:]. Of course, to use this approach you have to implement windowWillReturnFieldEditor:toObject: in your window controller to return your derived class when asked.

Kirk Swenson
KCP Technologies


Cocoa-dev mailing list (Cocoa-dev@xxxxxxxxxxxxxxx)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)

Help/Unsubscribe/Update your Subscription:


This email sent to maillists@xxxxxxxxx