iOS- UITextView與鍵盤迴收與鍵盤遮擋輸入框

1、UITextView

能夠實現多行輸入的文本框,基本屬性與UITextField類似,能夠輸入多行,能夠滾動。
UITextView還有個代理方式
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)textide

能夠控制輸入文字的數量,較爲經常使用post

#pragma mark UITextView的代理方法 //是否能夠開始編輯 - (BOOL)textViewShouldBeginEditing:(UITextView *)textView { NSLog(@"%s",__func__); return YES; } //是否能夠結束編輯 - (BOOL)textViewShouldEndEditing:(UITextView *)textView { NSLog(@"%s",__func__); return YES; } //已經開始編輯 - (void)textViewDidBeginEditing:(UITextView *)textView { NSLog(@"%s",__func__); } //已經結束編輯 - (void)textViewDidEndEditing:(UITextView *)textView { NSLog(@"%s",__func__); } //內容變化 - (void)textViewDidChange:(UITextView *)textView { NSLog(@"%s",__func__); } //光標變化 - (void)textViewDidChangeSelection:(UITextView *)textView { NSLog(@"%s",__func__); } //當前輸入的位置,當前輸入的文字,是否能夠繼續輸入 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { NSLog(@"length = %ld,location = %ld",range.length,range.location); NSLog(@"text = %@",text); return YES; } //下面這倆很差理解,大概是驗證url和文件名後綴的 //Asks the delegate if the specified text view should allow user interaction with the given URL in the given range of text. - (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange NS_AVAILABLE_IOS(7_0) { NSLog(@"url= %@",URL.host); NSLog(@"url touch"); return YES; } - (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange NS_AVAILABLE_IOS(7_0) { return YES; }

2、鍵盤迴收與鍵盤遮擋輸入框

使用輸入文本框時,常常出現一個問題,彈出的鍵盤擋住了文本框,這時能夠經過移動輸入框的位置來避免這樣的狀況。
思路比較簡單,註冊通知來監聽鍵盤的彈出和消失事件,再實現對應的方法,在鍵盤彈出或者消失的時候,改變本來視圖的frame
使視圖向上或者向下移動一個鍵盤的高度,鍵盤就不會遮擋住視圖了字體

首先註冊通知簡體鍵盤彈出事件動畫

//註冊通知,監聽鍵盤彈出事件 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardWillShowNotification object:nil]; //註冊通知,監聽鍵盤消失事件 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHidden) name:UIKeyboardDidHideNotification object:nil];

鍵盤一旦彈出或者消失就觸發方法對輸入框進行移動ui

// 鍵盤彈出時調用方法 -(void)keyboardDidShow:(NSNotification *)notification //鍵盤消失時調用 -(void)keyboardDidHidden

再設置一個觸摸事件,觸摸空白處能夠收回鍵盤url

//點擊屏幕空白處 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { //回收鍵盤,二者方式 //UITextView *textView = (UITextView*)[self.view viewWithTag:1001]; //[textView resignFirstResponder]; [self.view endEditing:YES]; NSLog(@"touch"); }

完整代碼:spa

#import "FirstViewController.h" @implementation FirstViewController -(void)viewDidLoad { [super viewDidLoad]; #pragma mark UITextView // UITextField: // 繼承UIControl,只能輸入一行,不能夠滾動,能夠設置提醒文字。 // 有return代理方法和clearButtonMode // UITextView: // 能輸入多行,能夠滾動,不能夠設置提醒文字。 UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 10, 280, 80)];//初始化 textView.backgroundColor=[UIColor colorWithRed:0.21 green:0.71 blue:0.51 alpha:0.5]; //背景色 textView.scrollEnabled = YES; //當文字超過視圖的邊框時是否容許滑動,默認爲「YES」 textView.editable = YES; //是否容許編輯內容,默認爲「YES」 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.delegate = self; //設置代理方法的實現類 textView.text = @"UITextView";//設置顯示的文本內容 [textView.layer setCornerRadius:10]; //設置圓角 textView.tag = 1001; //設置tag值 //添加鍵盤的監聽事件 //註冊通知,監聽鍵盤彈出事件 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil]; //註冊通知,監聽鍵盤消失事件 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHidden) name:UIKeyboardDidHideNotification object:nil]; //在view中添加一個子view,設置此子view的tag值爲1000,在此view上添加一個textView和一個發送按鈕, //以下圖;咱們要達到textView的鍵盤彈出時,整個View往上平移,鍵盤消失,view往下平移的效果,模擬發送短信的界面。 UIView *keyView = [[UIView alloc]initWithFrame:CGRectMake(0, 567, 375, 100)]; keyView.backgroundColor = [UIColor colorWithRed:0.2 green:0.3 blue:0.6 alpha:0.5]; keyView.tag = 1000; [keyView addSubview:textView]; [self.view addSubview:keyView]; } #pragma mark 實現監聽到鍵盤變化時的觸發的方法 // 鍵盤彈出時 -(void)keyboardDidShow:(NSNotification *)notification { //獲取鍵盤高度 NSValue *keyboardObject = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey]; NSLog(@"%@",keyboardObject); CGRect keyboardRect; [keyboardObject getValue:&keyboardRect]; //獲得鍵盤的高度 //CGRect keyboardRect = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue]; // 取得鍵盤的動畫時間,這樣能夠在視圖上移的時候更連貫 double duration = [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; NSLog(@"%f",duration); //調整放置有textView的view的位置 //設置動畫 [UIView beginAnimations:nil context:nil]; //定義動畫時間 [UIView setAnimationDuration:duration]; [UIView setAnimationDelay:0]; //設置view的frame,往上平移 [(UIView *)[self.view viewWithTag:1000] setFrame:CGRectMake(0, self.view.frame.size.height-keyboardRect.size.height-100, 375, 100)]; //提交動畫 [UIView commitAnimations]; } //鍵盤消失時 -(void)keyboardDidHidden { //定義動畫 //[UIView beginAnimations:nil context:nil]; // [UIView setAnimationDuration:0.25]; //設置view的frame,往下平移 [(UIView *)[self.view viewWithTag:1000] setFrame:CGRectMake(0, 567, 375, 100)]; // [UIView commitAnimations]; } //點擊屏幕空白處 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { //回收鍵盤,兩種方式 //UITextView *textView = (UITextView*)[self.view viewWithTag:1001]; //[textView resignFirstResponder]; [self.view endEditing:YES]; NSLog(@"touch"); } @end

效果圖代理

後面又一次要在tableView裏面加textView,很差找的textView去resignKeyBoard,就使用一個能夠獲得的textView 去becomeFirst,而後再resignFirstcode

還有直接得到第一響應者的方法
UIWindow keyWindow = [[UIApplication sharedApplication] keyWindow];
UIView 
firstResponder = [keyWindow performSelector:@selector(firstResponder)];
[firstResponder resignFirstResponder];orm

相關文章
相關標籤/搜索