ios 的UITextField自动完成?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3198337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:24:35  来源:igfitidea点击:

UITextField Autocomplete?

iosobjective-cautocompleteuitextfield

提问by Brett

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

我想弄清楚是否有办法在UITextField特定值中实现自动完成功能。

I know that the UITextFieldcan 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.

我知道UITextField可以使用 iPhone 字典来做到这一点(很像在 safari 中搜索 google 等),但我希望能够以编程方式将它正确地设置为我指定的某些值。

How to do this?

这该怎么做?

回答by DexterW

Brett,

布雷特,

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.

然后,查看 UIMenuController 类。它是在许多应用程序中显示剪切/复制/粘贴选项的类。您可以获取它的共享实例,自己填充菜单项,并将其显示在文本字段上方。然后用户可以简单地点击他们想要的术语。

In the end, the solution worked really well for our needs. I hope it works for you.

最后,该解决方案非常适合我们的需求。我希望这个对你有用。

回答by Mic Pringle

Check out this tutorialfrom Ray Wenderlich which demonstrates one way to achieve custom autocomplete

查看Ray Wenderlich 的本教程,其中演示了一种实现自定义自动完成的方法

回答by codeperson

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

或者,您可以使用此 UITextField 子类(受 DOAutocompleteTextField 启发):

https://github.com/hoteltonight/HTAutocompleteTextField

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.

它还有一些功能,并且正在积极开发中。该示例向您展示了如何使用数组作为自动建议文本的数据源。它采用与 DOAutocompleteTextField 相同的方法,因为它在用户键入时在文本字段中显示建议的完成文本“重影”。

回答by Robert

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

在前面提到的 Ray Wenderlich 教程的帮助下,我刚刚实现了一个版本来过滤现有UITableView.

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

我将我的文本字段的委托设置为我的视图控制器,我的视图控制器设置为 aUITextFieldDelegate并实现了这两个方法:

- (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];
}

回答by DenVog

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.

你调查过UISearchDisplayController吗?Stack Overflow 上有一些线程,包括 Core Data 引用(如果这是您正在使用的)。还有一些替代方法,别处。

回答by Rakesh Bhatt

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;
}

回答by Carlos

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

刚刚面对这个线程,因为我需要类似的东西。如何使用 UITextfieldDelegate 的方法实现您自己的搜索:

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

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

正如您可能知道的那样,每个 UITextfield 的输入都会调用此方法。