@@ -482,6 +482,11 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
482
482
483
483
- (BOOL )textView : (UITextView *)textView shouldChangeTextInRange : (NSRange )range replacementText : (NSString *)text
484
484
{
485
+ // 删除键监听
486
+ if ([text isEqualToString: @" " " " ]) {
487
+
488
+ NSLog (@" ----------------点击了系统键盘删除键" );
489
+ }
485
490
return YES ;
486
491
}
487
492
@@ -533,40 +538,19 @@ - (void)emotionClick:(UIButton *)emotionBtn
533
538
// 获取点击的表情
534
539
NSString *emotionKey = [NSString stringWithFormat: @" ChatEmotion_%li " ,emotionBtn.tag - 999 ];
535
540
NSString *emotionName = [self .emotionDict objectForKey: emotionKey];
536
- // 获取光标所在位置
537
- NSInteger location = self.msgTextView .selectedRange .location ;
538
- // 变为可变字符串
539
- NSMutableString *txtStrM = [[NSMutableString alloc ]initWithString:self .msgTextView.text];
540
541
541
542
// 判断是删除 , 还是点击了正常的emotion表情
542
543
if ([emotionName isEqualToString: @" [del_]" ]) {
543
544
544
- if (!txtStrM.length ) return ;
545
+ // 表情键盘删除
546
+ [self keyboardDelete ];
545
547
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
- }];
548
+ }else { // 点击表情
562
549
563
- // 如果光标前没有表情
564
- if (!deleteEmotion) {
565
- [txtStrM replaceCharactersInRange: NSMakeRange (txtStrM.length-1 , 1 ) withString: @" " ];
566
- self.msgTextView .text = txtStrM;
567
- }
568
-
569
- }else {
550
+ // 获取光标所在位置
551
+ NSInteger location = self.msgTextView .selectedRange .location ;
552
+ // 变为可变字符串
553
+ NSMutableString *txtStrM = [[NSMutableString alloc ]initWithString:self .msgTextView.text];
570
554
[txtStrM insertString: emotionName atIndex: location];
571
555
self.msgTextView .text = txtStrM;
572
556
// 光标后移
@@ -587,6 +571,42 @@ - (void)sendEmotionMessage:(UIButton *)emotionSendBtn
587
571
588
572
}
589
573
574
+ #pragma mark - 键盘删除内容
575
+ - (void )keyboardDelete
576
+ {
577
+
578
+ NSMutableString *txtStrM = [[NSMutableString alloc ]initWithString:self .msgTextView.text];
579
+ // 当前光标位置
580
+ NSInteger location = self.msgTextView .selectedRange .location ;
581
+ if (!txtStrM.length ) return ;
582
+
583
+ // 正则检测是否存在表情
584
+ NSRegularExpression *pression = [NSRegularExpression regularExpressionWithPattern: @" \\ [[^\\ [\\ ]]*\\ ]" options: NSRegularExpressionCaseInsensitive error: NULL ];
585
+ NSArray *results = [pression matchesInString: self .msgTextView.text options: NSMatchingReportProgress range: NSMakeRange (0 , self .msgTextView.text.length)];
586
+ // 检测光标前是否有表情
587
+ __block BOOL deleteEmotion = NO ;
588
+ [results enumerateObjectsUsingBlock: ^(NSTextCheckingResult *_Nonnull checkResult, NSUInteger idx, BOOL * _Nonnull stop) {
589
+ // 光标前面有表情
590
+ if (checkResult.range .location + checkResult.range .length == location) {
591
+
592
+ NSLog (@" -------光标前是表情------------" );
593
+ [txtStrM replaceCharactersInRange: checkResult.range withString: @" " ];
594
+ self.msgTextView .text = txtStrM;
595
+ // 光标前移
596
+ self.msgTextView .selectedRange = NSMakeRange (location - checkResult.range .length , 0 );
597
+ deleteEmotion = YES ;
598
+ *stop = YES ;
599
+ }
600
+ }];
601
+
602
+ // 如果光标前没有表情
603
+ if (!deleteEmotion) {
604
+ [txtStrM replaceCharactersInRange: NSMakeRange (txtStrM.length-1 , 1 ) withString: @" " ];
605
+ self.msgTextView .text = txtStrM;
606
+ // 光标前移
607
+ self.msgTextView .selectedRange = NSMakeRange (location - 1 , 0 );
608
+ }
609
+ }
590
610
591
611
- (void )dealloc
592
612
{
0 commit comments