NSTextField Autocomplete

Does anyone know of any class or lib that can implement autocomplete for NSTextField?

I am trying to make standard autocomplete work, but it is made as a synchronous api. I get my autocomplete words through an api call over the Internet.

What i have done so far:

- (void)controlTextDidChange:(NSNotification *)obj { if([obj object] == self.searchField) { [self.spinner startAnimation:nil]; [self.wordcompletionStore completeString:self.searchField.stringValue]; if(self.doingAutocomplete) return; else { self.doingAutocomplete = YES; [[[obj userInfo] objectForKey:@"NSFieldEditor"] complete:nil]; } } } 

When my store is completed, I have a delegate that is called:

 - (void) completionStore:(WordcompletionStore *)store didFinishWithWords:(NSArray *)arrayOfWords { [self.spinner stopAnimation:nil]; self.completions = arrayOfWords; self.doingAutocomplete = NO; } 

Code that returns a list of completions in the nstext field:

 - (NSArray *)control:(NSControl *)control textView:(NSTextView *)textView completions:(NSArray *)words forPartialWordRange:(NSRange)charRange indexOfSelectedItem:(NSInteger *)index { *index = -1; return self.completions; } 

My problem is that it will always be 1 request behind, and a completion list is only shown on every second char user input.

I tried to find google and SO as a crazy person, but I can not find any solutions.

Any help is greatly appreciated.

+8
objective-c autocomplete cocoa nstextfield
source share
2 answers

Instead of having the boolean property makeAutocomplete, make the property your control executed the request. Let me call it autoCompleteRequestor:

 @property (strong) NSControl* autoCompleteRequestor; 

So, when you set the current makeAutocomplete property to YES, store the link to your control instead.

 - (void)controlTextDidChange:(NSNotification *)obj { if([obj object] == self.searchField) { [self.spinner startAnimation:nil]; [self.wordcompletionStore completeString:self.searchField.stringValue]; if(self.autoCompleteRequestor) return; else { self.autoCompleteRequestor = [[obj userInfo] objectForKey:@"NSFieldEditor"]; } } } 

Now that your web request is complete, you can call complete: on your saved object.

 - (void) completionStore:(WordcompletionStore *)store didFinishWithWords:(NSArray *)arrayOfWords { [self.spinner stopAnimation:nil]; self.completions = arrayOfWords; if (self.autoCompleteRequestor) { [self.autoCompleteRequestor complete:nil]; self.autoCompleteRequestor = nil; } } 
+9
source share

NSTextView has word completion functionality for partial words.

See the documentation for this component. Perhaps you can switch to this component in your application.

-one
source share

All Articles