UITextField的使用
来源:互联网 发布:apm2.8源码编译 编辑:程序博客网 时间:2024/05/19 21:01
UITextField是输入字符的视图控件
1 是UIView的子类,具有与view一样的属性
2 字符输入只能是单行输入,不能换行,也不能多行输入
3 与自己特有的属性
4 textfield通常要设置其代理,并实现相应的代理方法
5 主要使用场景,如:登录页面中输入帐号密码,或输入手机号,或输入昵称等方面
6 使用注意事项:
(1)输入键盘的显示,以及隐藏;
(2)输入时避免输入框被键盘遮挡;
-
- UITextField *textfield001 = [[UITextField alloc] initWithFrame:CGRectMake(10.0, 50.0, 200.0, 30.0)];
-
-
- [self.view addSubview:textfield001];
-
-
- textfield001.backgroundColor = [UIColor redColor];
-
-
- textfield001.layer.cornerRadius = 10.0;
- textfield001.layer.masksToBounds = YES;
- textfield001.layer.borderColor = [UIColor yellowColor].CGColor;
- textfield001.layer.borderWidth = 1.0;
-
-
- textfield001.placeholder = @"我是单行字符输入框";
-
- textfield001.textColor = [UIColor yellowColor];
-
- textfield001.textAlignment = NSTextAlignmentRight;
-
- textfield001.tintColor = [UIColor greenColor];
-
- textfield001.font = [UIFont systemFontOfSize:10.0];
-
-
- UIImageView *leftImage = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 30.0, 30.0)];
- leftImage.contentMode = UIViewContentModeScaleAspectFit;
- leftImage.image = [UIImage imageNamed:@"leftImage"];
- textfield001.leftView = leftImage;
- textfield001.leftViewMode = UITextFieldViewModeAlways;
-
-
-
-
-
-
-
- textfield001.clearButtonMode = UITextFieldViewModeWhileEditing;
-
- textfield001.keyboardType = UIKeyboardTypeDefault;
-
- textfield001.returnKeyType = UIReturnKeySend;
-
- textfield001.enablesReturnKeyAutomatically = NO;
-
- textfield001.secureTextEntry = NO;
-
-
-
-
-
-
- textfield001.delegate = self;
-
- @interface ViewController () <UITextFieldDelegate>
-
- @end
-
-
-
- UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
- button.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
- button.frame = CGRectMake(0.0, 0.0, CGRectGetWidth(self.view.bounds), 40.0);
- [button setTitle:@"隐藏键盘" forState:UIControlStateNormal];
- [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
- [button setTitleColor:[UIColor yellowColor] forState:UIControlStateHighlighted];
- [button addTarget:self action:@selector(hiddenKeyboard) forControlEvents:UIControlEventTouchUpInside];
- textfield001.inputAccessoryView = button;
-
-
-
- - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
- {
- NSLog(@"即将开始编辑");
- return YES;
- }
-
-
- - (void)textFieldDidBeginEditing:(UITextField *)textField
- {
- NSLog(@"已经开始编辑");
- }
-
-
- - (BOOL)textFieldShouldEndEditing:(UITextField *)textField
- {
- NSLog(@"即将结束编辑");
- return YES;
- }
-
-
- - (void)textFieldDidEndEditing:(UITextField *)textField
- {
- NSLog(@"已经结束编辑");
- }
-
-
- - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
- {
- NSLog(@"正在编辑 = %@", textField.text);
-
-
-
-
-
-
-
-
- return YES;
- }
-
-
- - (BOOL)textFieldShouldClear:(UITextField *)textField
- {
- NSLog(@"点击了清除键");
-
- return YES;
- }
-
-
- - (BOOL)textFieldShouldReturn:(UITextField *)textField
- {
- NSLog(@"点击了回车键");
-
-
-
-
-
- [textField endEditing:YES];
-
- return YES;
- }
- - (void)hiddenKeyboard
- {
-
- [self.view endEditing:YES];
- }
0 0