Autocomplete UITextField

≯℡__Kan透↙ 提交于 2020-01-11 10:54:09

问题


I want to auto complete a text field from a custom value

Having researched Google and SO here UITextField Autocomplete - iPhone SDK

I tried this:

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

    if ([string isEqualToString:@"Fac"]) {
        _clientField.text = @"Factory";
    }

    return YES;
}

Problem is I get no predicted value entered Factory, just the typed value Fac

EDIT

Tried this based on answers...

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

if ([string isEqualToString:@"Fac"]) {
    _clientField.text = [_clientField.text stringByReplacingCharactersInRange:range
                                                   withString:@"Factory"];
  }

  return NO;
}

Still the same


回答1:


I checked your sample code and the text field's delegate is not set.

You can set it in your view controller using

_clientField.delegate = self;

Additionally you need to use a slightly different method to get the text which the users sees. Something like this:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSString *currentString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if ([currentString isEqualToString:@"Fac"]) {
        textField.text = @"Factory";
        return NO;
    }
    return YES;
}

Note that you probably want to fine tune this a bit, since it e.g. also autocompletes when the user deletes text. But this should get you on the right track.




回答2:


Sounds like HTAutocompleteTextField will do the job:

https://github.com/hoteltonight/HTAutocompleteTextField

You basically just need to give it an array of values as the autocomplete data source, and it will make the suggestions in the text field as the user types.




回答3:


You should have tried to NSLog string it always returns the last character the user inputs (except if you copy/paste a string: in this case your code works).

Actually, doing what you're trying to do is not as straightforward as it seems to be. Here what I came up with:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSLog(@"string: %@", string);

    NSString *currentText = textField.text;
    NSString *nextText = [currentText stringByAppendingString:string];

    if ([nextText isEqualToString:@"Fac"])
    {
        textField.text = @"Factory";
        return NO;
    }
    return YES;
}  

If you don't build the nextText yourself then the autocompletion will be delayed. If you only check the value of textField.text for instance, and that you type "Fac" the completion will occur on the next character input.
When you autocomplete the field you have to return NO also so the string is not added at the end of the text field (F -> Fa -> Factoryc, you don't want that). So you say that textField should not change characters in range... And YOU change the textField value yourself.

Hope this will help you to understand what's going on in this UITextFieldDelegate method.




回答4:


You can also have a look at this control (NHAutoCompleteTextField). In the example source code in the given link, you will see the search list filters as the user types.

NHAutoCompleteTextField is easy to integrate:

#import "NHMainHeader.h"

autoCompleteTextField = [[NHAutoCompleteTextField alloc] initWithFrame:CGRectMake(x, y, width, 18)];  // 18 - example height
[autoCompleteTextField setDropDownDirection:NHDropDownDirectionDown];
[autoCompleteTextField setDataSourceDelegate:self];
[autoCompleteTextField setDataFilterDelegate:self];

and flexible to do customization as per your need. for example, create cell as per you need here:

- (UITableViewCell *)autoCompleteTextBox:(NHAutoCompleteTextField *)autoCompleteTextBox cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

}

Adjustable for the given direction of drop down.

typedef enum
{
    NHDropDownDirectionUp,
    NHDropDownDirectionDown

} NHDropDownDirection;

This control also offers some good extension functions that will help to deal with other UIs as well.

#import "UIView+NHExtension.h"
#import "UILabel+Boldify.h"


来源:https://stackoverflow.com/questions/14384638/autocomplete-uitextfield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!