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


一、UITextView

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

可以控制輸入文字的數量,較為常用

 #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; }

二、鍵盤回收與鍵盤遮擋輸入框

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

首先注冊通知簡體鍵盤彈出事件

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

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

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

再設置一個觸摸事件,觸摸空白處可以收回鍵盤

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

完整代碼:

#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,然后再resignFirst

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

 

轉載:http://www.jianshu.com/p/1bf3407b15f7


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM