UITextField
2016-08-19 本文已影响10人
L柠_檬
目录
1.1 设置返回键样式
1.2 设置边框样式
1.3 一键删除
1.4 密文
1.5 添加左右视图
1.6 监听键盘出现
1.7 放弃第一响应者
1.8 数字键盘
1.9 手机号验证
1.10 return键点击
1.1 设置返回键样式
textField.returnKeyType=UIReturnKeyDone;
1.2 设置边框样式
textField.borderStyle=UITextBorderStyleRoundedRect;
1.3 一键删除
textField.clearButtonMode=UITextFieldViewModeAlways;
1.4 密文
textField.secureTextEntry=NO;
1.5 添加左右视图
UIImage *image=[UIImage imageNamed:@"account_candou"];
UIImageView *imageview=[[UIImageView alloc]initWithFrame:CGRectMake(0,0 ,
50, 50)];
imageview.image=image;
textField.leftView=imageview;
textField.leftViewMode=UITextFieldViewModeAlways;
1.6 监听键盘出现
1.出现
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification object:nil];
2.消失
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHidden:)
name:UIKeyboardWillHideNotification object:nil];
3.实现方法
- (void)keyboardWillShow:(NSNotificationCenter *)noti{
self.tableView.frame = CGRectMake(0, 0,
SCREEN_WIDTH, SCREEN_HEIGHT - 280);
}
- (void)keyboardWillHidden:(NSNotificationCenter *)noti{
self.tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
}
1.7 放弃第一响应者
- (BOOL)textFieldShouldReturn:(UITextField *)textField;
{
[textField resignFirstResponder];
return YES;
}
1.8 数字键盘
textField.keyboardType = UIKeyboardTypePhonePad;
1.9 手机号验证
记得写代理
- (BOOL)textField:(UITextField*)textField
shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString*)string {
NSCharacterSet *cs;
cs = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789"]
invertedSet];
NSString*filtered = [[string componentsSeparatedByCharactersInSet:cs]
componentsJoinedByString:@""];
BOOL basicTest = [string isEqualToString:filtered];
if(!basicTest) {
return NO;
}
NSString *temp = [textField.text
stringByReplacingCharactersInRange:range
withString:string];
if (temp.length > 11) {
return NO;
}
return YES;
}
1.10 return键点击
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
return YES;
}