UITextView與UITextField功能類似,也是字符輸入的視圖控件。
區別在於:
1、UITextView是多行字符輸入,可通過回車鍵進行換行輸入
2、也可以設置固定高度的范圍,輸入多行字符,然後通過上下滾動顯示輸入的字符
3、無左,或右間距視圖
4、無清除按鈕視圖
- UITextView*textview001=[[UITextViewalloc]initWithFrame:CGRectMake(10.0,50.0,300.0,80.0)];
- //view的屬性
- //添加到父視圖
- [self.viewaddSubview:textview001];
- //背景顏色
- textview001.backgroundColor=[UIColorcolorWithWhite:0.0alpha:0.3];
- //圓角設置
- textview001.layer.cornerRadius=5.0;
- textview001.layer.masksToBounds=YES;
- //邊框設置
- textview001.layer.borderWidth=1.0;
- textview001.layer.borderColor=[UIColorblackColor].CGColor;
- //字體屬性設置
- textview001.textColor=[UIColorbrownColor];
- textview001.textAlignment=NSTextAlignmentRight;
- textview001.font=[UIFontsystemFontOfSize:12.0];
- //鍵盤類型
- textview001.keyboardType=UIKeyboardTypeASCIICapable;
- //回車鍵類型
- textview001.returnKeyType=UIReturnKeyNext;
- //回車鍵響應條件,有輸入時才能響應,默認為NO,即沒有限制
- textview001.enablesReturnKeyAutomatically=NO;
- //改變輸入源視圖,默認是鍵盤
- UIImageView*inputImageView=[[UIImageViewalloc]initWithFrame:CGRectMake(0.0,0.0,CGRectGetWidth(self.view.bounds),100.0)];
- inputImageView.image=[UIImageimageNamed:@"inputImage"];
- textview001.inputView=inputImageView;
- //改變鍵盤上方的子視圖
- UIButton*accessoryButton=[[UIButtonalloc]initWithFrame:CGRectMake(0.0,0.0,CGRectGetWidth(self.view.bounds),40.0)];
- accessoryButton.backgroundColor=[UIColorredColor];
- [accessoryButtonsetTitleColor:[UIColorblackColor]forState:UIControlStateNormal];
- [accessoryButtonsetTitleColor:[UIColoryellowColor]forState:UIControlStateHighlighted];
- [accessoryButtonsetTitle:@"隱藏鍵盤"forState:UIControlStateNormal];
- [accessoryButtonaddTarget:selfaction:@selector(hiddionKeyboard)forControlEvents:UIControlEventTouchUpInside];
- textview001.inputAccessoryView=accessoryButton;
- //隱藏鍵盤的按鈕方法
- -(void)hiddionKeyboard
- {
- //方法1
- //[textViewendEditing:YES];
- //方法2
- //[textViewresignFirstResponder];
- //方法3
- //[self.viewendEditing:YES];
- //方法4
- [[UIApplicationsharedApplication].keyWindowendEditing:YES];
- }
- /*
-
- 設置代理
- 1、設置UITextView代理方法的響應者
- 2、添加協議
- 3、實現代理方法
- */
- textview001.delegate=self
- //添加協議
- @interfaceViewController()
-
- @end
-
- //實現代理方法UITextViewDelegate
- -(BOOL)textViewShouldBeginEditing:(UITextView*)textView
- {
- //即將開始編輯
- NSLog(@"即將開始編輯");
-
- returnYES;
- }
-
- -(BOOL)textViewShouldEndEditing:(UITextView*)textView
- {
- //即將結束編輯
- NSLog(@"即將結束編輯");
-
- returnYES;
- }
-
- -(void)textViewDidBeginEditing:(UITextView*)textView
- {
- //已經開始編輯
- NSLog(@"已經開始編輯");
- }
-
- -(void)textViewDidEndEditing:(UITextView*)textView
- {
- //已經結束編輯
- NSLog(@"已經結束編輯");
- }
-
- -(BOOL)textView:(UITextView*)textViewshouldChangeTextInRange:(NSRange)rangereplacementText:(NSString*)text
- {
- //正在編輯
- NSString*string=[textView.textstringByReplacingCharactersInRange:rangewithString:text];//NSString*string=textView.text;
- NSLog(@"正在編輯%@",string);
-
- /*
- 通常用途
- 1、判斷回車鍵,結束編輯
- 2、限制規定字符的輸入
- 3、限制長度字符的輸入
- */
-
- if([textisEqualToString:@"\n"])
- {
- //結束編輯,即隱藏鍵盤
- [selfhiddionKeyboard];
-
- returnNO;//表示不能輸入,反之則可輸入
- }
-
- returnYES;
- }
-
- -(void)textViewDidChange:(UITextView*)textView
- {
- //正在編輯的改變
- NSLog(@"正在編輯的改變");
- }