IOS-UITextField-邮箱后缀联想匹配

来源:互联网 发布:别人的网页添加js脚本 编辑:程序博客网 时间:2024/05/01 18:07

最近做项目,有一个功能,百度了一下 结果没有 就研究了一下。 

当用户输入邮箱形式的账号时,输入完“@”符号后,联想出常用的邮箱

点击某一行,将改行代表邮箱自动输入到账号输入框内

 

如果控件属性不懂或者不认识 ,请百度!



说一下原理,首先我们要判断输入的是否是“@”,之后在在进行范围截取,最后匹配 

- (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 allocinitWithObjects:@"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:rangewithString:string];

   if (textField ==_accountTextField) {

       //是否包含@

       if ([textcontainsString:@"@"]) {  

       //抱歉 这是IOS8方法  此处可以替换为      ([text rangeOfString:@"@"].location !=NSNotFound)

            [selfsetShowList:YES];

            [self.tabviewDataremoveAllObjects];

           //范围

           NSRange range = [textrangeOfString:@"@"];

           if ((range.location + range.length) == text.length) {

               for (NSString *strinself.emalArray) {

                    [self.tabviewDataaddObject:[NSStringstringWithFormat:@"%@%@",text,str]];

                }

            }else{

               NSString *suffix = [textsubstringWithRange:NSMakeRange(range.location+range.length, text.length-(range.location+range.length))];

               NSString *headText = [textsubstringWithRange:NSMakeRange(0,range.location+range.length)];

               for (NSString *strinself.emalArray) {

                   //匹配

                   if ([strhasPrefix: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{

   staticNSString *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];

}





0 0
原创粉丝点击