Skip navigation.
 
mlRe: Autocomplete word as you are typing
FROM : Stéphane Sudre
DATE : Wed Feb 06 23:43:04 2008

On Feb 6, 2008, at 10:50 PM, Chris Schmitt wrote:

> I am trying to figure out how I would go about doing an 
> autocompletion of a word as you are typing.  I am trying to do 
> something like any autocomplete done on the web where you start 
> typing in your information and if there is a 1 to 1 match against a 
> list it suggests a completion to the word.  I know there is the 
> "textView:completions:forPartialWordRange:indexOfSelectedItem:" 
> delegate method, but I want something that will happen as the 
> person types without hitting the escape key.
>
>
> Does anyone have any examples, advice, delegate methods to look at.


Maybe something like this (not tested, just wrote it in Mail):

[[NSNotificationCenter defaultNotificationCenter] addObserver:self
                           selector:@selector(controlTextDidChange:)
                            name: NSControlTextDidChangeNotification
                             object: myTextControl];

[NSNotificationCenter defaultNotificationCenter] addObserver:self
                           selector:@selector(controlTextDidEnd:)
                            name: NSControlTextDidEndEditingNotification
                             object: myTextControl];

...

- (void) controlTextDidChange:(NSNotification  *) inNotification
{
   [NSObject cancelPreviousPerformRequestsWithTarget:self];

   [NSObject performSelector:@selector(myCompletionRoutine:) 
withObject: [[inNotification userInfo] objectForKey:@"NSFieldEditor"] 
afterDelay:1.0f];
}

- (void) controlTextDidEnd:(NSNotification  *) inNotification
{        
   [NSObject cancelPreviousPerformRequestsWithTarget:self];
}

- (void) myCompletionRoutine:(NSTextFieldCell *) inTextFieldCell
{
   // Do your completion

   
}

Related mailsAuthorDate
mlAutocomplete word as you are typing Chris Schmitt Feb 6, 22:50
mlRe: Autocomplete word as you are typing Stéphane Sudre Feb 6, 23:43
mlRe: Autocomplete word as you are typing Joshua Emmons Feb 6, 23:48
mlRe: Autocomplete word as you are typing ? ?? Feb 7, 00:46
mlRe: Autocomplete word as you are typing ? ?? Feb 7, 00:51