Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 313d083

Browse files
author
mengyaoyao
committed
更新输入框拉高
1 parent cd83744 commit 313d083

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

CocoaAsyncSocket_TCP/Controller/ChatViewController.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ - (void)initUI
197197

198198
//初始化键盘
199199
[self.view addSubview:self.customKeyboard];
200-
self.customKeyboard.frame = Frame(0, SCREEN_HEIGHT - 49, SCREEN_WITDTH, CUSTOMKEYBOARD_HEIGHT);
200+
self.customKeyboard.frame = Frame(0, SCREEN_HEIGHT - 49, SCREEN_WITDTH, CTKEYBOARD_DEFAULTHEIGHT);
201201
}
202202

203203
#pragma mark - 注册通知

CocoaAsyncSocket_TCP/View/KeyBoard/ChatKeyboard.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// Created by 孟遥 on 2017/5/15.
66
// Copyright © 2017年 mengyao. All rights reserved.
77
//
8-
#define CUSTOMKEYBOARD_HEIGHT 273
8+
#define CTKEYBOARD_DEFAULTHEIGHT 273
99

1010
#import <UIKit/UIKit.h>
1111

CocoaAsyncSocket_TCP/View/KeyBoard/ChatKeyboard.m

+36-3
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,12 @@ - (UITextView *)msgTextView
231231
_msgTextView.font = FontSet(14);
232232
_msgTextView.showsVerticalScrollIndicator = NO;
233233
_msgTextView.showsHorizontalScrollIndicator = NO;
234-
_msgTextView.scrollEnabled = NO;
235234
_msgTextView.returnKeyType = UIReturnKeySend;
236235
_msgTextView.enablesReturnKeyAutomatically = YES;
237236
_msgTextView.delegate = self;
238237
ViewRadius(_msgTextView, 5);
238+
//观察者监听高度变化
239+
[_msgTextView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
239240
}
240241
return _msgTextView;
241242
}
@@ -318,11 +319,11 @@ - (void)configUIFrame
318319
{
319320
self.messageBar.frame = Frame(0, 0, SCREEN_WITDTH, 49); //消息栏
320321
self.audioButton.frame = Frame(10, (Height(self.messageBar.frame) - 30)*0.5, 30, 30); //语音按钮
321-
self.audioLpButton.frame = Frame(MaxX(self.audioButton.frame)+15,(Height(self.messageBar.frame)-34)*0.5, SCREEN_WITDTH - 155, 34); //长按录音按钮
322+
self.audioLpButton.frame = Frame(MaxX(self.audioButton.frame)+15,(Height(self.messageBar.frame)-35)*0.5, SCREEN_WITDTH - 155, 35); //长按录音按钮
322323
self.msgTextView.frame = self.audioLpButton.frame; //输入框
323324
self.swtFaceButton.frame = Frame(MaxX(self.msgTextView.frame)+15, (Height(self.messageBar.frame)-30)*0.5,30, 30); //表情键盘切换按钮
324325
self.swtHandleButton.frame = Frame(MaxX(self.swtFaceButton.frame)+15, (Height(self.messageBar.frame)-30)*0.5, 30, 30); //加号按钮切换操作键盘
325-
self.keyBoardContainer.frame = Frame(0,Height(self.messageBar.frame), SCREEN_WITDTH,CUSTOMKEYBOARD_HEIGHT - Height(self.messageBar.frame)); //自定义键盘容器
326+
self.keyBoardContainer.frame = Frame(0,Height(self.messageBar.frame), SCREEN_WITDTH,CTKEYBOARD_DEFAULTHEIGHT - Height(self.messageBar.frame)); //自定义键盘容器
326327
self.handleKeyboard.frame = self.keyBoardContainer.bounds ;//键盘操作栏
327328
self.facesKeyboard.frame = self.keyBoardContainer.bounds ; //表情键盘部分
328329

@@ -388,6 +389,14 @@ - (void)audioLpButtonTouchUpInside:(UIButton *)audioLpButton
388389
#pragma mark - 切换到表情键盘
389390
- (void)switchFaceKeyboard:(UIButton *)swtFaceButton
390391
{
392+
393+
if (swtFaceButton.selected) {
394+
395+
}else{
396+
397+
}
398+
399+
391400
_msgTextView.hidden = NO;
392401
_audioLpButton.hidden = YES;
393402
[_msgTextView resignFirstResponder];
@@ -421,6 +430,25 @@ - (void)textViewDidChange:(UITextView *)textView
421430
{
422431

423432
}
433+
434+
#pragma mark - 监听输入框变化 (这里如果放到layout里自动让他布局 , 会稍显麻烦一些 , 所以自动手动控制一下)
435+
//这里用contentSize计算较为简单和精确 , 如果计算文字高度 , 还需要加上textView的内间距.
436+
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
437+
{
438+
CGFloat oldHeight = [change[@"old"]CGSizeValue].height;
439+
CGFloat newHeight = [change[@"new"]CGSizeValue].height;
440+
if (oldHeight <=0 || newHeight <=0) return;
441+
NSLog(@"------new ----%@",change[@"new"]);
442+
NSLog(@"-------old ---%@",change[@"old"]);
443+
if (change[@"new"] != change[@"old"]) {
444+
NSLog(@"高度变化");
445+
self.messageBar.frame = Frame(0, 0, SCREEN_WITDTH, newHeight+MinY(self.msgTextView.frame)*2);
446+
self.msgTextView.frame = Frame(MinX(self.msgTextView.frame),(Height(self.messageBar.frame)-newHeight)*0.5, Width(self.msgTextView.frame), newHeight);
447+
self.keyBoardContainer.frame = Frame(0, MaxY(self.messageBar.frame), SCREEN_WITDTH, Height(self.keyBoardContainer.frame));
448+
self.frame = Frame(0,SCREEN_HEIGHT - Height(self.messageBar.frame) - Height(self.keyBoardContainer.frame)-49, SCREEN_WITDTH,Height(self.keyBoardContainer.frame) + Height(self.messageBar.frame));
449+
}
450+
}
451+
424452
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
425453
{
426454
return YES;
@@ -471,4 +499,9 @@ - (void)sendEmotionMessage:(UIButton *)emotionSendBtn
471499

472500
}
473501

502+
503+
- (void)dealloc
504+
{
505+
[self.msgTextView removeObserver:self forKeyPath:@"contentSize"];
506+
}
474507
@end

0 commit comments

Comments
 (0)