首页 > 代码库 > UITextField-邮箱后缀联想匹配
UITextField-邮箱后缀联想匹配
最近做项目,有一个功能,百度了一下 结果没有 就研究了一下。
当用户输入邮箱形式的账号时,输入完“@”符号后,联想出常用的邮箱
点击某一行,将改行代表邮箱自动输入到账号输入框内
如果控件属性不懂或者不认识 ,请百度!
- (BOOL)hasPrefix:(NSString *)aString //系统 已经提供了匹配方法,用不着正则! 直接上代码!
#import "UserLoginViewController.h"
@interface UserLoginViewController ()<UITextFieldDelegate,UITableViewDataSource,UITableViewDelegate>
{
BOOL _showList;
}@property (nonatomic)UITextField *accountTextField;
@property (nonatomic)UITableView *listTableView;
@property (nonatomic)NSArray *emalArray;//邮箱后缀
@property (nonatomic)NSMutableArray *tabviewData; //服务器数据
- (void)dealloc
{
[selfunregisterNotifications];
}
- (void)viewDidLoad {
[superviewDidLoad];
[selfregisterNotifications];
_showList = NO;//默认不显示
self.emalArray = [[NSArray alloc] initWithObjects:@"sohu.com",@"sina.com",@"sina.cn",@"163.com",@"126.com",@"qq.com",@"hotmail.com",@"gmail.com", nil];
self.tabviewData = [NSMutableArray array];
_accountTextField= [selfcreateLoginField:@"手机号/用户名/邮箱/"]; //此处自定义控件 不会请百度
_accountTextField.frame =CGRectMake(0,0,220,49);
[self.view addSubview:_accountTextField];
//下拉列表
_listTableView = [[UITableViewalloc]initWithFrame:
CGRectMake(0,0,280,120)];
_listTableView.top =50;
_listTableView.left =20;
_listTableView.dataSource=self;
_listTableView.delegate=self;
_listTableView.bounces =NO;
_listTableView.backgroundColor= [UIColorwhiteColor];
_listTableView.separatorColor= [UIColorlightGrayColor];
_listTableView.hidden=!_showList;//一开始listView是隐藏的,此后根据showList的值显示或隐藏
[self.viewaddSubview:_listTableView];
// Do any additional setup after loading the view.
}
-(BOOL)showList{//setShowList:No为隐藏,setShowList:Yes为显示
return_showList;
}
-(void)setShowList:(BOOL)iShow{
_showList=iShow;
_listTableView.hidden=!iShow;
}
核心代码
#pragma mark UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[[[UIApplicationsharedApplication] keyWindow]endEditing:YES];
return YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
//判断text是否输入过@ 如果输入过则不出现下啦菜单
NSString *text = [textField.textstringByReplacingCharactersInRange:range withString:string];
if (textField ==_accountTextField) {
//是否包含@
if ([text containsString:@"@"]) {
[selfsetShowList:YES];
[self.tabviewDataremoveAllObjects];
//范围
NSRange range = [text rangeOfString:@"@"];
if ((range.location + range.length) == text.length) {
for (NSString *strin self.emalArray) {
[self.tabviewDataaddObject:[NSStringstringWithFormat:@"%@%@",text,str]];
}
}else{
NSString *suffix = [text substringWithRange:NSMakeRange(range.location+range.length, text.length-(range.location+range.length))];
NSString *headText = [text substringWithRange:NSMakeRange(0,range.location+range.length)];
for (NSString *strin self.emalArray) {
//匹配
if ([str hasPrefix:suffix]) {
[self.tabviewDataaddObject:[NSStringstringWithFormat:@"%@%@",headText,str]];
}
}
if (self.tabviewData.count<=0) {
[selfsetShowList:NO];
}
}
[self.listTableViewreloadData];
}else
{
[selfsetShowList:NO];
}
}
return YES;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
//返回一个BOOL值指明是否允许根据用户请求清除内容
//可以设置在特定条件下才允许清除内容
[selfsetShowList:NO];
return YES;
}
#pragma mark 监听键盘
- (void)registerNotifications
{
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(textFiledEditChanged:)
name:@"UITextFieldTextDidChangeNotification"
object:nil];
}
- (void)unregisterNotifications
{
//移除通知
[[NSNotificationCenterdefaultCenter]removeObserver:self];
}
- (void)textFiledEditChanged:(NSNotification *)obj
{
//此处可以拿到 正在输入的值 做一些处理
}
#pragma mark listViewdataSource method and delegate method
-(NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section{
returnself.tabviewData.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellid=@"listviewid";
UITableViewCell* cell=[tableViewdequeueReusableCellWithIdentifier:cellid];
if(cell==nil){
cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellid];
cell.layer.borderColor = [UIColorgrayColor].CGColor;
cell.layer.borderWidth =1;
}
cell.textLabel.frame =CGRectMake(0,0, 220,40);
cell.textLabel.text = [self.tabviewDataobjectAtIndex:indexPath.row];
cell.textLabel.font =_accountTextField.font;
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 40;
}
//当选择下拉列表中的一行时,设置文本框中的值,隐藏下拉列表
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//显示值
NSString *string = [self.tabviewDataobjectAtIndex:indexPath.row];
_accountTextField.text = string;
[selfsetShowList:NO];
}
UITextField-邮箱后缀联想匹配