Skip to content

Commit 0cd167e

Browse files
孟遥遥孟遥遥
authored andcommitted
光标移动处理
1 parent 5975dfa commit 0cd167e

File tree

1 file changed

+48
-28
lines changed

1 file changed

+48
-28
lines changed

CocoaAsyncSocket_TCP/View/KeyBoard/ChatKeyboard.m

Lines changed: 48 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,11 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
482482

483483
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
484484
{
485+
//删除键监听
486+
if ([text isEqualToString:@""""]) {
487+
488+
NSLog(@"----------------点击了系统键盘删除键");
489+
}
485490
return YES;
486491
}
487492

@@ -533,40 +538,19 @@ - (void)emotionClick:(UIButton *)emotionBtn
533538
//获取点击的表情
534539
NSString *emotionKey = [NSString stringWithFormat:@"ChatEmotion_%li",emotionBtn.tag - 999];
535540
NSString *emotionName = [self.emotionDict objectForKey:emotionKey];
536-
//获取光标所在位置
537-
NSInteger location = self.msgTextView.selectedRange.location;
538-
//变为可变字符串
539-
NSMutableString *txtStrM = [[NSMutableString alloc]initWithString:self.msgTextView.text];
540541

541542
//判断是删除 , 还是点击了正常的emotion表情
542543
if ([emotionName isEqualToString:@"[del_]"]) {
543544

544-
if (!txtStrM.length) return;
545+
//表情键盘删除
546+
[self keyboardDelete];
545547

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{ //点击表情
562549

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];
570554
[txtStrM insertString:emotionName atIndex:location];
571555
self.msgTextView.text = txtStrM;
572556
//光标后移
@@ -587,6 +571,42 @@ - (void)sendEmotionMessage:(UIButton *)emotionSendBtn
587571

588572
}
589573

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+
}
590610

591611
- (void)dealloc
592612
{

0 commit comments

Comments
 (0)