Skip to content

Commit 5975dfa

Browse files
孟遥遥孟遥遥
authored andcommitted
输入表情,删除逻辑
1 parent 185061b commit 5975dfa

File tree

2 files changed

+53
-8
lines changed

2 files changed

+53
-8
lines changed

.DS_Store

2 KB
Binary file not shown.

CocoaAsyncSocket_TCP/View/KeyBoard/ChatKeyboard.m

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ - (void)layoutSubviews
2222

2323
@end
2424

25-
static CGFloat sysKeyboardEndY = 0;
25+
//记录当前键盘的高度 ,键盘除了系统的键盘还有咱们自定义的键盘,互相来回切换
26+
static CGFloat keyboardHeight = 0;
2627

2728
@interface ChatKeyboard ()<UITextViewDelegate,UIScrollViewDelegate>
2829
//表情
@@ -349,7 +350,8 @@ - (void)systemKeyboardWillShow:(NSNotification *)note
349350
[self reloadSwitchButtons];
350351
//获取系统键盘高度
351352
CGFloat systemKbHeight = [note.userInfo[@"UIKeyboardBoundsUserInfoKey"]CGRectValue].size.height;
352-
//记录Y值
353+
//记录系统键盘高度
354+
keyboardHeight = systemKbHeight;
353355
//将自定义键盘跟随位移
354356
[self customKeyboardMove:SCREEN_HEIGHT - systemKbHeight - Height(self.messageBar.frame)];
355357
}
@@ -390,6 +392,8 @@ - (void)switchFaceKeyboard:(UIButton *)swtFaceButton
390392
//重置其他按钮seleted
391393
self.audioButton.selected = NO;
392394
self.swtHandleButton.selected = NO;
395+
//更新记录键盘高度
396+
keyboardHeight = Height(self.keyBoardContainer.frame);
393397

394398
if (swtFaceButton.selected) {
395399
_msgTextView.hidden = NO;
@@ -410,6 +414,8 @@ - (void)switchHandleKeyboard:(UIButton *)swtHandleButton
410414
//重置其他按钮selected
411415
self.audioButton.selected = NO;
412416
self.swtFaceButton.selected = NO;
417+
//更新记录键盘高度
418+
keyboardHeight = Height(self.keyBoardContainer.frame);
413419

414420
if (swtHandleButton.selected) {
415421
_msgTextView.hidden = NO;
@@ -469,7 +475,8 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
469475
NSLog(@"-------old ---%@",change[@"old"]);
470476
if (change[@"new"] != change[@"old"]) {
471477
NSLog(@"高度变化");
472-
[self msgTextViewHeightFit];
478+
//根据实时的键盘高度进行布局
479+
[self msgTextViewHeightFit:keyboardHeight];
473480
}
474481
}
475482

@@ -487,12 +494,12 @@ - (void)reloadSwitchButtons
487494
}
488495

489496
#pragma mark - 输入框拉高
490-
- (void)msgTextViewHeightFit
497+
- (void)msgTextViewHeightFit:(CGFloat)currentKbHeight
491498
{
492499
self.messageBar.frame = Frame(0, 0, SCREEN_WITDTH, self.msgTextView.contentSize.height +MinY(self.msgTextView.frame)*2);
493500
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);
494501
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));
496503
}
497504

498505
#pragma mark - 拍摄 , 照片 ,视频按钮点击
@@ -522,15 +529,50 @@ - (void)handleButtonClick:(ChatHandleButton *)button
522529
#pragma mark - 点击表情
523530
- (void)emotionClick:(UIButton *)emotionBtn
524531
{
532+
525533
//获取点击的表情
526534
NSString *emotionKey = [NSString stringWithFormat:@"ChatEmotion_%li",emotionBtn.tag - 999];
527535
NSString *emotionName = [self.emotionDict objectForKey:emotionKey];
528536
//获取光标所在位置
529537
NSInteger location = self.msgTextView.selectedRange.location;
538+
//变为可变字符串
530539
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+
}
534576
}
535577

536578
#pragma mark - scrollViewDelegate
@@ -550,4 +592,7 @@ - (void)dealloc
550592
{
551593
[self.msgTextView removeObserver:self forKeyPath:@"contentSize"];
552594
}
595+
596+
597+
553598
@end

0 commit comments

Comments
 (0)