博客> iOS--TextView自适应高度以及键盘遮挡问题
iOS--TextView自适应高度以及键盘遮挡问题
2018-12-16 12:23 评论:0 阅读:676 小色样i
ios textView 键盘遮挡 高度自适应

1. 制作一个textview

遵守协议,设置相关属性,这里是我做的例子 协议:UITextViewDelegate textview.backgroundColor=[UIColor whiteColor]; //背景色

textview.scrollEnabled = NO; //当文字超过视图的边框时是否允许滑动,默认为“YES”

textview.delegate = self; //设置代理方法的实现类

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

textview.returnKeyType = UIReturnKeyDefault;//return键的类型

// textview.keyboardType = UIKeyboardTypeDefault;//键盘类型换行

textview.returnKeyType = UIReturnKeyDone;//键盘类型完成 不是用完成的话 当文字过多 无法回收键盘

// textview.textAlignment = NSTextAlignmentLeft; //文本显示的位置默认为居左

textview.dataDetectorTypes = UIDataDetectorTypeAll; //显示数据类型的连接模式(如电话号码、网址、地址等)

textview.textColor = [UIColor blackColor];

textview.autoresizingMask = UIViewAutoresizingFlexibleHeight;

textview.text =NSLocalizedString(@"qingshuru",@"");//设置显示的文本内容

self.automaticallyAdjustsScrollViewInsets=NO;

2. 实现高度自适应

(1) //内容改变自动调用 自适应文本高度

  • (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)对文字输入的字数进行限制 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. 实现当文字过多键盘遮挡问题

(1)对textview设置监听 //注册通知,监听键盘出现

[[NSNotificationCenter defaultCenter]addObserver:self

selector:@selector(handleKeyboardDidShow:)

name:UIKeyboardDidShowNotification

object:nil]; //注册通知,监听键盘消失事件

[[NSNotificationCenter defaultCenter]addObserver:self

selector:@selector(handleKeyboardDidHidden)

name:UIKeyboardDidHideNotification

object:nil]; (2)实现监听方法

  • (void)handleKeyboardDidShow:(NSNotification*)paramNotification

{

NSLog(@"监听方法");

//获取键盘高度

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)实现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);

}];

} } 此时已经实现了高度自适应和键盘遮挡问题

收藏
0
sina weixin mail 回到顶部