UITextView与UITextField功能类似,也是字符输入的视图控件。
区别在于:
1、UITextView是多行字符输入,可通过回车键进行换行输入
2、也可以设置固定高度的范围,输入多行字符,然后通过上下滚动显示输入的字符
3、无左,或右间距视图
4、无清除按钮视图
- UITextView *textview001 = [[UITextView alloc] initWithFrame:CGRectMake(10.0, 50.0, 300.0, 80.0)];
- // view的属性
- // 添加到父视图
- [self.view addSubview:textview001];
- // 背景颜色
- textview001.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
- // 圆角设置
- textview001.layer.cornerRadius = 5.0;
- textview001.layer.masksToBounds = YES;
- // 边框设置
- textview001.layer.borderWidth = 1.0;
- textview001.layer.borderColor = [UIColor blackColor].CGColor;
- // 字体属性设置
- textview001.textColor = [UIColor brownColor];
- textview001.textAlignment = NSTextAlignmentRight;
- textview001.font = [UIFont systemFontOfSize:12.0];
- // 键盘类型
- textview001.keyboardType = UIKeyboardTypeASCIICapable;
- // 回车键类型
- textview001.returnKeyType = UIReturnKeyNext;
- // 回车键响应条件,有输入时才能响应,默认为NO,即没有限制
- textview001.enablesReturnKeyAutomatically = NO;
- // 改变输入源视图,默认是键盘
- UIImageView *inputImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, CGRectGetWidth(self.view.bounds), 100.0)];
- inputImageView.image = [UIImage imageNamed:@"inputImage"];
- textview001.inputView = inputImageView;
- // 改变键盘上方的子视图
- UIButton *accessoryButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0, 0.0, CGRectGetWidth(self.view.bounds), 40.0)];
- accessoryButton.backgroundColor = [UIColor redColor];
- [accessoryButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
- [accessoryButton setTitleColor:[UIColor yellowColor] forState:UIControlStateHighlighted];
- [accessoryButton setTitle:@"隐藏键盘" forState:UIControlStateNormal];
- [accessoryButton addTarget:self action:@selector(hiddionKeyboard) forControlEvents:UIControlEventTouchUpInside];
- textview001.inputAccessoryView = accessoryButton;
- // 隐藏键盘的按钮方法
- - (void)hiddionKeyboard
- {
- // 方法1
- // [textView endEditing:YES];
- // 方法2
- // [textView resignFirstResponder];
- // 方法3
- // [self.view endEditing:YES];
- // 方法4
- [[UIApplication sharedApplication].keyWindow endEditing:YES];
- }
- /*
- 设置代理
- 1、设置UITextView代理方法的响应者
- 2、添加协议
- 3、实现代理方法
- */
- textview001.delegate = self;
- // 添加协议
- @interface ViewController () <UITextViewDelegate>
- @end
- // 实现代理方法UITextViewDelegate
- - (BOOL)textViewShouldBeginEditing:(UITextView *)textView
- {
- // 即将开始编辑
- NSLog(@"即将开始编辑");
- return YES;
- }
- - (BOOL)textViewShouldEndEditing:(UITextView *)textView
- {
- // 即将结束编辑
- NSLog(@"即将结束编辑");
- return YES;
- }
- - (void)textViewDidBeginEditing:(UITextView *)textView
- {
- // 已经开始编辑
- NSLog(@"已经开始编辑");
- }
- - (void)textViewDidEndEditing:(UITextView *)textView
- {
- // 已经结束编辑
- NSLog(@"已经结束编辑");
- }
- - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
- {
- // 正在编辑
- NSString *string = [textView.text stringByReplacingCharactersInRange:range withString:text]; // NSString *string = textView.text;
- NSLog(@"正在编辑 %@", string);
- /*
- 通常用途
- 1、判断回车键,结束编辑
- 2、限制规定字符的输入
- 3、限制长度字符的输入
- */
- if ([text isEqualToString:@"\n"])
- {
- // 结束编辑,即隐藏键盘
- [self hiddionKeyboard];
- return NO; // 表示不能输入,反之则可输入
- }
- return YES;
- }
- - (void)textViewDidChange:(UITextView *)textView
- {
- // 正在编辑的改变
- NSLog(@"正在编辑的改变");
- }