===================================
UITextView
===================================
1.UITextView常用属性
- (void)viewDidLoad {
[super viewDidLoad];
//天生就会滚动,就是UIScrollView的子类
//ViewController试图控制器一旦检测到自己是在NavigationController中,就会把可以滚动的视图留出一个导航栏高度的位置,因为会滚动的视图通常被设计为全屏使用。反之如果没被装在NavigationController中系统不会给你留白。
//解决方案:
self.automaticallyAdjustsScrollViewInsets = NO;
UITextView * tv = [[UITextView alloc]initWithFrame:CGRectMake(10, 100, 300, 100)];
tv.backgroundColor = [UIColor orangeColor];
//设置字体
tv.font = [UIFont systemFontOfSize:30];
//设置字体颜色
tv.textColor = [UIColor blueColor];
//设置文字对齐方式
tv.textAlignment = NSTextAlignmentLeft;
//设置是否滚动
tv.scrollEnabled = YES;
//设置键盘色彩
tv.keyboardAppearance = UIKeyboardAppearanceAlert;
//设置键盘样式
tv.keyboardType = UIKeyboardTypeDefault;
//设置return按键的样式
tv.returnKeyType = UIReturnKeyDone;
//设置是否自动大小写
tv.autocapitalizationType = UITextAutocapitalizationTypeNone;
//设置是否自动纠错
tv.autocorrectionType = UITextAutocorrectionTypeNo;
//设置自定义键盘
UIView * keyView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 300)];
keyView.backgroundColor = [UIColor greenColor];
//tv.inputView = keyView;
//设置二级键盘
tv.inputAccessoryView = nil;
//设置禁止编辑
tv.editable = YES;
//设置文本内容
tv.text = @"真是全都忘了";
//设置不能进行交互
tv.userInteractionEnabled = YES;
//控制器没有tag,只有UIView子类有
tv.tag = 100;
//设置代理
tv.delegate = self;
[self.view addSubview:tv];
self.view.backgroundColor = [UIColor whiteColor];
}
2.UITextView常用代理方法
#pragma mark - 实现代理
-(BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
NSLog(@"将要开始编辑,返回YES允许编辑,NO不允许");
return YES;
}
-(BOOL)textViewShouldEndEditing:(UITextView *)textView
{
NSLog(@"将要结束编辑,返回YES允许,NO不允许");
return YES;
}
-(void)textViewDidBeginEditing:(UITextView *)textView
{ //焦点不在tv身上
NSLog(@"已经开始编辑");
}
-(void)textViewDidEndEditing:(UITextView *)textView
{
NSLog(@"已经结束编辑");
}
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
NSLog(@"当文本内容将要发生改变,参考UITextField");
return YES;
}
-(void)textViewDidChange:(UITextView *)textView
{
NSLog(@"当文本内容已经改变");
}
-(void)textViewDidChangeSelection:(UITextView *)textView
{
NSLog(@"选中的内容发生改变");
//获取选中文本的范围
NSRange range = textView.selectedRange;
//把选中文本得到
NSString * str = [textView.text substringWithRange:range];
NSLog(@"您选中了%@",str);
}