UITextView 比较常用于‘反馈’‘意见’这样的页面中,没有提示语,所以常用于自定义方式来创建提示语,比如放一个 Label 或者自己画一个继承自 UIView 的页面
一、UITextView 的创建和初始化
1、UITextView 的创建
用这种方式去创建一个 UITextView
@property (nonatomic, strong) UITextView *textView;
2、UITextView 的创建
用这种方式去创建一个 UITextView
self.textView = [[UITextView alloc] initWithFrame:CGRectMake(50, 100, self.view.bounds.size.width-100, self.view.bounds.size.height-200)];
二、UITextView 属性
1、text
text ---- 显示的文本内容(默认值一般不设)
self.textView.text = @"默认显示的文本内容";
2、font
font ---- 文字大小
self.textView.font = [UIFont systemFontOfSize:15];
3、textColor
textColor ---- 文字颜色
self.textView.textColor = [UIColor blackColor];
4、textAlignment
textAlignment ---- 文字对齐方式
self.textView.textAlignment = NSTextAlignmentLeft;
5、selectedRange
selectedRange ---- 选中的文本区域
self.textView.selectedRange = NSMakeRange(0, 2);
6、editable
editable ---- 文本是否可以编辑
self.textView.editable = YES;
7、selectable
selectable ---- 文本是否可以选择
self.textView.selectable = YES;
8、allowsEditingTextAttributes
allowsEditingTextAttributes ---- 是否允许编辑文本属性
self.textView.allowsEditingTextAttributes = YES;
9、attributedText
attributedText ---- 设置文本属性,填的是 NSMutableAttributedString 类型的,就不具体暂时了,详见UILabel 中的富文本内容
self.textView.attributedText = [];
10、inputView
inputView ---- 输入视图 就是编辑输入时会出现的视图
UIView *view = [[UIView alloc] initWithFrame:CGRectMake( 0, 0, self.view.bounds.size.width, 50)];
view.backgroundColor = [UIColor orangeColor];
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(50, 10, view.bounds.size.width-100, view.bounds.size.height-20)];
view1.backgroundColor = [UIColor whiteColor];
[view addSubview:view1];
self.textView.inputView = view;
11、inputAccessoryView
inputAccessoryView ---- 输入域切换视图 带键盘一起弹出,与上属性类似
UIView *view = [[UIView alloc] initWithFrame:CGRectMake( 0, 0, self.view.bounds.size.width, 50)];
view.backgroundColor = [UIColor orangeColor];
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(50, 10, view.bounds.size.width-100, view.bounds.size.height-20)];
view1.backgroundColor = [UIColor whiteColor];
[view addSubview:view1];
self.textView.inputAccessoryView = view;
12、textContainer
textContainer ---- 文本容器,(我暂时也不是很了解,没有用到过)
self.textView.textContainer = [[NSTextContainer alloc] init];
13、textContainerInset
textContainerInset ---- 文本边距,就是文本显示距离左右的偏移
self.textView.textContainerInset = UIEdgeInsetsMake(20, 20, 0, 0);
14、layoutManager
layoutManager ---- 文本布局管理者(我也不是很清楚,暂时还没有用到)
15、textStorage
textStorage ---- 文本保存实例(我也不是很清楚,暂时还没有用到)
三、协议(UITextViewDelegate)
1、点击进入文本将要开始编辑时触发的方法
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
NSLog(@"将要开始编辑");
return YES;
}
2、离开文本将要结束编辑时触发的方法
- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
NSLog(@"将要结束编辑");
return YES;
}
3、文本已经开始编辑时触发的方法
- (void)textViewDidBeginEditing:(UITextView *)textView
{
NSLog(@"已经开始编辑");
}
4、文本已经结束编辑时触发的方法
- (void)textViewDidEndEditing:(UITextView *)textView
{
NSLog(@"已经结束编辑");
}
5、文本内容将要发生改变时触发的方法
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
NSLog(@"文本内容将要发生改变");
return YES;
}
6、文本内容已经发生改变时触发的方法
- (void)textViewDidChange:(UITextView *)textView
{
NSLog(@"文本内容已经发生改变");
}
7、文本内容已经选中时触发的方法
- (void)textViewDidChangeSelection:(UITextView *)textView
{
NSLog(@"文本内容已经选中");
}
四、简单页面展示
五、UITextView 小结
UITextView 与 UITextField 比较类似,但是前者比较趋向于多行类型的文本输入框,类似于‘反馈‘这样的情况,而后者比较适合于登录界面。
UITextView 的属性协议,基本上已经写了
谢谢观看!如果有什么错误,请帮忙指出!谢谢!!!