How to prevent NSSearchField from overwriting entered lines using the first entry in the autocomplete list?

I am looking for a way to create an nssearch field that behaves as follows:

  • types of users in the text
  • match-based pop-up autocomplete appears
  • the text in the search field is not autocomplete to the first item in the list

The fact is that my string matching the search for any substring and autocomplete in the text field will not work, because it will overwrite my entered string. Actually, it looks like this should be the default behavior, or do I not understand the purpose of the search field?
Imprinting further would limit the list further and further, but only after selecting an item in the autocomplete drop-down list, this element would be inserted into the text box.

If this is not possible using the nssearchfield field, is there an alternative?

+5
source share
1 answer

My own solution was actually very simple: just add the search bar to the list of suggestions for autocomplete.
This is done in the NSSearchField control:textView:completions:forPartialWordRange:indexOfSelectedItem: delegate method::

 ... partialString = [[textView string] substringWithRange:charRange]; ... matches = [NSMutableArray array]; // find any match in our keyword array against what was typed - for (i=0; i< count; i++) { string = [keywords objectAtIndex:i]; if ([string rangeOfString:partialString options: NSCaseInsensitiveSearch | NSForcedOrderingSearch range:NSMakeRange (0, [string length])] .location != NSNotFound) { [matches addObject:string]; } } [matches sortUsingSelector:@selector(compare:)]; // Make sure we insert the already entered string, even if it does not // match with any of the retrieved keywords. This will enter this string // in the search field, as we intended, and it will not be overwritten // with any match. [matches insertObject:partialString atIndex: 0]; return matches; 
+3
source

All Articles