Re: limit text input in NSTextView
Re: limit text input in NSTextView
- Subject: Re: limit text input in NSTextView
- From: Douglas Davidson <email@hidden>
- Date: Tue, 17 Sep 2002 12:33:24 -0700
On Tuesday, September 17, 2002, at 12:18 PM, Arthur Clemens wrote:
It seems that shouldChangeTextInRange: is useful for intercepting
character input; for delete and arrow keys navigation I must still
rely on keyDown.
Let me sketch my problem:
I have a NSTextView with a number of 'keywords' that I want to be
immutable - you cannot delete them or click inside them. Around the
keywords (and between the keywords) is text that allows all the normal
operations: insert, delete, paste, etcetera.
When the user does a text operation, for instance 'delete', I must
know the cursor position relative to the keywords.
So for instance when I have the cursor directly to the right side of a
keyword, I must intercept the delete command. If the cursor is placed
elsewhere, I will allow the delete command.
As
- (BOOL)shouldChangeTextInRange:(NSRange)affectedCharRange
replacementString:(NSString *)replacementString
gives me a string but no keycode, I cannot infer what key is being
pressed, when this is the delete key or an arrow key.
So I now assume I have to do some checking in both
shouldChangeTextInRange: and in keyDown:.
No, you still don't want to use keyDown: for this. To prevent deletion
of your keywords, implement the
textView:shouldChangeTextInRange:replacementString: delegate method and
determine whether the affectedCharRange intersects any of your
keywords. If it does, disallow the change.
This method will not be called when an arrow key is pressed, because
pressing an arrow key does not change the text. Instead, it changes
the selection. If you want to control the selection--for example, to
prevent keywords or portions of keywords from being selected--implement
the delegate method
- (NSRange)textView:(NSTextView *)textView
willChangeSelectionFromCharacterRange:(NSRange)oldSelectedCharRange
toCharacterRange:(NSRange)newSelectedCharRange;
which allows you to prevent or alter any change in selection, whether
via keyboard or mouse.
If you want to be informed specifically when a special key is pressed,
implement
- (BOOL)textView:(NSTextView *)textView
doCommandBySelector:(SEL)commandSelector;
and look for appropriate selectors, e.g. @selector(insertNewline:) for
carriage return.
I suggest you take a look at Address Book on Jaguar. Try editing an
entry, for example. What you see there is a single text view,
customized by methods like these. Then take a look at some of the ADC
examples, e.g. the TextViewDelegate example.
Douglas Davidson
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.