UITextField的基本使用

2016-08-09  本文已影响50人  CoderRH

UITextField的基本使用

    // 设置文本框左边的内容
    UIView *leftView = [[UIView alloc] init];
    leftView.frame = CGRectMake(0, 0, 10, 0);
    textField.leftView = leftView;
    //模式设置为一直显示
    textField.leftViewMode = UITextFieldViewModeAlways;
键盘状态改变的时候,系统会发出一些特定的通知
UIKeyboardWillShowNotification // 键盘即将显示
UIKeyboardDidShowNotification // 键盘显示完毕
UIKeyboardWillHideNotification // 键盘即将隐藏
UIKeyboardDidHideNotification // 键盘隐藏完毕
UIKeyboardWillChangeFrameNotification//键盘的位置尺寸即将发生改变
UIKeyboardDidChangeFrameNotification // 键盘的位置尺寸改变完毕
系统发出键盘通知时,会附带一下跟键盘有关的额外信息(字典),字典常见的key如下:
UIKeyboardFrameBeginUserInfoKey // 键盘刚开始的frame
UIKeyboardFrameEndUserInfoKey // 键盘最终的frame(动画执行完毕后)
UIKeyboardAnimationDurationUserInfoKey // 键盘动画的时间
UIKeyboardAnimationCurveUserInfoKey // 键盘动画的执行节奏(快慢)

键盘弹出和消失的时候屏幕的改变

   - (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的编辑事件的监听

[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];
}

 //调用时刻 : 成为第一响应者(开始编辑\弹出键盘\获得焦点)
- (BOOL)becomeFirstResponder{
    return [super becomeFirstResponder];
}

//调用时刻 : 不做第一响应者(结束编辑\退出键盘\失去焦点)
- (BOOL)resignFirstResponder{
 return [super resignFirstResponder];
}

UITextField的常见需求

[textField setValue:[UIColor grayColor] forKeyPath:@"placeholderLabel.textColor"];
上一篇 下一篇

猜你喜欢

热点阅读