How can I enable case-insensitive form autocompletion for NSComboBox?

I bound NSComboBox to the data source in the interface builder. I get auto-fill suggestions correctly when I type in NSComboBox.

However, autocomplete is case sensitive, which means that I don't get any suggestions if the character uses the wrong case.

How can I enable case-insensitive autocomplete for NSComboBox associated with data source in interface builder?

thank

+5
source share
2 answers

You must implement comboBox:completedString:NSComboBox in the data source, for example:

- (NSString *)comboBox:(NSComboBox *)comboBox completedString:(NSString *)partialString
{
    for (NSString dataString in dataSourceArray) {
        if ([[dataString commonPrefixWithString:partialString options:NSCaseInsensitiveSearch] length] == [commonPrefixWithString:partialString length]) {
            return testItem;
        }
    }
    return @"";
}
+4
source

you can subclass NSComboBoxCell and override [NSComboBoxCell completedString:].

- (NSString *)completedString:(NSString *)string
{
    NSString *result = nil;

    if (string == nil)
        return result;

    for (NSString *item in self.objectValues) {
        NSString *truncatedString = [item substringToIndex:MIN(item.length, string.length)];
        if ([truncatedString caseInsensitiveCompare:string] == NSOrderedSame) {
            result = item;
            break;
        }
    }

    return result;
}
+4

All Articles