iOS- 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];
相關推薦
iOS- UITextView與鍵盤迴收與鍵盤遮擋輸入框
可以實現多行輸入的文字框,基本屬性與UITextField相似,可以輸入多行,可以滾動。UITextView還有個代理方式- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementTex
鍵盤迴收,tableview上的cell判斷被鍵盤遮擋解決方案
點選之定義tableviewcell,彈出鍵盤,通過距離判斷解決鍵盤遮擋問題; 核心程式碼是如果tableview上的cell判斷被遮擋 可以通過改變contentInset和滾動tableview到指定cell來實現 #pragma mark -- table da
iOS解決鍵盤遮擋輸入框問題
導讀:UITextField(輸入框)獲取焦點後會彈出鍵盤,有時候鍵盤會遮擋住輸入框,影響使用者互動,所以需要在彈出鍵盤的時候將檢視上移至不會遮擋的位置。下面主要講述幾種常見解決方法。 一、彈出鍵盤時,將整個檢視上移:監聽鍵盤事件 //監聽鍵盤 //1、鍵盤彈出時
iOS 鍵盤遮擋輸入框解決方案
// 方法一 - (void)addNotification { [[NSNotificationCenterdefaultCenter] addObserver:selfselector:
[ios]TableView的Cell中有Textfield時放置鍵盤遮擋輸入框
tableview初始化時- (UITableView *)tableView { if (!_tableView) { UITableViewController* tvc=
H5移動端彈出鍵盤時遮擋輸入框
記一次,解決移動端文字框彈出鍵盤遮擋輸入框的方法: document.activeElement.scrollIntoViewIfNeeded(); 方法用來將不在瀏覽器視窗的可見區域內的元素滾動到瀏覽器視窗的可見區域。 如果該元素已經在瀏覽器視窗的可見區域內,則不會
WebView使用中的那些坑之軟鍵盤遮擋輸入框
一、測試環境: HuaWei U9508 4.2.2 二、問題: 在ViewPager的一個Fragment頁面中只有一個WebView,用於載入url,但是最下面的輸入框點選後被軟鍵盤遮擋
React-native鍵盤遮擋輸入框問題的解決
現在有一個更準確一點的做法是用一個View包裹住TextInput,然後通過該View的onLayout方法獲取該輸入框的y軸位置,再減去一個適當的高度去處理scrollview的滾動,如下所示: <View onLayout={this._downloadLa
解決軟鍵盤遮擋輸入框的問題
根據前人經驗總結普通Activity(不帶WebView),直接使用adjustpan或者adjustResize如果帶WebView:a) 如果非全屏模式,可以使用adjustResizeb) 如果是全屏模式,則使用AndroidBug5497Workaround進行處理。
js解決軟鍵盤遮擋輸入框問題
經驗須知 彈出軟鍵盤時: ios端$(‘body’).scrollTop()會改變android端$(window).height()會改變拉起鍵盤不是一瞬間,而是有一個緩動過程 問題重現 ios端,經常會出現輸入法遮擋輸入框的問題(特別是那種有一個白色頂部的輸入法,如:
h5鍵盤遮擋輸入框問題 、模仿微信輸入框失去焦點時隱藏iphone的軟鍵盤和聚焦時出現輸入框
最近的專案做得是混合開發,其實比較尷尬的啦,手機端的安卓與ios挺多相容問題的。 1、手機端h5頁面中輸入法鍵盤會遮擋輸入框的問題。 $('input').on('focus',function(event){ //自動反彈 輸入法高度
鍵盤彈起,遮擋輸入框
方式一: extension LoginViewController:UITextFieldDelegate { func textFieldShouldReturn(textField:
移動端手機軟鍵盤遮擋輸入框問題
頁面: <section class="links"> <h3 class="title">聯絡方式</h3> <div class="c
swift開發筆記24 解決鍵盤遮擋輸入框 的方法
func textViewDidBeginEditing(textView:UITextView) { UIView.animateWithDuration(0.4, anim
Android 自定義數字鍵盤(三)自定義輸入框
Android 自定義數字鍵盤(一) Android 自定義數字鍵盤(二)隨機數字 Demo地址:https://github.com/danfengfirst/KeyDemo 這篇部落格是在上面兩篇部落格的基礎上修改的一個相對比較完整的demo,
React中鍵盤遮擋輸入框
很多情況下,我們需要在介面中進行輸入資訊,免不了TextInput元件,但是問題來了,有時候鍵盤彈上來的位置正好遮住了輸入框,使用者根本看不到自己輸入了什麼。 這裡面有一個非常簡單的方法來實現: i
Android WebView 軟鍵盤遮擋輸入框問題的解決方法
1. 在java程式碼中設定 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); 2. 在
iOS正則表示式之區分大小寫中英文,判斷輸入框型別
在開發過程中,有時需要對使用者輸入的型別做判斷,最常見是在註冊頁面即使用者名稱和密碼,直接上程式碼 #pragma - mark 只能為中文 -(BOOL)onlyInputChineseChara
iOS 鍵盤彈出與回收、介面上移和下移
//新增通知,來控制鍵盤和輸入框的位置 [[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(ke
h5 ios輸入框與鍵盤 相容性優化
起因 h5的輸入框引起鍵盤導致體驗不好,目前就算微信、知乎、百度等產品也沒有很好的技術方案實現,尤其底部固定位置的輸入框各種方案都用的前提下體驗也並沒有很好,這個問題也是老大難問題了。目前在準備一套與native協議 來解決這個問題,目前專案中的解決方案還是有值得借