UITextView 文本輸入框
////別忘在 .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"); }</pre>