Subject: Best way to modify user input into NSTextView



Hi,
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:

For example:

**
*

- (BOOL)textView:(NSTextView *)textView
shouldChangeTextInRanges:(NSArray*)affectedRanges replacementStrings:(
NSArray *)replacementStrings{

if ([[replacementStrings objectAtIndex:0] isEqualToString:@"*"]){

[textView insertText:@"×"];

return NO;

}

return YES;

}


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
NSTextViews?

Much obliged,

-Zac
**

***
_______________________________________________

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)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/maillists%40codeha.us

This email sent to maillists@xxxxxxxxx

...



Privacy