标签:ios邮箱匹配 ios邮箱后缀 uitextfield邮箱后缀 ios邮箱联想匹配 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
{
[self unregisterNotifications];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self registerNotifications];
_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= [self createLoginField:@"手机号/用户名/邮箱/"]; //此处自定义控件 不会请百度
_accountTextField.frame = CGRectMake(0, 0,220,49);
[self.view addSubview:_accountTextField];
//下拉列表
_listTableView = [[UITableView alloc]initWithFrame:
CGRectMake(0,0,280,120)];
_listTableView.top = 50;
_listTableView.left = 20;
_listTableView.dataSource=self;
_listTableView.delegate=self;
_listTableView.bounces = NO;
_listTableView.backgroundColor= [UIColor whiteColor];
_listTableView.separatorColor= [UIColor lightGrayColor];
_listTableView.hidden=!_showList;//一开始listView是隐藏的,此后根据showList的值显示或隐藏
[self.view addSubview:_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
{
[[[UIApplication sharedApplication] keyWindow] endEditing:YES];
return YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
//判断text 是否输入过@ 如果输入过则不出现下啦菜单
NSString *text = [textField.text stringByReplacingCharactersInRange:range withString:string];
if (textField ==_accountTextField) {
//是否包含@
if ([text containsString:@"@"]) {
[self setShowList:YES];
[self.tabviewData removeAllObjects];
//范围
NSRange range = [text rangeOfString:@"@"];
if ((range.location + range.length) == text.length) {
for (NSString *str in self.emalArray) {
[self.tabviewData addObject:[NSString stringWithFormat:@"%@%@",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 *str in self.emalArray) {
//匹配
if ([str hasPrefix:suffix]) {
[self.tabviewData addObject:[NSString stringWithFormat:@"%@%@",headText,str]];
}
}
if (self.tabviewData.count<=0) {
[self setShowList:NO];
}
}
[self.listTableView reloadData];
}else
{
[self setShowList:NO];
}
}
return YES;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
//返回一个BOOL值指明是否允许根据用户请求清除内容
//可以设置在特定条件下才允许清除内容
[self setShowList:NO];
return YES;
}
#pragma mark 监听键盘
- (void)registerNotifications
{
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFiledEditChanged:)
name:@"UITextFieldTextDidChangeNotification"
object:nil];
}
- (void)unregisterNotifications
{
//移除通知
[[NSNotificationCenter defaultCenter]removeObserver:self];
}
- (void)textFiledEditChanged:(NSNotification *)obj
{
//此处可以拿到 正在输入的值 做一些处理
}
#pragma mark listViewdataSource method and delegate method
-(NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section{
return self.tabviewData.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellid=@"listviewid";
UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:cellid];
if(cell==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellid];
cell.layer.borderColor = [UIColor grayColor].CGColor;
cell.layer.borderWidth = 1;
}
cell.textLabel.frame = CGRectMake(0, 0, 220, 40);
cell.textLabel.text = [self.tabviewData objectAtIndex: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.tabviewData objectAtIndex:indexPath.row];
_accountTextField.text = string;
[self setShowList:NO];
}
标签:ios邮箱匹配 ios邮箱后缀 uitextfield邮箱后缀 ios邮箱联想匹配 uitextfield邮箱后缀匹配
原文地址:http://blog.csdn.net/han63504/article/details/40401609