ios 自定义键盘输入框 输入框跟随文字换行变宽 输入框随着键盘消失出现位置进行变化
2016-11-08 16:24
1181 查看
1.首先通过xib创建一个自定义输入框view
2.在自定义的输入框View中添加block,并实现textView的代理方法
3.在使用该自定义view的类中实现以下方法
以上三步即可实现自定义输入框高度随着输入行数的变化而变化
4.在添加自定义输入框的类中实现输入框随着键盘出现消失,位置跟随变化
键盘失去响应根据自己需求添加
2.在自定义的输入框View中添加block,并实现textView的代理方法
@property (nonatomic,copy) void(^changeInputViewFrame)(float viewH); - (void)textViewDidChange:(UITextView *)textView{ if (textView != self.inputTextView) { return; } CGSize size = [self.inputTextView.text sizeWithAttributes:@{UIFontDescriptorCascadeListAttribute:[UIFont systemFontOfSize:15.0]}]; CGFloat singleH = size.height; NSInteger lines = (NSInteger)self.inputTextView.contentSize.height/singleH; NSLog(@"行数行数======%ld",lines); if (lines>self.minLines) { self.minLines = lines; if (self.changeInputViewFrame&&lines<=7) { self.changeInputViewFrame(singleH); } } if (lines<self.minLines) { self.minLines = lines; if (self.changeInputViewFrame&&lines>2) { self.changeInputViewFrame(singleH*(-1)); } } }
3.在使用该自定义view的类中实现以下方法
__weak CustomChatViewController * weakSelf = self; self.inputBar.changeInputViewFrame = ^(float viewH){ weakSelf.inputBar.frame = CGRectMake(0, weakSelf.inputBar.frame.origin.y-viewH, weakSelf.view.frame.size.width, weakSelf.inputBar.frame.size.height+viewH); //自定义输入view刷新约束 [weakSelf.inputBar layoutIfNeeded]; };
以上三步即可实现自定义输入框高度随着输入行数的变化而变化
4.在添加自定义输入框的类中实现输入框随着键盘出现消失,位置跟随变化
/增加监听,当键盘出现或改变时收出消息 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; //增加监听,当键退出时收出消息 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
- (void)keyboardWillShow:(NSNotification *)aNotification { //获取键盘的高度 /* iphone 6: 中文 2014-12-31 11:16:23.643 Demo[686:41289] 键盘高度是 258 2014-12-31 11:16:23.644 Demo[686:41289] 键盘宽度是 375 英文 2014-12-31 11:55:21.417 Demo[1102:58972] 键盘高度是 216 2014-12-31 11:55:21.417 Demo[1102:58972] 键盘宽度是 375 iphone 6 plus: 英文: 2014-12-31 11:31:14.669 Demo[928:50593] 键盘高度是 226 2014-12-31 11:31:14.669 Demo[928:50593] 键盘宽度是 414 中文: 2015-01-07 09:22:49.438 Demo[622:14908] 键盘高度是 271 2015-01-07 09:22:49.439 Demo[622:14908] 键盘宽度是 414 iphone 5 : 2014-12-31 11:19:36.452 Demo[755:43233] 键盘高度是 216 2014-12-31 11:19:36.452 Demo[755:43233] 键盘宽度是 320 ipad Air: 2014-12-31 11:28:32.178 Demo[851:48085] 键盘高度是 264 2014-12-31 11:28:32.178 Demo[851:48085] 键盘宽度是 768 ipad2 : 2014-12-31 11:33:57.258 Demo[1014:53043] 键盘高度是 264 2014-12-31 11:33:57.258 Demo[1014:53043] 键盘宽度是 768 */ NSDictionary *userInfo = [aNotification userInfo]; NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]; CGRect keyboardRect = [aValue CGRectValue]; float height = keyboardRect.size.height; self.inputBar.frame = CGRectMake(0, self.view.frame.size.height-50-height, self.view.frame.size.width, 50); } //当键退出时调用 - (void)keyboardWillHide:(NSNotification *)aNotification{ [UIView animateWithDuration:0.5 animations:^{ self.inputBar.frame = CGRectMake(0, self.view.frame.size.height-50, self.view.frame.size.width, 50); }]; }
键盘失去响应根据自己需求添加
相关文章推荐
- 【iOS开发-16】UITextField协议的用法,键盘的隐藏,以及视图随着键盘的出现隐藏而上下调整位置
- eclipse左边导航(菜单栏)package explorer自动定位(跟随打开的代码变化位置)、导航栏消失找不到解决
- Android listview 内容布局随着键盘的出现消失,发生改变
- 图片放大后,右下角文字位置跟随一起变化
- IOS开发之功能模块--输入框随着键盘的位置移动而移动
- 网页输入框默认文字出现与消失
- iOS UITextField随着键盘的高度变化动态调整位置
- 文字显示位置随着进度变化
- 监听键盘位置,使输入框跟随键盘移动
- eclipse左边导航(菜单栏)package explorer自动定位(跟随打开的代码变化位置)、导航栏消失找不到解决
- 【实用】iOS开发iOS9中使用UIAlertView,弹窗消失时出现键盘闪现,导致输入框出现在屏幕中。
- iOS:UITextField协议的用法,键盘的隐藏,以及视图随着键盘的出现隐藏而上下调整位置
- iOS开发UITextField协议的用法,键盘的隐藏,以及视图随着键盘的出现隐藏而上下调整位置
- 随着浏览器尺寸变化,文字不换行
- ie中div设置底部固定后,不随着浏览器的分辨率变化而出现div位置的变动
- 在进行文字自绘时,自动换行打印
- android关于输入框弹出后 点击输入框以外的位置会让输入框消失
- 制作点击输入框时默认文字消失的效果
- Jquery 操作页面中iframe自动跟随窗口大小变化,而不出现滚动条,只在iframe内部出滚动条
- adroid 类似QQ,点击输入框外面可以消失软键盘