Re: Custom autocompletion with NSTextField
Re: Custom autocompletion with NSTextField
- Subject: Re: Custom autocompletion with NSTextField
- From: Kyle Sluder <email@hidden>
- Date: Sun, 31 Jul 2011 17:12:44 -0700
On Sun, Jul 31, 2011 at 4:29 PM, Joe White <email@hidden> wrote:
> Hi all,
>
> I'm currently implementing custom autocomplete functionality into a
> NSTextField. I've managed so far to generate a list of my own available
> words and these show when escape is pressed.
>
> However, I'd like the autocompletion to automatically show as the user
> types.
Have you thought about how this will interact with the default
autocorrect-options-as-you-type on Lion?
>
> I'm using the delegate methods of a NSTextView to override -
> (NSArray*)textView:(
> NSTextView *)textView completions:(NSArray *)words
> forPartialWordRange:(NSRange)charRange indexOfSelectedItem:(NSInteger*)index
>
> And setting the delegate in - (void)controlTextDidBeginEditing:(
> NSNotification *)obj {
>
> fieldEditor = [[obj userInfo] objectForKey:@"NSFieldEditor"];
>
> [fieldEditor setDelegate:self]; }
Do not change the delegate of a field editor. It needs to be the
control that's being edited. This is the mechanism whereby
-controlTextDidChange: gets called.
You have two options:
1. Subclass NSTextField, override the appropriate text view delegate
methods (like -textDidChange: or
-textView:shouldChangeTextInRange:replacementString:), and call
super's implementation.
2. Add your object as an observer of the relevant NSNotifications on
the field editor. You might still need to subclass NSTextField so you
can perform your setup in -setUpFieldEditorAttributes:, but most
likely you could get away with doing this in your window delegate's
-window:willReturnFieldEditor:toObject:. Still, this approach is
slightly less powerful, but if you don't need all the power of being
the field editor's delegate it could result in a cleaner
implementation.
--Kyle Sluder
_______________________________________________
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