【转】UITextView的使用详解
//初始化并定义大小
UITextView *textview = [[UITextView alloc] initWithFrame:CGRectMake(20, 10, 280, 30)];
textview.backgroundColor=[UIColor whiteColor]; //背景色
textview.scrollEnabled = NO; //当文字超过视图的边框时是否允许滑动,默认为“YES”
textview.editable = YES; //是否允许编辑内容,默认为“YES”
textview.delegate = self; //设置代理方法的实现类
textview.font=[UIFont fontWithName:@"Arial" size:18.0]; //设置字体名字和字体大小;
textview.returnKeyType = UIReturnKeyDefault;//return键的类型
textview.keyboardType = UIKeyboardTypeDefault;//键盘类型
textview.textAlignment = NSTextAlignmentLeft; //文本显示的位置默认为居左
textview.dataDetectorTypes = UIDataDetectorTypeAll; //显示数据类型的连接模式(如电话号码、网址、地址等)
textview.textColor = [UIColor blackColor];
textview.text = @"UITextView详解";//设置显示的文本内容
[self.view addSubview:textview];
UITextView的代理方法如下:
//将要开始编辑
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView;
//将要结束编辑
- (BOOL)textViewShouldEndEditing:(UITextView *)textView;
//开始编辑
- (void)textViewDidBeginEditing:(UITextView *)textView;
//结束编辑
- (void)textViewDidEndEditing:(UITextView *)textView;
//内容将要发生改变编辑
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
//内容发生改变编辑
- (void)textViewDidChange:(UITextView *)textView;
//焦点发生改变
- (void)textViewDidChangeSelection:(UITextView *)textView;
有时候我们要控件自适应输入的文本的内容的高度,只要在textViewDidChange的代理方法中加入调整控件大小的代理即可
- (void)textViewDidChange:(UITextView *)textView{
//计算文本的高度
CGSize constraintSize;
constraintSize.width = textView.frame.size.width-16;
constraintSize.height = MAXFLOAT;
CGSize sizeFrame =[textView.text sizeWithFont:textView.font
constrainedToSize:constraintSize
lineBreakMode:UILineBreakModeWordWrap];
//重新调整textView的高度
textView.frame = CGRectMake(textView.frame.origin.x,textView.frame.origin.y,textView.frame.size.width,sizeFrame.height+5);
}
控制输入文字的长度和内容,可通调用以下代理方法实现
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if (range.location>=100)
{
//控制输入文本的长度
return NO;
}
if ([text isEqualToString:@"\n"]) {
//禁止输入换行
return NO;
}
else
{
return YES;
}
}
UITextView退出键盘的几种方式
因为iphone的软键盘没有自带的退键盘键,所以要实现退出键盘需要自己实现,有如下几种方式:
1)如果你程序是有导航条的,可以在导航条上面加多一个Done的按钮,用来退出键盘,当然要先实UITextViewDelegate。
- (void)textViewDidBeginEditing:(UITextView *)textView {
UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:@selector(dismissKeyBoard)];
self.navigationItem.rightBarButtonItem = done;
[done release];
done = nil;
}
- (void)textViewDidEndEditing:(UITextView *)textView {
self.navigationItem.rightBarButtonItem = nil;
}
- (void)dismissKeyBoard {
[self.textView resignFirstResponder];
}
2)如果你的textview里不用回车键,可以把回车键当做退出键盘的响应键。
代码如下:
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if ([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
return NO;
}
return YES;
}
3)还有你也可以自定义其他加载键盘上面用来退出,比如在弹出的键盘上面加一个view来放置退出键盘的Done按钮。
代码如下:
UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
[topView setBarStyle:UIBarStyleBlack];
UIBarButtonItem *btnSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:self
action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]initWithTitle:@"Done"
style:UIBarButtonItemStyleDone
target:self
action:@selector(dismissKeyBoard)];
NSArray * buttonsArray = @[btnSpace, doneButton];;
[doneButton release];
[btnSpace release];
[topView setItems:buttonsArray];
[textView setInputAccessoryView:topView];//当文本输入框加上topView
[topView release];
topView = nil;
-(IBAction)dismissKeyBoard
{
[tvTextView resignFirstResponder];
}