@@ -22,7 +22,8 @@ - (void)layoutSubviews
22
22
23
23
@end
24
24
25
- static CGFloat sysKeyboardEndY = 0 ;
25
+ // 记录当前键盘的高度 ,键盘除了系统的键盘还有咱们自定义的键盘,互相来回切换
26
+ static CGFloat keyboardHeight = 0 ;
26
27
27
28
@interface ChatKeyboard ()<UITextViewDelegate,UIScrollViewDelegate>
28
29
// 表情
@@ -349,7 +350,8 @@ - (void)systemKeyboardWillShow:(NSNotification *)note
349
350
[self reloadSwitchButtons ];
350
351
// 获取系统键盘高度
351
352
CGFloat systemKbHeight = [note.userInfo[@" UIKeyboardBoundsUserInfoKey" ]CGRectValue].size .height ;
352
- // 记录Y值
353
+ // 记录系统键盘高度
354
+ keyboardHeight = systemKbHeight;
353
355
// 将自定义键盘跟随位移
354
356
[self customKeyboardMove: SCREEN_HEIGHT - systemKbHeight - Height (self .messageBar.frame)];
355
357
}
@@ -390,6 +392,8 @@ - (void)switchFaceKeyboard:(UIButton *)swtFaceButton
390
392
// 重置其他按钮seleted
391
393
self.audioButton .selected = NO ;
392
394
self.swtHandleButton .selected = NO ;
395
+ // 更新记录键盘高度
396
+ keyboardHeight = Height (self.keyBoardContainer .frame );
393
397
394
398
if (swtFaceButton.selected ) {
395
399
_msgTextView.hidden = NO ;
@@ -410,6 +414,8 @@ - (void)switchHandleKeyboard:(UIButton *)swtHandleButton
410
414
// 重置其他按钮selected
411
415
self.audioButton .selected = NO ;
412
416
self.swtFaceButton .selected = NO ;
417
+ // 更新记录键盘高度
418
+ keyboardHeight = Height (self.keyBoardContainer .frame );
413
419
414
420
if (swtHandleButton.selected ) {
415
421
_msgTextView.hidden = NO ;
@@ -469,7 +475,8 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
469
475
NSLog (@" -------old ---%@ " ,change[@" old" ]);
470
476
if (change[@" new" ] != change[@" old" ]) {
471
477
NSLog (@" 高度变化" );
472
- [self msgTextViewHeightFit ];
478
+ // 根据实时的键盘高度进行布局
479
+ [self msgTextViewHeightFit: keyboardHeight];
473
480
}
474
481
}
475
482
@@ -487,12 +494,12 @@ - (void)reloadSwitchButtons
487
494
}
488
495
489
496
#pragma mark - 输入框拉高
490
- - (void )msgTextViewHeightFit
497
+ - (void )msgTextViewHeightFit : (CGFloat) currentKbHeight
491
498
{
492
499
self.messageBar .frame = Frame (0 , 0 , SCREEN_WITDTH, self.msgTextView .contentSize .height +MinY (self.msgTextView .frame )*2 );
493
500
self.msgTextView .frame = Frame (MinX (self.msgTextView .frame ),(Height (self.messageBar .frame )-self.msgTextView .contentSize .height )*0.5 , Width (self.msgTextView .frame ), self.msgTextView .contentSize .height );
494
501
self.keyBoardContainer .frame = Frame (0 , MaxY (self.messageBar .frame ), SCREEN_WITDTH, Height (self.keyBoardContainer .frame ));
495
- 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 ));
502
+ self.frame = Frame (0 ,SCREEN_HEIGHT - currentKbHeight- Height (self.messageBar .frame ), SCREEN_WITDTH,Height (self.keyBoardContainer .frame ) + Height (self.messageBar .frame ));
496
503
}
497
504
498
505
#pragma mark - 拍摄 , 照片 ,视频按钮点击
@@ -522,15 +529,50 @@ - (void)handleButtonClick:(ChatHandleButton *)button
522
529
#pragma mark - 点击表情
523
530
- (void )emotionClick : (UIButton *)emotionBtn
524
531
{
532
+
525
533
// 获取点击的表情
526
534
NSString *emotionKey = [NSString stringWithFormat: @" ChatEmotion_%li " ,emotionBtn.tag - 999 ];
527
535
NSString *emotionName = [self .emotionDict objectForKey: emotionKey];
528
536
// 获取光标所在位置
529
537
NSInteger location = self.msgTextView .selectedRange .location ;
538
+ // 变为可变字符串
530
539
NSMutableString *txtStrM = [[NSMutableString alloc ]initWithString:self .msgTextView.text];
531
- [txtStrM insertString: emotionName atIndex: location];
532
- self.msgTextView .text = txtStrM;
533
- NSLog (@" --------当前点击了表情 : ------------------%@ " ,emotionName);
540
+
541
+ // 判断是删除 , 还是点击了正常的emotion表情
542
+ if ([emotionName isEqualToString: @" [del_]" ]) {
543
+
544
+ if (!txtStrM.length ) return ;
545
+
546
+ // 正则检测是否存在表情
547
+ NSRegularExpression *pression = [NSRegularExpression regularExpressionWithPattern: @" \\ [[^\\ [\\ ]]*\\ ]" options: NSRegularExpressionCaseInsensitive error: NULL ];
548
+ NSArray *results = [pression matchesInString: self .msgTextView.text options: NSMatchingReportProgress range: NSMakeRange (0 , self .msgTextView.text.length)];
549
+ // 检测光标前是否有表情
550
+ __block BOOL deleteEmotion = NO ;
551
+ [results enumerateObjectsUsingBlock: ^(NSTextCheckingResult *_Nonnull checkResult, NSUInteger idx, BOOL * _Nonnull stop) {
552
+ // 光标前面有表情
553
+ if (checkResult.range .location + checkResult.range .length == location) {
554
+
555
+ NSLog (@" -------光标前是表情------------" );
556
+ [txtStrM replaceCharactersInRange: checkResult.range withString: @" " ];
557
+ self.msgTextView .text = txtStrM;
558
+ deleteEmotion = YES ;
559
+ *stop = YES ;
560
+ }
561
+ }];
562
+
563
+ // 如果光标前没有表情
564
+ if (!deleteEmotion) {
565
+ [txtStrM replaceCharactersInRange: NSMakeRange (txtStrM.length-1 , 1 ) withString: @" " ];
566
+ self.msgTextView .text = txtStrM;
567
+ }
568
+
569
+ }else {
570
+ [txtStrM insertString: emotionName atIndex: location];
571
+ self.msgTextView .text = txtStrM;
572
+ // 光标后移
573
+ self.msgTextView .selectedRange = NSMakeRange (location + emotionName.length , 0 );
574
+ NSLog (@" --------当前点击了表情 : ------------------%@ " ,emotionName);
575
+ }
534
576
}
535
577
536
578
#pragma mark - scrollViewDelegate
@@ -550,4 +592,7 @@ - (void)dealloc
550
592
{
551
593
[self .msgTextView removeObserver: self forKeyPath: @" contentSize" ];
552
594
}
595
+
596
+
597
+
553
598
@end
0 commit comments