////别忘在 .h 中写代理 <UITextViewDelegate> ///UILabel 显示的文本只读,无法编辑,可以根据文字个数自动换行; ///UITextField 可编辑本文,但是无法换行,只能在一行显示;当点击键盘上的return时会收到一个事件做一些事情。 ////UITextView 可编辑文本,提供换行功能。 UITextView* textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 10, 300, 100)]; textView.backgroundColor = [UIColor grayColor]; //文本 textView.text = @"aa"; //字体 textView.font = [UIFont boldSystemFontOfSize:20.0]; //对齐 textView.textAlignment = NSTextAlignmentCenter; //字体颜色 textView.textColor = [UIColor redColor]; //允许编辑 textView.editable = YES; //用户交互 /////////////////////若想有滚动条 不能交互 上为No,下为Yes textView.userInteractionEnabled = YES; /// //自定义键盘 //textView.inputView = view; //textView.inputAccessoryView = view; textView.delegate = self; [self.view addSubview:textView]; //[textView release]; //////////事件 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ //判断类型,如果是UITextView类型,收起键盘 for (UIView* view in self.view.subviews) { if ([view isKindOfClass:[UITextView class]]) { UITextView* tv = (UITextView*)view; [tv resignFirstResponder]; } } } - (BOOL)textViewShouldBeginEditing:(UITextView *)textView{ return YES; } - (BOOL)textViewShouldEndEditing:(UITextView *)textView{ return YES; } - (void)textViewDidBeginEditing:(UITextView *)textView{ NSLog(@"开始编辑"); } - (void)textViewDidEndEditing:(UITextView *)textView{ NSLog(@"结束编辑"); } - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{ return YES; } - (void)textViewDidChange:(UITextView *)textView{ NSLog(@"已经修改"); } - (void)textViewDidChangeSelection:(UITextView *)textView{ NSLog(@"textViewDidChangeSelection"); }