UITextField

  
    // 文本输入框
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 440, 300, 30)];
    textField.tag = 101;
    // 设置边框风格
    textField.borderStyle = UITextBorderStyleLine;
    // 设置键盘风格
    textField.keyboardAppearance = UIKeyboardAppearanceDark;
    // 设置键盘样式
    textField.keyboardType = UIKeyboardTypeDefault;
    // 设置密文输入 secureTextEntry 安全文本入口
    textField.secureTextEntry = YES;
    // 设置默认文字
    textField.placeholder = @"Please input your password";
    // 设置清除按钮
    textField.clearButtonMode = UITextFieldViewModeAlways;
    // 设置最小输入值
    //    textField.minimumFontSize = 5.0;
    textField.rightViewMode =  UITextFieldViewModeAlways;
    textField.leftViewMode = UITextFieldViewModeAlways;
    // 设置return样式
    textField.returnKeyType = UIReturnKeyGoogle;
    // 设置代理
    textField.delegate = self; // 让self代替它做事
    [self.view addSubview:textField];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(10, 100, 150, 30);
    [button setTitle:@"Close the keyboard" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    button.backgroundColor = [UIColor lightGrayColor];
    
    // 订阅键盘升起和落下的系统通知
//    UIKeyboardWillShowNotification
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow) name:UIKeyboardWillShowNotification object:nil];
//    UIKeyboardWillHideNotification
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide) name:UIKeyboardWillHideNotification object:nil];
    // self为通知接收者,收到通知后,执行操作
    // 键盘升起和落下的系统通知
//    1. UIKeyboardWillShowNOtification
//    2. UIKeyboardWillHideNotification
    
    // 自定义弹出视图
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 230)];
    view.backgroundColor = [UIColor lightGrayColor];
//    textField.inputView = view;
    
}


#pragma  mark - NSNotification Action
// 当接收到通知,改变textField的frame,从而实现动态变化
- (void)keyboardWillShow {
    UITextField *textField = (UITextField *)[self.view viewWithTag:101];
    textField.frame = CGRectMake(10, 220, 300, 30);
}

- (void)keyboardWillHide {
    UITextField *textField = (UITextField *)[self.view viewWithTag:101];
    textField.frame = CGRectMake(10, 440, 300, 30);
}

#pragma mark - button Action
- (void)buttonAction {
    
    UITextField *textField = (UITextField *)[self.view viewWithTag:101];
    [textField resignFirstResponder];
}

#pragma mark - UITextField Delegate
// 是否可以进入编辑模式
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    return  YES;
}
// 是否结束编辑模式
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField  {
    return YES;
}
// 是否可以清除(清除按钮是否有效)      defalut is YES
- (BOOL)textFieldShouldClear:(UITextField *)textField {
    
//    [textField resignFirstResponder];
    return YES;
}
// 输入之后是否可以改变               default is YES
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    return YES;
}
// return是否可用
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    // 收起键盘,响应者,一个屏幕上会有第一响应者  firstResponder
    // 取消第一响应者 收起键盘 关闭光标
    
    [textField resignFirstResponder];
    
    /**
     *  1.点击return
     2.点击空白地方
     3.点击按钮
     4.滑动
     */
    return YES;
}
// 手指点击屏幕的函数
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
    UITextField *textField = (UITextField *)[self.view viewWithTag:101];
    [textField resignFirstResponder];
    // 事件路由  事件分发
    
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值