UITextField AutoComplete iOS输入框内文本自动完成
当你打开Safari的时候,输入网址,会有许多候选网址,点击后,自动填充到输入框,进入网页。
打开词典查单词的时候,输入前面部分字母,软件会给出符合的候选单词。
这样做的目的,是为了省去用户繁琐的输入,节省时间,提升用户体验。
先上效果图

今天对基本的UITextField进行改装,让其具备此功能。
新建项目后,在Main.storyboard里,放好UItextField和UIButton。
下一步,使用control+drag将UITextField拖到interface文件里,选择outlet,名为*urlField
同理,UIButton拖进去,选择IBAction,名为goPressed
先引入将要使用的3个委托,两个是UITableView有关,一个用于收回键盘
建立两个Array, pastUrls和autocompleteUrls
建立一个UITableView用于呈现候选数据,下面是interface内代码
#import <UIKit/UIKit.h> @class WebViewController; @interface RootViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate> @property (nonatomic, retain) UITextField *urlField;
@property (nonatomic, retain) NSMutableArray *pastUrls;
@property (nonatomic, retain) NSMutableArray *autocompleteUrls;
@property (nonatomic, retain) UITableView *autocompleteTableView; - (IBAction)goPressed;
- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring; @end
pastUrls放入匹配的数据,初始化UITableView,并将其隐藏
- (void)viewDidLoad {
self.pastUrls = [[NSMutableArray alloc] initWithObjects:@"www.google.com", @"www.cnblog.com", nil];
self.autocompleteUrls = [[NSMutableArray alloc] init];
autocompleteTableView = [[UITableView alloc] initWithFrame:CGRectMake(, , , ) style:UITableViewStylePlain];
autocompleteTableView.delegate = self;
autocompleteTableView.dataSource = self;
autocompleteTableView.scrollEnabled = YES;
autocompleteTableView.hidden = YES;
[self.view addSubview:autocompleteTableView];
[super viewDidLoad];
}
- (IBAction)goPressed:(id)sender {
// 按下button,收回键盘,隐藏UITableView
[urlField resignFirstResponder];
autocompleteTableView.hidden = YES;
// Add the URL to the list of entered URLS as long as it isn't already there
if (![pastUrls containsObject:urlField.text]) {
[pastUrls addObject:urlField.text];
}
}
- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring {
// Put anything that starts with this substring into the autocompleteUrls array
// 过滤,剩下符合输入文字的候选
[autocompleteUrls removeAllObjects];
for(NSString *curString in pastUrls) {
NSRange substringRange = [curString rangeOfString:substring];
if (substringRange.location == 0) {
[autocompleteUrls addObject:curString];
}
}
[autocompleteTableView reloadData];
}
#pragma mark UITextFieldDelegate methods
//当用户增,删字符的时候,都会调用此方法
//
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
autocompleteTableView.hidden = NO;
NSString *substring = [NSString stringWithString:textField.text];
substring = [substring stringByReplacingCharactersInRange:range withString:string];
[self searchAutocompleteEntriesWithSubstring:substring];
return YES;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section {
return autocompleteUrls.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
static NSString *AutoCompleteRowIdentifier = @"AutoCompleteRowIdentifier";
cell = [tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AutoCompleteRowIdentifier] autorelease];
}
cell.textLabel.text = [autocompleteUrls objectAtIndex:indexPath.row];
return cell;
}
#pragma mark UITableViewDelegate methods
//将用户选择的候选网址,设置到输入框里,并调用button的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
urlField.text = selectedCell.textLabel.text;
[self goPressed];
}
UITextField AutoComplete iOS输入框内文本自动完成的更多相关文章
- ios应用内嵌h5页面数据自动变色识别为手机号码的解决方法——手机号码拨号禁用IOS手机页面数字自动识别为手机号
异常如下: ios应用内嵌h5页面,本来是设置了白色的数字,两三秒之后会自动变为黑色,然后点击的时候就会弹出是否拨号的提示: 解决方法: 添加如下meta标签,即可解决: <meta name= ...
- iOS 委托与文本输入(内容根据iOS编程编写)
文本框(UITextField) 本章节继续编辑 JXHypnoNerd .文件地址 . 首先我们继续编辑 JXHypnosisViewController.m 修改 loadView 方法,向 ...
- jQuery UI Autocomplete是jQuery UI的自动完成组件(share)
官网:http://jqueryui.com/autocomplete/ 以下分享自:http://www.cnblogs.com/yuzhongwusan/archive/2012/06/04/25 ...
- jQuery UI Autocomplete是jQuery UI的自动完成组件
支持的数据源 jQuery UI Autocomplete主要支持字符串Array.JSON两种数据格式. 普通的Array格式没有什么特殊的,如下: ? 1 ["cnblogs" ...
- 在IOS输入框中 键盘上显示“搜索”
移动端web页面上使用软键盘时如何让其显示“前往”(GO)而不是换行?‘ 用一个 form 表单包裹住就会显示前往,单独的一个 input 就会提示换行.下面是测试地址: 有表单:https://js ...
- 在iOS微信浏览器中自动播放HTML5 audio(音乐)的2种正确方式
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 【原创】修复ios输入框获取焦点时不支持fixed的bug
前些日子,做了一个手机站的项目,有一个页面是这样的, 有一个固定(position:fixed)的头部和底部导航,中间是一些表单内容,没啥特别的.但是到了ios中,正常滚动页面没有问题,一旦触发了文本 ...
- iOS应用内抓包、NSURLProtocol 拦截 APP 内的网络请求
前言 开发中遇到需要获取SDK中的数据,由于无法看到代码,所以只能通过监听所有的网络请求数据,截取相应的返回数据,可以通过NSURLProtocol实现,还可用于与H5的交互 一.NSURLProto ...
- input、textarea等输入框输入中文时,拼音在输入框内会触发input事件的问题
监听文本输入框的input事件,在拼写汉字(输入法)但汉字并未实际填充到文本框中(选词)时会触发input事件,如图: 但是在很多情况下,只需要输入到输入框的中文字符. 解决办法: 通过查阅资料得知在 ...
随机推荐
- 在外国网站上看到一个用artoolKit做的demo,学习了用gcd创建单列
外国网站:http://www.raywenderlich.com/42266/augmented-reality-ios-tutorial-location-based 看了下里面的demo,源代码 ...
- 线性代数(矩阵乘法):NOI 2007 生成树计数
这道题就是深搜矩阵,再快速幂. #include <iostream> #include <cstring> #include <cstdio> #include ...
- CSS3中text-overflow支持以...代替超出文本
CSS3中text-overflow支持以...代替超出文本. 1.div1:默认状态.超出文本默认显示在div外 2.div2:text-overflow:ellipsis; 使用text-over ...
- 数据结构——HDU1312:Red and Black(DFS)
题目描述 There is a rectangular room, covered with square tiles. Each tile is colored either red or blac ...
- [Locked] Meeting Room I && II
Meeting Room Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2 ...
- ssh技巧
1. 打通ssh key的简单方法: ssh-copyid 192.168.1.1 2.使用ssh 将Linux主机变成http代理服务器 ssh -NfD 192.168.22.1:10080 12 ...
- linux运维社区站点收集
1, 新世纪linux社区 offical locator: http://www.21ops.com/industry-news/24370.html 2,
- Spark GraphX的函数源码分析及应用实例
1. outerJoinVertices函数 首先给出源代码 override def outerJoinVertices[U: ClassTag, VD2: ClassTag] (other: RD ...
- JAVAEE filter总结
1. 为什么需要filter? filter相当于客户端和服务器端之间的一扇门,就像保安一样.作用:比如说设置字符集和权限控制等等. 2. 细节; * . 只能对post请求起作用 * .可以使 ...
- 系统自带.net版本
首先我们可以参照下面的图来得到各Windows系统包括server版的自带.NET Framework的信息,下图只列出了.NET Framework 2.0及其之后的版本. Which Versio ...