UITextView的使用

UITextViewUITextField功能类似,也是字符输入的视图控件。

区别在于:

1UITextView是多行字符输入,可通过回车键进行换行输入

2、也可以设置固定高度的范围,输入多行字符,然后通过上下滚动显示输入的字符

3、无左,或右间距视图

4、无清除按钮视图


[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. UITextView *textview001 = [[UITextView alloc] initWithFrame:CGRectMake(10.050.0300.080.0)];  

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // view的属性  
  2. // 添加到父视图  
  3. [self.view addSubview:textview001];  
  4. // 背景颜色  
  5. textview001.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];  
  6. // 圆角设置  
  7. textview001.layer.cornerRadius = 5.0;  
  8. textview001.layer.masksToBounds = YES;  
  9. // 边框设置  
  10. textview001.layer.borderWidth = 1.0;  
  11. textview001.layer.borderColor = [UIColor blackColor].CGColor;  
  12. // 字体属性设置  
  13. textview001.textColor = [UIColor brownColor];  
  14. textview001.textAlignment = NSTextAlignmentRight;  
  15. textview001.font = [UIFont systemFontOfSize:12.0];  


[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 键盘类型  
  2. textview001.keyboardType = UIKeyboardTypeASCIICapable;  
  3. // 回车键类型  
  4. textview001.returnKeyType = UIReturnKeyNext;  
  5. // 回车键响应条件,有输入时才能响应,默认为NO,即没有限制  
  6. textview001.enablesReturnKeyAutomatically = NO;  


[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 改变输入源视图,默认是键盘  
  2. UIImageView *inputImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.00.0, CGRectGetWidth(self.view.bounds), 100.0)];  
  3. inputImageView.image = [UIImage imageNamed:@"inputImage"];  
  4. textview001.inputView = inputImageView;  

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 改变键盘上方的子视图  
  2. UIButton *accessoryButton = [[UIButton alloc] initWithFrame:CGRectMake(0.00.0, CGRectGetWidth(self.view.bounds), 40.0)];  
  3. accessoryButton.backgroundColor = [UIColor redColor];  
  4. [accessoryButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];  
  5. [accessoryButton setTitleColor:[UIColor yellowColor] forState:UIControlStateHighlighted];  
  6. [accessoryButton setTitle:@"隐藏键盘" forState:UIControlStateNormal];  
  7. [accessoryButton addTarget:self action:@selector(hiddionKeyboard) forControlEvents:UIControlEventTouchUpInside];  
  8. textview001.inputAccessoryView = accessoryButton;  
[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 隐藏键盘的按钮方法  
  2. - (void)hiddionKeyboard  
  3. {  
  4.     // 方法1  
  5. //        [textView endEditing:YES];  
  6.     // 方法2  
  7. //        [textView resignFirstResponder];  
  8.     // 方法3  
  9. //        [self.view endEditing:YES];  
  10.     // 方法4  
  11.     [[UIApplication sharedApplication].keyWindow endEditing:YES];  
  12. }  

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /* 
  2. 设置代理 
  3. 1、设置UITextView代理方法的响应者 
  4. 2、添加协议 
  5. 3、实现代理方法 
  6. */  
  7. textview001.delegate = self;  
[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 添加协议  
  2. @interface ViewController () <UITextViewDelegate>  
  3.   
  4. @end  
  5.   
  6. // 实现代理方法UITextViewDelegate  
  7. - (BOOL)textViewShouldBeginEditing:(UITextView *)textView  
  8. {  
  9.     // 即将开始编辑  
  10.     NSLog(@"即将开始编辑");  
  11.       
  12.     return YES;  
  13. }  
  14.   
  15. - (BOOL)textViewShouldEndEditing:(UITextView *)textView  
  16. {  
  17.     // 即将结束编辑  
  18.     NSLog(@"即将结束编辑");  
  19.       
  20.     return YES;  
  21. }  
  22.   
  23. - (void)textViewDidBeginEditing:(UITextView *)textView  
  24. {  
  25.     // 已经开始编辑  
  26.     NSLog(@"已经开始编辑");  
  27. }  
  28.   
  29. - (void)textViewDidEndEditing:(UITextView *)textView  
  30. {  
  31.     // 已经结束编辑  
  32.     NSLog(@"已经结束编辑");  
  33. }  
  34.   
  35. - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text  
  36. {  
  37.     // 正在编辑  
  38.     NSString *string = [textView.text stringByReplacingCharactersInRange:range withString:text]; // NSString *string = textView.text;  
  39.     NSLog(@"正在编辑 %@", string);  
  40.       
  41.     /* 
  42.      通常用途 
  43.      1、判断回车键,结束编辑 
  44.      2、限制规定字符的输入 
  45.      3、限制长度字符的输入 
  46.     */  
  47.       
  48.     if ([text isEqualToString:@"\n"])  
  49.     {  
  50.         // 结束编辑,即隐藏键盘  
  51.         [self hiddionKeyboard];  
  52.           
  53.         return NO// 表示不能输入,反之则可输入  
  54.     }  
  55.       
  56.     return YES;  
  57. }  
  58.   
  59. - (void)textViewDidChange:(UITextView *)textView  
  60. {  
  61.     // 正在编辑的改变  
  62.     NSLog(@"正在编辑的改变");  
  63. }  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值