UITextField的基本使用
- 有时候光标距离textField的左边太近 想调节一下距离:
// 设置文本框左边的内容
UIView *leftView = [[UIView alloc] init];
leftView.frame = CGRectMake(0, 0, 10, 0);
textField.leftView = leftView;
//模式设置为一直显示
textField.leftViewMode = UITextFieldViewModeAlways;
-
常见代理方法
//是否允许开始编辑 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField; //是否允许结束编辑 - (BOOL)textFieldShouldEndEditing:(UITextField *)textField; // 是否允许用户输入文字 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; // 文本框开始编辑的时候调用 - (void)textFieldDidBeginEditing:(UITextField *)textField;
键盘弹出时的notification
弹出的通知名称
键盘状态改变的时候,系统会发出一些特定的通知
UIKeyboardWillShowNotification // 键盘即将显示
UIKeyboardDidShowNotification // 键盘显示完毕
UIKeyboardWillHideNotification // 键盘即将隐藏
UIKeyboardDidHideNotification // 键盘隐藏完毕
UIKeyboardWillChangeFrameNotification//键盘的位置尺寸即将发生改变
UIKeyboardDidChangeFrameNotification // 键盘的位置尺寸改变完毕
- 通知中包含的有用的信息
系统发出键盘通知时,会附带一下跟键盘有关的额外信息(字典),字典常见的key如下:
UIKeyboardFrameBeginUserInfoKey // 键盘刚开始的frame
UIKeyboardFrameEndUserInfoKey // 键盘最终的frame(动画执行完毕后)
UIKeyboardAnimationDurationUserInfoKey // 键盘动画的时间
UIKeyboardAnimationCurveUserInfoKey // 键盘动画的执行节奏(快慢)
键盘弹出和消失的时候屏幕的改变
- 在键盘弹出和消失的时候一般控制器的view会作出相应的改变,以便键盘不会遮挡住view
- (void)viewDidLoad {
[super viewDidLoad];
// 监听键盘通知
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillChangeFrame:)
name:UIKeyboardWillChangeFrameNotification
object:nil];
}
- (void)keyboardWillChangeFrame:(NSNotification *)notification {
// 取出键盘最终的frame
CGRect rect = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
// 取出键盘弹出需要花费的时间
double duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
// 修改transform
[UIView animateWithDuration:duration animations:^{
CGFloat ty = [UIScreen mainScreen].bounds.size.height - rect.origin.y;
self.view.transform = CGAffineTransformMakeTranslation(0, - ty);
}];
}
UITextField的编辑事件的监听
- 通过UIControl的addTarget方法
[textField addTarget:target action:@selector(editingDidBegin) forControlEvents:UIControlEventEditingDidBegin];
[textField addTarget:target action:@selector(editingDidEnd) forControlEvents:UIControlEventEditingDidEnd];
- 通过代理
- (void)textFieldDidBeginEditing:(UITextField *)textField{
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
}
- 通过通知
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(beginEditing)
name:UITextFieldTextDidBeginEditingNotification
object:textField];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(endEditing)
name:UITextFieldTextDidEndEditingNotification
object:textField];
- (void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- 重写UITextField的
becomeFirstResponder
和resignFirstResponder
方法
//调用时刻 : 成为第一响应者(开始编辑\弹出键盘\获得焦点)
- (BOOL)becomeFirstResponder{
return [super becomeFirstResponder];
}
//调用时刻 : 不做第一响应者(结束编辑\退出键盘\失去焦点)
- (BOOL)resignFirstResponder{
return [super resignFirstResponder];
}
UITextField的常见需求
- 更改光标的颜色
textField.tintColor = [UIColor whiteColor];
- 设置占位文字:设置
placeholder
或者attributedPlaceholder
- 自定义占位文字颜色
- 使用
attributedPlaceholder
进行设置NSMutableDictionary *attributes = [NSMutableDictionary dictionary]; attributes[NSForegroundColorAttributeName] = [UIColor whiteColor]; self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Placeholder" attributes:attributes];
- 重写drawPlaceholderInRect方法
//前提是先设置placeholder占位文字 否则这个方法不会走 - (void)drawPlaceholderInRect:(CGRect)rect{ // 文字属性 NSMutableDictionary *attrs = [NSMutableDictionary dictionary]; attrs[NSForegroundColorAttributeName] = [UIColor whiteColor]; attrs[NSFontAttributeName] = self.font; CGPoint placeholderPoint = CGPointMake(0, (rect.size.height - self.font.lineHeight) * 0.5); [self.placeholder drawAtPoint:placeholderPoint withAttributes:attrs]; // 画出占位文字 // CGRect placeholderRect; // placeholderRect.size.width = rect.size.width; // placeholderRect.size.height = self.font.lineHeight; // placeholderRect.origin.x = 0; // placeholderRect.origin.y = (rect.size.height - self.font.lineHeight) * 0.5; // [self.placeholder drawInRect:placeholderRect withAttributes:attrs]; }
- 修改内部占位文字Label的文字颜色
[textField setValue:[UIColor grayColor] forKeyPath:@"placeholderLabel.textColor"];
- 在有多个textfield的时候在聚焦的时候占位文字颜色是一种颜色,非聚焦的时候是另一种颜色:实现方式是在成为第一响应者的时候设置一次占位文字颜色,在市区第一响应者的时候设置一次占位文字颜色