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]; // 事件路由 事件分发 }
相关文章推荐
- 系统签名 sharedUserId android.uid.system
- UILabel
- UIWindow
- Xcode5 创建模板和UIView 关联XIB
- UIButton
- JAVA GUI
- 实习小结——UIBarButtonItem和UINavigationBar的坑
- [置顶] Qt Quick之多个ListView控件间相互拖动及其拖动效果实现
- [leetcode] N-Queens II
- UIViewController——Handling View-Related Notifications(处理与视图相关的通知)
- UIView
- HDU1711Number Sequence(KMP)
- 545D. Queue
- UILable
- [leetcode] N-Queens
- js无法强制修改input type=file的value
- UiAutomator环境搭建及详细操作
- [置顶] Qt Quick之ListView单元的上下拖动功能实现
- iOS基础-UIKit框架-触摸事件-响应者链条
- [LeetCode] N-Queens II