您的位置:首页 > 产品设计 > UI/UE

UITextField

2015-08-09 11:44 519 查看
// 文本输入框
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];
// 事件路由  事件分发

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: