//设置边框模式
tf.borderStyle = UITextBorderStyleRoundedRect;
//设置最小字体
tf.minimumFontSize = 15;
//设置默认文本
tf.text = @”username”;
//编辑时自动清空
tf.clearsOnBeginEditing = YES;
//设置删除按钮显示模式
tf.clearButtonMode = UITextFieldViewModeWhileEditing;
//开启密码输入模式
//tf.secureTextEntry = YES;
//垂直方向的对齐方式,容易忽略
tf.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
//水平方向的对齐方式
tf.textAlignment = NSTextAlignmentCenter;
//设置自动大写
tf.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
//自动校正模式(提示)
tf.autocorrectionType = UITextAutocorrectionTypeYes;
//设置键盘种类
//tf.keyboardType = UIKeyboardTypeNumberPad;
//设置代理
tf.delegate = self;
/设置正常状态下的背景图
tf.background = [UIImage imageNamed:@”normalbg”];
//设置禁用状态下的背景图,在tf.enabled为NO时显示
tf.disabledBackground = [UIImage imageNamed:@”disable”];
//设置左边视图||//设置右边视图,会覆盖掉clearButton
tf.leftView = leftView;
tf.leftViewMode = UITextFieldViewModeAlways;
//代理方法之一
//是否结束编辑
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
if (textField.text.length < 6) {
return NO;
}
return YES;
}
//递归查找第一响应者,自己实现
- (UITextField *)searchFirstResponderOnView:(UIView *)view
{
for (UITextField *subView in view.subviews) {
if ([subView isFirstResponder]) {
return subView;
} else {
UITextField *grandchild = [self searchFirstResponderOnView:subView];
if (grandchild) {
return grandchild;
}
}
}
return nil;
}
//定制二级键盘
_textField.inputAccessoryView = [self customAccessoryView];
//设置主键盘
_textField.inputView = view;
#pragma mark - 监听键盘
-(void)monitorKeyboard
{
//监听键盘弹出
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
//监听键盘收起
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification *)news
{
//NSLog(@"%@", news.userInfo);
NSValue *value = [news.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
//转换成frame
CGRect frame = [value CGRectValue];
CGPoint center = _textField.center;
center.y -= frame.size.height;
_textField.center = center;
}
- (void)keyboardWillHide:(NSNotification *)news
{
//NSLog(@"%@", news.userInfo);
NSValue *value = [news.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
//转换成frame
CGRect frame = [value CGRectValue];
CGPoint center = _textField.center;
center.y += frame.size.height;
_textField.center = center;
}
//结束监听
[[NSNotificationCenter defaultCenter] removeObserver:self];