'UITextField Autocomplete?

I am trying to figure out if there is a way to implement an autocomplete functionality in a UITextField for specific values.

I know that the UITextField can do this using the iPhone dictionary (much like searching google in safari, etc), but I want to be able to programmatically have it correct to certain values that I specify.

How to do this?



Solution 1:[1]

I did something very similar to this while working on a recent and rather large project. We had a constantly changing list of auto complete terms and built an auto-complete around them.

First, you'll want to make some type of auto-complete controller. It should take a string and return all possible auto complete terms for that string.

-(NSArray *)completionsForString:(NSString *)myString;

Then, check out the UIMenuController class. It's the class that shows the cut/copy/paste options in many applications. You can get the shared instance of it, populate the menu items yourself, and show it above the text field. The user can then simply tap the term they want.

In the end, the solution worked really well for our needs.

Solution 2:[2]

Alternatively, you can use this UITextField subclass (inspired by DOAutocompleteTextField):

https://github.com/hoteltonight/HTAutocompleteTextField

It's got a few more features and is actively developed. The example shows you how to use an array as the data source for the autosuggest text. It takes the same approach as DOAutocompleteTextField, in that it shows the suggested completion text "ghosted" in the text field as the user types.

Solution 3:[3]

Have you looked into UISearchDisplayController? There are a few threads here on Stack Overflow, including Core Data references if that is what you are using. Also some alternative methods, elsewhere.

Solution 4:[4]

With the help of the aforementioned Ray Wenderlich tutorial, I just implemented a version of this to filter names in an existing UITableView.

I set my text field's delegate as my view controller, my view controller as a UITextFieldDelegate and implemented these two methods:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString *substring = [NSString stringWithString:textField.text];
    substring = [substring stringByReplacingCharactersInRange:range withString:string];
    [self searchAutocompleteEntriesWithSubstring:substring];
    return YES;
}

- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring
{   
    NSMutableArray *autoCompleteArray = [[NSMutableArray alloc]init];
    [self retrieveData];

    for(NSString *curString in _staffTableArray)
    {
        NSString *lowerCaseCur = [curString lowercaseString];
        NSRange substringRange = [lowerCaseCur rangeOfString:substring];
        if (substringRange.location == 0)
        {
            [autoCompleteArray addObject:curString];
        }
    }
    if (![substring isEqualToString:@""])
    {
         _staffTableArray = [NSMutableArray arrayWithArray:autoCompleteArray];
    }  
    [_staffListTableView reloadData];
}

Solution 5:[5]

use this delegate method. you can replace values that you specify.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;   // return NO to not change text
      if ([string isEqualToString:@"StackO"]) {
           textField.text=@"StackOverflow";
      }
    return YES;
}

Solution 6:[6]

Just faced with this thread because I need something similar. How about implementing you own search with the UITextfieldDelegate's method:

- (BOOL)textField:(UITextField *) textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

As you'd probably know this method is called for every UITextfield's typing.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dharman
Solution 2 codeperson
Solution 3 Community
Solution 4
Solution 5 Rakesh Bhatt
Solution 6 Carlos