iOS--TextView自適應高度以及鍵盤遮擋問題

1. 制作一個textview,遵守協(xié)議,設置相關屬性,這里是我做的例子

協(xié)議:UITextViewDelegate

textview.backgroundColor=[UIColor whiteColor]; //背景色

textview.scrollEnabled = NO;? ? //當文字超過視圖的邊框時是否允許滑動,默認為“YES”

textview.delegate = self;? ? ? //設置代理方法的實現(xiàn)類

textview.font=[UIFont fontWithName:@"Arial" size:18.0]; //設置字體名字和字體大小;

textview.returnKeyType = UIReturnKeyDefault;//return鍵的類型

//? ? textview.keyboardType = UIKeyboardTypeDefault;//鍵盤類型換行

textview.returnKeyType = UIReturnKeyDone;//鍵盤類型完成? 不是用完成的話 當文字過多 無法回收鍵盤

//? ? textview.textAlignment = NSTextAlignmentLeft; //文本顯示的位置默認為居左

textview.dataDetectorTypes = UIDataDetectorTypeAll; //顯示數(shù)據(jù)類型的連接模式(如電話號碼、網(wǎng)址、地址等)

textview.textColor = [UIColor blackColor];

textview.autoresizingMask = UIViewAutoresizingFlexibleHeight;

textview.text =NSLocalizedString(@"qingshuru",@"");//設置顯示的文本內(nèi)容

self.automaticallyAdjustsScrollViewInsets=NO;

2. 實現(xiàn)高度自適應

(1)

//內(nèi)容改變自動調(diào)用 自適應文本高度

- (void)textViewDidChange:(UITextView *)textView;

(2)在上面的方法中寫入

CGSize size=[textview sizeThatFits:CGSizeMake(CGRectGetWidth(textview.frame), MAXFLOAT)];

CGRect frame=textview.frame;

frame.size.height=size.height;

textview.frame=frame;

這樣就完成了自適應高度

(3)對文字輸入的字數(shù)進行限制

if([[self currentLanguage] compare:@"zh-Hans-CN" options:NSCaseInsensitiveSearch]==NSOrderedSame || [[self currentLanguage] compare:@"zh-Hant-CN" options:NSCaseInsensitiveSearch]==NSOrderedSame)

{

if (textView.text.length > 300)

{

textView.text = [textView.text substringToIndex:300];

}

}

else{

if (textView.text.length > 500)

{

textView.text = [textView.text substringToIndex:500];

}

}

3. 實現(xiàn)當文字過多鍵盤遮擋問題

(1)對textview設置監(jiān)聽

//注冊通知,監(jiān)聽鍵盤出現(xiàn)

[[NSNotificationCenter defaultCenter]addObserver:self

selector:@selector(handleKeyboardDidShow:)

name:UIKeyboardDidShowNotification

object:nil];

//注冊通知,監(jiān)聽鍵盤消失事件

[[NSNotificationCenter defaultCenter]addObserver:self

selector:@selector(handleKeyboardDidHidden)

name:UIKeyboardDidHideNotification

object:nil];

(2)實現(xiàn)監(jiān)聽方法

- (void)handleKeyboardDidShow:(NSNotification*)paramNotification

{

NSLog(@"監(jiān)聽方法");

//獲取鍵盤高度

keyboardRectAsObject=[[paramNotification userInfo]objectForKey:UIKeyboardFrameEndUserInfoKey];

[keyboardRectAsObject getValue:&keyboardRect];

textview.contentInset=UIEdgeInsetsMake(0, 0,keyboardRect.size.height, 0);

animationTime = [paramNotification.userInfo[@"UIKeyboardAnimationDurationUserInfoKey"] floatValue];

}

- (void)handleKeyboardDidHidden

{

textview.contentInset=UIEdgeInsetsZero;

}

(3)實現(xiàn)textview的delegate方法

在- (void)textViewDidChange:(UITextView *)textView;方法中寫入

self.view.transform = CGAffineTransformIdentity;

UIView *editView = textview;

CGRect tfRect = [editView.superview convertRect:editView.frame toView:self.view];

//? ? ? ? NSLog(@"%@", keyboardRectAsObject);

CGRect keyBoardF = [keyboardRectAsObject CGRectValue];

CGFloat _editMaxY = CGRectGetMaxY(tfRect);

CGFloat _keyBoardMinY = CGRectGetMinY(keyBoardF);

//? ? ? ? NSLog(@"%f %f", _editMaxY, _keyBoardMinY);

if (keyboardRectAsObject) {

if (_keyBoardMinY < _editMaxY && _editMaxY-_keyBoardMinY>20) {

CGFloat moveDistance = _editMaxY - _keyBoardMinY;

[UIView animateWithDuration:animationTime animations:^{

textview.transform = CGAffineTransformTranslate(textview.transform, 0, -moveDistance);

}];

}

}

此時已經(jīng)實現(xiàn)了高度自適應和鍵盤遮擋問題

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容