Best way to modify user input into NSTextView
Best way to modify user input into NSTextView
- Subject: Best way to modify user input into NSTextView
- From: Zac Samuel <email@hidden>
- Date: Tue, 31 Mar 2009 21:47:26 +1100
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 (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