UITextView类继承自UIScrollView,所以它拥有UIScrollView类的属性和方法,支持上下滑动,因此适合长文本的输入。
1.UITextView的属性介绍
UITextView与UITextField的属性大同小异
// 文字内容。
@property(null_resettable, nonatomic, copy) NSString *text;
// UITextView的背景颜色。
@property(nullable, nonatomic, copy) UIColor *backgroundColor UI_APPERANCE_SELECTOR;
// 文字字体颜色。
@property(nullable, nonatomic, strong) UIColor *textColor;
// 文本对齐方式。
@property(nonatomic) NSTextAlignment textAlignment;
// 文本框内边距设置。
@property(nonatomic, assign) UIEdgeInsets textContainerInsets;
// 当文字超过视图的边框时是否允许滑动。
@property(nonatomic, getter=isScrollEnabled) BOOL scrollEnabled;
// 选中文字范围。
@property(nonatomic) NSRange selectedRange;
-(void)viewDidLoad {
[super viewDidLoad];
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];
textView.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2, 200);
textView.backgroundColor = [UIColor lightGrayColor];
textView.textAlignment = NSTextAlignmentCenter;
textView.scrollEnabled = YES;
textView.textColor = [UIColor redColor];
textView.keyboardType = UIKwyboardTypwNamePhonePad;
textView.returnKeyType = UIReturnKeyDone;
textView.text = @"textView";
textView.font = [UIFont systemFontOfSize:20.0];
textView.contentInset = UIEdgeInsetdMake(10.0f, 10.0f, 10.0f, 10.0f,);
[self.view addSubview:textView];
}
2 UITextView的代理方法
UITextViewDelegate代理协议中如下几个常用的代理是比较常用的,需要优先掌握。
// UITextView对象开始编辑时调用。
-(void)textViewDidBeginEditing:(UITextView *)textView;
// UITextView对象结束编辑时调用。
-(void)textViewDidEndEditing:(UITextView *)textView;
// UITextView的内容和属性变化时调用。
-(void)textViewDidChange:(UITextView *)textView;
// 是否允许开始编辑。当返回值为NO时,UITextView中的内容不能修改。
-(BOOL)textViewShouldBeginEditing:(UITextView *)textView;
// 是否允许结束编辑。当返回值为NO时,UITextView不能结束编辑状态。
-(BOOL)textViewShouldEndEditing:(UITextView *)textView;