监听UITextField的内容变化,获取键盘的模式

方法1:通知中心

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(textFieldEditChanged:)
                                                 name:@"UITextFieldTextDidChangeNotification"
                                               object:_inputTextField];

UIKIT_EXTERN NSString *const UITextFieldTextDidBeginEditingNotification;
UIKIT_EXTERN NSString *const UITextFieldTextDidEndEditingNotification;
UIKIT_EXTERN NSString *const UITextFieldTextDidChangeNotification;

- (void)textFieldEditChanged:(NSNotification *)obj{
    UITextField *textField = (UITextField *)obj.object;
    NSString *toBeString = textField.text;
}

补充:获取键盘的模式

    NSString *lang = [[UITextInputMode currentInputMode] primaryLanguage]; // 键盘输入模式
    if ([lang isEqualToString:@":-Hans"]) {
        UITextRange *selectedRange = [textField markedTextRange];
        UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
        if (!position) {

        }else{
        // 中文以外的输入法

        }
    }

方法2:KVO监听

    // KVO增加监听
    [_couponTextField addTarget:self action:@selector(textFieldChangedFromTextField:) forControlEvents:UIControlEventEditingChanged];

补充:

#pragma mark - UITextFieldDelegate
// 开始编辑键盘弹出的时候将组件向上移动  4s
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    KTLog(@"%f",self.view.frame.size.height);
    if (iPhone4s) {
        if ([textField isEqual:_phoneNumTextField]) {
            [UIView animateWithDuration:0.5 animations:^{
                _tgView.frame = CGRectMake(SCREEN_WIDTH, -SCREEN_HEIGHT/5, SCREEN_WIDTH, SCREEN_HEIGHT);
            }];
        }
    }

}
// 结束编辑键盘消失后将组件移到原来的位置
- (void)textFieldDidEndEditing:(UITextField *)textField {
    if (iPhone4s) {
        if ([textField isEqual:_phoneNumTextField]) {
            [UIView animateWithDuration:0.5 animations:^{
                _tgView.frame = CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT-54);
            }];
        }
    }

}




你可能感兴趣的:(ios,UITextField,键盘模式)