UITextField是开发当中常用的控件,通常用于外部数据输入,以实现人机交互。下面我对它的一些常用功能做了一个简单的总结。
1.更改placeholder的属性
[textField setValue[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"]; [textField setValue[UIFont boldSystemFontOfSize16] forKeyPath:@"_placeholderLabel.font"];
2.点击输入框弹出键盘,并实现了回收键盘的功能,这里使用了UITextField的代理
#pragma mark - UITextFieldDelegate- (BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return YES; } -(BOOL)textFieldShouldBeginEditing:(UITextField *)textField { CGRect frame = _viewTF.frame; if (_flag == 20) { _flag = frame.origin.y; } frame.origin.y = 20; _viewTF.frame = frame; return YES; } -(BOOL)textFieldShouldEndEditing:(UITextField *)textField { CGRect frame = _viewTF.frame; frame.origin.y = _flag; _viewTF.frame = frame; return YES; }
这样干点击最后一个输入框,再点击上一个输入框,键盘就回去了,用户体验不好,我们可以使用一个通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dealShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dealHide:) name:UIKeyboardWillHideNotification object:nil]; -(void)dealShow:(NSNotification *)n { double time = [n.userInfo[@"UIKeyboardAnimationDurationUserInfoKey"] doubleValue]; CGRect frame; [n.userInfo[@"UIKeyboardFrameEndUserInfoKey"] getValue:&frame]; CGSize size = [UIScreen mainScreen].bounds.size; [UIView animateWithDuration:time animations:^{ _bottomButton.frame = CGRectMake(0, size.height-frame.size.height - 44, _bottomButton.frame.size.width, _bottomButton.frame.size.height); }]; CGRect frameTF = _viewTF.frame; if (_flag == 20) { _flag = frame.origin.y; } frameTF.origin.y = 20; _viewTF.frame = frameTF; } -(void)dealHide:(NSNotification *)n { double time = [n.userInfo[@"UIKeyboardAnimationDurationUserInfoKey"] doubleValue]; CGRect frame; [n.userInfo[@"UIKeyboardFrameEndUserInfoKey"] getValue:&frame]; CGSize size = [UIScreen mainScreen].bounds.size; [UIView animateWithDuration:time animations:^{ _bottomButton.frame = CGRectMake(0, size.height - 44, _bottomButton.frame.size.width, _bottomButton.frame.size.height); }]; CGRect frameTF = _viewTF.frame; frameTF.origin.y = _flag; _viewTF.frame = frameTF; }
3.输入密码时限制只输入数字,将Secure Text Entry勾选上就可以使用系统的安全输入
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSCharacterSet *cs; if(textField == _numberTextField) { cs = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789 "] invertedSet]; NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""]; BOOL basicTest = [string isEqualToString:filtered]; if(!basicTest) { return NO; } } return YES; }
4.限制输入字符个数
-(void)create { _codeTextField.delegate = self; [_codeTextField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"]; [_codeTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged]; _win = [[UIApplication sharedApplication].windows objectAtIndex:1]; } -(void)textFieldDidChange:(UITextField *)textField { if (textField.text.length>4) { textField.text = [textField.text substringToIndex:4]; } }