TUIInputBar_Minimalist.m 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. //
  2. // TUIInputBar_Minimalist.m
  3. // UIKit
  4. //
  5. // Created by kennethmiao on 2018/9/18.
  6. // Copyright © 2018 Tencent. All rights reserved.
  7. //
  8. #import "TUIInputBar_Minimalist.h"
  9. #import <TIMCommon/NSString+TUIEmoji.h>
  10. #import <TIMCommon/NSTimer+TUISafe.h>
  11. #import <TIMCommon/TIMDefine.h>
  12. #import <TUICore/TUICore.h>
  13. #import <TUICore/TUIDarkModel.h>
  14. #import <TUICore/TUIGlobalization.h>
  15. #import <TUICore/TUITool.h>
  16. #import <TUICore/UIView+TUILayout.h>
  17. #import "ReactiveObjC/ReactiveObjC.h"
  18. #import "TUIAudioRecorder.h"
  19. #import "TUIChatConfig.h"
  20. @interface TUIInputBar_Minimalist () <UITextViewDelegate, TUIAudioRecorderDelegate>
  21. @property(nonatomic, strong) NSDate *recordStartTime;
  22. @property(nonatomic, strong) TUIAudioRecorder *recorder;
  23. @property(nonatomic, strong) NSTimer *recordTimer;
  24. @property(nonatomic, assign) BOOL isFocusOn;
  25. @property(nonatomic, strong) NSTimer *sendTypingStatusTimer;
  26. @property(nonatomic, assign) BOOL allowSendTypingStatusByChangeWord;
  27. @end
  28. @implementation TUIInputBar_Minimalist
  29. - (void)dealloc {
  30. if (_sendTypingStatusTimer) {
  31. [_sendTypingStatusTimer invalidate];
  32. _sendTypingStatusTimer = nil;
  33. }
  34. [[NSNotificationCenter defaultCenter] removeObserver:self];
  35. }
  36. - (id)initWithFrame:(CGRect)frame {
  37. self = [super initWithFrame:frame];
  38. if (self) {
  39. [self setupViews];
  40. [self defaultLayout];
  41. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onThemeChanged) name:TUIDidApplyingThemeChangedNotfication object:nil];
  42. }
  43. return self;
  44. }
  45. - (void)setupViews {
  46. self.backgroundColor = RGBA(255, 255, 255, 1);
  47. _lineView = [[UIView alloc] init];
  48. _lineView.backgroundColor = TIMCommonDynamicColor(@"separator_color", @"#FFFFFF");
  49. _moreButton = [[UIButton alloc] init];
  50. [_moreButton addTarget:self action:@selector(clickMoreBtn:) forControlEvents:UIControlEventTouchUpInside];
  51. [_moreButton setImage:[[TUIImageCache sharedInstance] getResourceFromCache:TUIChatImagePath_Minimalist(@"TypeSelectorBtnHL_Black")]
  52. forState:UIControlStateNormal];
  53. [_moreButton setImage:[[TUIImageCache sharedInstance] getResourceFromCache:TUIChatImagePath_Minimalist(@"TypeSelectorBtnHL_Black")]
  54. forState:UIControlStateHighlighted];
  55. [self addSubview:_moreButton];
  56. _inputTextView = [[TUIResponderTextView_Minimalist alloc] init];
  57. _inputTextView.delegate = self;
  58. [_inputTextView setFont:kTUIInputNoramlFont];
  59. _inputTextView.backgroundColor = TUIChatDynamicColor(@"chat_input_bg_color", @"#FFFFFF");
  60. _inputTextView.textColor = TUIChatDynamicColor(@"chat_input_text_color", @"#000000");
  61. UIEdgeInsets ei = UIEdgeInsetsMake(kScale390(9), kScale390(16), kScale390(9), kScale390(30));
  62. _inputTextView.textContainerInset = rtlEdgeInsetsWithInsets(ei);
  63. _inputTextView.textAlignment = isRTL()?NSTextAlignmentRight: NSTextAlignmentLeft;
  64. [_inputTextView setReturnKeyType:UIReturnKeySend];
  65. [self addSubview:_inputTextView];
  66. _keyboardButton = [[UIButton alloc] init];
  67. [_keyboardButton addTarget:self action:@selector(clickKeyboardBtn:) forControlEvents:UIControlEventTouchUpInside];
  68. [_keyboardButton setImage:TUIChatBundleThemeImage(@"chat_ToolViewKeyboard_img", @"ToolViewKeyboard") forState:UIControlStateNormal];
  69. [_keyboardButton setImage:TUIChatBundleThemeImage(@"chat_ToolViewKeyboardHL_img", @"ToolViewKeyboardHL") forState:UIControlStateHighlighted];
  70. _keyboardButton.hidden = YES;
  71. [self addSubview:_keyboardButton];
  72. _faceButton = [[UIButton alloc] init];
  73. [_faceButton addTarget:self action:@selector(clickFaceBtn:) forControlEvents:UIControlEventTouchUpInside];
  74. [_faceButton setImage:[[TUIImageCache sharedInstance] getResourceFromCache:TUIChatImagePath_Minimalist(@"ToolViewEmotion")] forState:UIControlStateNormal];
  75. [_faceButton setImage:[[TUIImageCache sharedInstance] getResourceFromCache:TUIChatImagePath_Minimalist(@"ToolViewEmotion")]
  76. forState:UIControlStateHighlighted];
  77. [self addSubview:_faceButton];
  78. _micButton = [[UIButton alloc] init];
  79. [_micButton addTarget:self action:@selector(recordBtnDown:) forControlEvents:UIControlEventTouchDown];
  80. [_micButton addTarget:self action:@selector(recordBtnUp:) forControlEvents:UIControlEventTouchUpInside];
  81. [_micButton addTarget:self action:@selector(recordBtnCancel:) forControlEvents:UIControlEventTouchUpOutside | UIControlEventTouchCancel];
  82. [_micButton addTarget:self action:@selector(recordBtnDragExit:) forControlEvents:UIControlEventTouchDragExit];
  83. [_micButton addTarget:self action:@selector(recordBtnDragEnter:) forControlEvents:UIControlEventTouchDragEnter];
  84. [_micButton setImage:[[TUIImageCache sharedInstance] getResourceFromCache:TUIChatImagePath_Minimalist(@"ToolViewInputVoice")]
  85. forState:UIControlStateNormal];
  86. [self addSubview:_micButton];
  87. _cameraButton = [[UIButton alloc] init];
  88. [_cameraButton addTarget:self action:@selector(clickCameraBtn:) forControlEvents:UIControlEventTouchUpInside];
  89. [_cameraButton setImage:[[TUIImageCache sharedInstance] getResourceFromCache:TUIChatImagePath_Minimalist(@"ToolViewInputCamera")]
  90. forState:UIControlStateNormal];
  91. [_cameraButton setImage:[[TUIImageCache sharedInstance] getResourceFromCache:TUIChatImagePath_Minimalist(@"ToolViewInputCamera")]
  92. forState:UIControlStateHighlighted];
  93. [self addSubview:_cameraButton];
  94. [self initRecordView];
  95. }
  96. - (void)initRecordView {
  97. _recordView = [[UIView alloc] init];
  98. _recordView.backgroundColor = RGBA(255, 255, 255, 1);
  99. _recordView.hidden = YES;
  100. [self addSubview:_recordView];
  101. _recordDeleteView = [[UIImageView alloc] init];
  102. [_recordView addSubview:_recordDeleteView];
  103. _recordBackgroudView = [[UIView alloc] init];
  104. [_recordView addSubview:_recordBackgroudView];
  105. _recordTimeLabel = [[UILabel alloc] init];
  106. _recordTimeLabel.textColor = [UIColor whiteColor];
  107. _recordTimeLabel.font = [UIFont systemFontOfSize:14];
  108. [_recordView addSubview:_recordTimeLabel];
  109. _recordAnimateViews = [NSMutableArray array];
  110. for (int i = 0; i < 6; i++) {
  111. UIImageView *recordAnimateView = [[UIImageView alloc] init];
  112. [recordAnimateView setImage:[[TUIImageCache sharedInstance] getResourceFromCache:TUIChatImagePath_Minimalist(@"voice_record_animation")]];
  113. [_recordView addSubview:recordAnimateView];
  114. [_recordAnimateViews addObject:recordAnimateView];
  115. }
  116. _recordAnimateCoverView = [[UIImageView alloc] init];
  117. [_recordView addSubview:_recordAnimateCoverView];
  118. _recordTipsView = [[UIView alloc] init];
  119. _recordTipsView.backgroundColor = [UIColor whiteColor];
  120. _recordTipsView.frame = CGRectMake(0, -56, Screen_Width, 56);
  121. [_recordView addSubview:_recordTipsView];
  122. _recordTipsLabel = [[UILabel alloc] init];
  123. _recordTipsLabel.textColor = RGBA(102, 102, 102, 1);
  124. _recordTipsLabel.textColor = [UIColor blackColor];
  125. _recordTipsLabel.text = TIMCommonLocalizableString(TUIKitInputRecordTipsTitle);
  126. _recordTipsLabel.textAlignment = NSTextAlignmentCenter;
  127. _recordTipsLabel.font = [UIFont systemFontOfSize:14];
  128. _recordTipsLabel.frame = CGRectMake(0, 10, Screen_Width, 22);
  129. [_recordTipsView addSubview:_recordTipsLabel];
  130. [self setRecordStatus:TUIRecordStatus_Init];
  131. }
  132. - (void)setRecordStatus:(TUIRecordStatus)status {
  133. switch (status) {
  134. case TUIRecordStatus_Init:
  135. case TUIRecordStatus_Record:
  136. case TUIRecordStatus_Cancel: {
  137. _recordDeleteView.frame = CGRectMake(kScale390(16), _recordDeleteView.mm_y, 24, 24);
  138. if (isRTL()){
  139. [_recordDeleteView resetFrameToFitRTL];
  140. }
  141. _recordDeleteView.image = [[TUIImageCache sharedInstance] getResourceFromCache:TUIChatImagePath_Minimalist(@"voice_record_delete")];
  142. _recordBackgroudView.backgroundColor = RGBA(20, 122, 255, 1);
  143. _recordAnimateCoverView.backgroundColor = _recordBackgroudView.backgroundColor;
  144. _recordAnimateCoverView.frame = self.recordAnimateCoverViewFrame;
  145. _recordTimeLabel.text = @"0:00";
  146. _recordTipsLabel.text = TIMCommonLocalizableString(TUIKitInputRecordTipsTitle);
  147. if (TUIRecordStatus_Record == status) {
  148. _recordView.hidden = NO;
  149. } else {
  150. _recordView.hidden = YES;
  151. }
  152. } break;
  153. case TUIRecordStatus_Delete: {
  154. _recordDeleteView.frame = CGRectMake(0, _recordDeleteView.mm_y, 20, 24);
  155. if (isRTL()){
  156. [_recordDeleteView resetFrameToFitRTL];
  157. }
  158. _recordDeleteView.image = [[TUIImageCache sharedInstance] getResourceFromCache:TUIChatImagePath_Minimalist(@"voice_record_delete_ready")];
  159. _recordBackgroudView.backgroundColor = RGBA(255, 88, 76, 1);
  160. _recordAnimateCoverView.backgroundColor = _recordBackgroudView.backgroundColor;
  161. _recordTipsLabel.text = TIMCommonLocalizableString(TUIKitInputRecordCancelTipsTitle);
  162. _recordView.hidden = NO;
  163. } break;
  164. default:
  165. break;
  166. }
  167. }
  168. - (void)applyBorderTheme {
  169. if (_recordBackgroudView) {
  170. [_recordBackgroudView.layer setMasksToBounds:YES];
  171. [_recordBackgroudView.layer setCornerRadius:_recordBackgroudView.mm_h / 2.0];
  172. }
  173. if (_inputTextView) {
  174. [_inputTextView.layer setMasksToBounds:YES];
  175. [_inputTextView.layer setCornerRadius:_inputTextView.mm_h / 2.0];
  176. [_inputTextView.layer setBorderWidth:0.5f];
  177. [_inputTextView.layer setBorderColor:RGBA(221, 221, 221, 1).CGColor];
  178. }
  179. }
  180. - (void)defaultLayout {
  181. _lineView.frame = CGRectMake(0, 0, Screen_Width, TLine_Heigh);
  182. CGFloat iconSize = 24;
  183. _moreButton.frame = CGRectMake(kScale390(16), kScale390(13), iconSize, iconSize);
  184. _cameraButton.frame = CGRectMake(Screen_Width - kScale390(16) - iconSize, 13, iconSize, iconSize);
  185. _micButton.frame = CGRectMake(Screen_Width - kScale390(56) - iconSize, 13, iconSize, iconSize);
  186. CGFloat faceSize = 19;
  187. _faceButton.frame = CGRectMake(_micButton.mm_x - kScale390(50), 15, faceSize, faceSize);
  188. _keyboardButton.frame = _faceButton.frame;
  189. _inputTextView.frame = CGRectMake(kScale390(56), 7, Screen_Width - kScale390(152), 36);
  190. _recordView.frame = CGRectMake(0, _inputTextView.mm_y, self.mm_w, _inputTextView.mm_h);
  191. _recordDeleteView.frame = CGRectMake(kScale390(16), 4, iconSize, iconSize);
  192. _recordBackgroudView.frame = CGRectMake(kScale390(54), 0, self.mm_w - kScale390(70), _recordView.mm_h);
  193. _recordTimeLabel.frame = CGRectMake(kScale390(70), kScale390(7), 32, 22);
  194. CGFloat animationStartX = kScale390(112);
  195. CGFloat animationY = 8;
  196. CGFloat animationSize = 20;
  197. CGFloat animationSpace = kScale390(8);
  198. CGFloat animationCoverWidth = 0;
  199. for (int i = 0; i < self.recordAnimateViews.count; ++i) {
  200. UIView *animationView = self.recordAnimateViews[i];
  201. animationView.frame = CGRectMake(animationStartX + (animationSize + animationSpace) * i, animationY, animationSize, animationSize);
  202. animationCoverWidth = (animationSize + animationSpace) * (i + 1);
  203. }
  204. _recordAnimateCoverViewFrame = CGRectMake(animationStartX, animationY, animationCoverWidth, animationSize);
  205. _recordAnimateCoverView.frame = self.recordAnimateCoverViewFrame;
  206. [self applyBorderTheme];
  207. if(isRTL()) {
  208. for (UIView *subviews in self.subviews) {
  209. [subviews resetFrameToFitRTL];
  210. }
  211. for (UIView *subview in _recordView.subviews) {
  212. [subview resetFrameToFitRTL];
  213. }
  214. }
  215. }
  216. - (void)layoutButton:(CGFloat)height {
  217. CGRect frame = self.frame;
  218. CGFloat offset = height - frame.size.height;
  219. frame.size.height = height;
  220. self.frame = frame;
  221. if (_delegate && [_delegate respondsToSelector:@selector(inputBar:didChangeInputHeight:)]) {
  222. [_delegate inputBar:self didChangeInputHeight:offset];
  223. }
  224. }
  225. - (void)clickCameraBtn:(UIButton *)sender {
  226. _micButton.hidden = NO;
  227. _keyboardButton.hidden = YES;
  228. _inputTextView.hidden = NO;
  229. _faceButton.hidden = NO;
  230. [self setRecordStatus:TUIRecordStatus_Cancel];
  231. if (_delegate && [_delegate respondsToSelector:@selector(inputBarDidTouchCamera:)]) {
  232. [_delegate inputBarDidTouchCamera:self];
  233. }
  234. }
  235. - (void)clickKeyboardBtn:(UIButton *)sender {
  236. _micButton.hidden = NO;
  237. _keyboardButton.hidden = YES;
  238. _inputTextView.hidden = NO;
  239. _faceButton.hidden = NO;
  240. [self setRecordStatus:TUIRecordStatus_Cancel];
  241. [self layoutButton:_inputTextView.frame.size.height + 2 * TTextView_Margin];
  242. if (_delegate && [_delegate respondsToSelector:@selector(inputBarDidTouchKeyboard:)]) {
  243. [_delegate inputBarDidTouchKeyboard:self];
  244. }
  245. }
  246. - (void)clickFaceBtn:(UIButton *)sender {
  247. _micButton.hidden = NO;
  248. _faceButton.hidden = YES;
  249. _keyboardButton.hidden = NO;
  250. _inputTextView.hidden = NO;
  251. [self setRecordStatus:TUIRecordStatus_Cancel];
  252. if (_delegate && [_delegate respondsToSelector:@selector(inputBarDidTouchFace:)]) {
  253. [_delegate inputBarDidTouchFace:self];
  254. }
  255. _keyboardButton.frame = _faceButton.frame;
  256. }
  257. - (void)clickMoreBtn:(UIButton *)sender {
  258. if (_delegate && [_delegate respondsToSelector:@selector(inputBarDidTouchMore:)]) {
  259. [_delegate inputBarDidTouchMore:self];
  260. }
  261. }
  262. - (void)recordBtnDown:(UIButton *)sender {
  263. [self.recorder record];
  264. }
  265. - (void)recordBtnUp:(UIButton *)sender {
  266. NSTimeInterval interval = [[NSDate date] timeIntervalSinceDate:_recordStartTime];
  267. if (interval < 1) {
  268. [self.recorder cancel];
  269. } else if (interval > 60) {
  270. [self.recorder cancel];
  271. } else {
  272. [self.recorder stop];
  273. NSString *path = self.recorder.recordedFilePath;
  274. if (path) {
  275. if (_delegate && [_delegate respondsToSelector:@selector(inputBar:didSendVoice:)]) {
  276. [_delegate inputBar:self didSendVoice:path];
  277. }
  278. }
  279. }
  280. [self setRecordStatus:TUIRecordStatus_Cancel];
  281. }
  282. - (void)recordBtnCancel:(UIGestureRecognizer *)gesture {
  283. [self setRecordStatus:TUIRecordStatus_Cancel];
  284. [self.recorder cancel];
  285. }
  286. - (void)recordBtnDragExit:(UIButton *)sender {
  287. [self setRecordStatus:TUIRecordStatus_Delete];
  288. }
  289. - (void)recordBtnDragEnter:(UIButton *)sender {
  290. [self setRecordStatus:TUIRecordStatus_Record];
  291. }
  292. - (void)showHapticFeedback {
  293. if (@available(iOS 10.0, *)) {
  294. dispatch_async(dispatch_get_main_queue(), ^{
  295. UIImpactFeedbackGenerator *generator = [[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleMedium];
  296. [generator prepare];
  297. [generator impactOccurred];
  298. });
  299. } else {
  300. // Fallback on earlier versions
  301. }
  302. }
  303. #pragma mark - talk
  304. - (void)textViewDidBeginEditing:(UITextView *)textView {
  305. self.keyboardButton.hidden = YES;
  306. self.micButton.hidden = NO;
  307. self.faceButton.hidden = NO;
  308. self.isFocusOn = YES;
  309. self.allowSendTypingStatusByChangeWord = YES;
  310. __weak typeof(self) weakSelf = self;
  311. self.sendTypingStatusTimer = [NSTimer tui_scheduledTimerWithTimeInterval:4
  312. repeats:YES
  313. block:^(NSTimer *_Nonnull timer) {
  314. __strong typeof(weakSelf) strongSelf = weakSelf;
  315. strongSelf.allowSendTypingStatusByChangeWord = YES;
  316. }];
  317. if (self.isFocusOn && [textView.textStorage tui_getPlainString].length > 0) {
  318. if (_delegate && [_delegate respondsToSelector:@selector(inputTextViewShouldBeginTyping:)]) {
  319. [_delegate inputTextViewShouldBeginTyping:textView];
  320. }
  321. }
  322. }
  323. - (void)textViewDidEndEditing:(UITextView *)textView {
  324. self.isFocusOn = NO;
  325. if (_delegate && [_delegate respondsToSelector:@selector(inputTextViewShouldEndTyping:)]) {
  326. [_delegate inputTextViewShouldEndTyping:textView];
  327. }
  328. }
  329. - (void)textViewDidChange:(UITextView *)textView {
  330. if (self.allowSendTypingStatusByChangeWord && self.isFocusOn && [textView.textStorage tui_getPlainString].length > 0) {
  331. if (_delegate && [_delegate respondsToSelector:@selector(inputTextViewShouldBeginTyping:)]) {
  332. self.allowSendTypingStatusByChangeWord = NO;
  333. [_delegate inputTextViewShouldBeginTyping:textView];
  334. }
  335. }
  336. if (self.isFocusOn && [textView.textStorage tui_getPlainString].length == 0) {
  337. if (_delegate && [_delegate respondsToSelector:@selector(inputTextViewShouldEndTyping:)]) {
  338. [_delegate inputTextViewShouldEndTyping:textView];
  339. }
  340. }
  341. if (self.inputBarTextChanged) {
  342. self.inputBarTextChanged(_inputTextView);
  343. }
  344. CGSize size = [_inputTextView sizeThatFits:CGSizeMake(_inputTextView.frame.size.width, TTextView_TextView_Height_Max)];
  345. CGFloat oldHeight = _inputTextView.frame.size.height;
  346. CGFloat newHeight = size.height;
  347. if (newHeight > TTextView_TextView_Height_Max) {
  348. newHeight = TTextView_TextView_Height_Max;
  349. }
  350. if (newHeight < TTextView_TextView_Height_Min) {
  351. newHeight = TTextView_TextView_Height_Min;
  352. }
  353. if (oldHeight == newHeight) {
  354. return;
  355. }
  356. __weak typeof(self) ws = self;
  357. [UIView animateWithDuration:0.3
  358. animations:^{
  359. CGRect textFrame = ws.inputTextView.frame;
  360. textFrame.size.height += newHeight - oldHeight;
  361. ws.inputTextView.frame = textFrame;
  362. [ws layoutButton:newHeight + 2 * TTextView_Margin];
  363. }];
  364. }
  365. - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
  366. if ([text tui_containsString:@"["] && [text tui_containsString:@"]"]) {
  367. NSRange selectedRange = textView.selectedRange;
  368. if (selectedRange.length > 0) {
  369. [textView.textStorage deleteCharactersInRange:selectedRange];
  370. }
  371. NSMutableAttributedString *textChange = [text getAdvancedFormatEmojiStringWithFont:kTUIInputNoramlFont
  372. textColor:kTUIInputNormalTextColor
  373. emojiLocations:nil];
  374. [textView.textStorage insertAttributedString:textChange atIndex:textView.textStorage.length];
  375. dispatch_async(dispatch_get_main_queue(), ^{
  376. self.inputTextView.selectedRange = NSMakeRange(self.inputTextView.textStorage.length + 1, 0);
  377. });
  378. return NO;
  379. }
  380. if ([text isEqualToString:@"\n"]) {
  381. if (_delegate && [_delegate respondsToSelector:@selector(inputBar:didSendText:)]) {
  382. NSString *sp = [[textView.textStorage tui_getPlainString] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
  383. if (sp.length == 0) {
  384. UIAlertController *ac = [UIAlertController alertControllerWithTitle:TIMCommonLocalizableString(TUIKitInputBlankMessageTitle)
  385. message:nil
  386. preferredStyle:UIAlertControllerStyleAlert];
  387. [ac tuitheme_addAction:[UIAlertAction actionWithTitle:TIMCommonLocalizableString(Confirm) style:UIAlertActionStyleDefault handler:nil]];
  388. [self.mm_viewController presentViewController:ac animated:YES completion:nil];
  389. } else {
  390. [_delegate inputBar:self didSendText:[textView.textStorage tui_getPlainString]];
  391. [self clearInput];
  392. }
  393. }
  394. return NO;
  395. } else if ([text isEqualToString:@""]) {
  396. if (textView.textStorage.length > range.location) {
  397. // Delete the @ message like @xxx at one time
  398. NSAttributedString *lastAttributedStr = [textView.textStorage attributedSubstringFromRange:NSMakeRange(range.location, 1)];
  399. NSString *lastStr = [lastAttributedStr tui_getPlainString];
  400. if (lastStr && lastStr.length > 0 && [lastStr characterAtIndex:0] == ' ') {
  401. NSUInteger location = range.location;
  402. NSUInteger length = range.length;
  403. // corresponds to ascii code
  404. int at = 64;
  405. // (space) ascii
  406. // Space (space) corresponding ascii code
  407. int space = 32;
  408. while (location != 0) {
  409. location--;
  410. length++;
  411. // Convert characters to ascii code, copy to int, avoid out of bounds
  412. int c = (int)[[[textView.textStorage attributedSubstringFromRange:NSMakeRange(location, 1)] tui_getPlainString] characterAtIndex:0];
  413. if (c == at) {
  414. NSString *atText = [[textView.textStorage attributedSubstringFromRange:NSMakeRange(location, length)] tui_getPlainString];
  415. UIFont *textFont = kTUIInputNoramlFont;
  416. NSAttributedString *spaceString = [[NSAttributedString alloc] initWithString:@"" attributes:@{NSFontAttributeName : textFont}];
  417. [textView.textStorage replaceCharactersInRange:NSMakeRange(location, length) withAttributedString:spaceString];
  418. if (self.delegate && [self.delegate respondsToSelector:@selector(inputBar:didDeleteAt:)]) {
  419. [self.delegate inputBar:self didDeleteAt:atText];
  420. }
  421. return NO;
  422. } else if (c == space) {
  423. // Avoid "@nickname Hello, nice to meet you (space) "" Press del after a space to over-delete to @
  424. break;
  425. }
  426. }
  427. }
  428. }
  429. }
  430. // Monitor the input of @ character, including full-width/half-width
  431. else if ([text isEqualToString:@"@"] || [text isEqualToString:@"@"]) {
  432. if (self.delegate && [self.delegate respondsToSelector:@selector(inputBarDidInputAt:)]) {
  433. [self.delegate inputBarDidInputAt:self];
  434. }
  435. return NO;
  436. }
  437. return YES;
  438. }
  439. - (void)onDeleteBackward:(TUIResponderTextView_Minimalist *)textView {
  440. if (self.delegate && [self.delegate respondsToSelector:@selector(inputBarDidDeleteBackward:)]) {
  441. [self.delegate inputBarDidDeleteBackward:self];
  442. }
  443. }
  444. - (void)clearInput {
  445. [_inputTextView.textStorage deleteCharactersInRange:NSMakeRange(0, _inputTextView.textStorage.length)];
  446. [self textViewDidChange:_inputTextView];
  447. }
  448. - (NSString *)getInput {
  449. return [_inputTextView.textStorage tui_getPlainString];
  450. }
  451. - (void)addEmoji:(TUIFaceCellData *)emoji {
  452. // Create emoji attachment
  453. TUIEmojiTextAttachment *emojiTextAttachment = [[TUIEmojiTextAttachment alloc] init];
  454. emojiTextAttachment.faceCellData = emoji;
  455. // Set tag and image
  456. emojiTextAttachment.emojiTag = emoji.name;
  457. emojiTextAttachment.image = [[TUIImageCache sharedInstance] getFaceFromCache:emoji.path];
  458. // Set emoji size
  459. emojiTextAttachment.emojiSize = kTIMDefaultEmojiSize;
  460. NSAttributedString *str = [NSAttributedString attributedStringWithAttachment:emojiTextAttachment];
  461. NSRange selectedRange = _inputTextView.selectedRange;
  462. if (selectedRange.length > 0) {
  463. [_inputTextView.textStorage deleteCharactersInRange:selectedRange];
  464. }
  465. // Insert emoji image
  466. [_inputTextView.textStorage insertAttributedString:str atIndex:_inputTextView.selectedRange.location];
  467. _inputTextView.selectedRange = NSMakeRange(_inputTextView.selectedRange.location + 1, 0);
  468. [self resetTextStyle];
  469. if (_inputTextView.contentSize.height > TTextView_TextView_Height_Max) {
  470. float offset = _inputTextView.contentSize.height - _inputTextView.frame.size.height;
  471. [_inputTextView scrollRectToVisible:CGRectMake(0, offset, _inputTextView.frame.size.width, _inputTextView.frame.size.height) animated:YES];
  472. }
  473. [self textViewDidChange:_inputTextView];
  474. }
  475. - (void)resetTextStyle {
  476. // After changing text selection, should reset style.
  477. NSRange wholeRange = NSMakeRange(0, _inputTextView.textStorage.length);
  478. [_inputTextView.textStorage removeAttribute:NSFontAttributeName range:wholeRange];
  479. [_inputTextView.textStorage removeAttribute:NSForegroundColorAttributeName range:wholeRange];
  480. [_inputTextView.textStorage addAttribute:NSForegroundColorAttributeName value:kTUIInputNormalTextColor range:wholeRange];
  481. [_inputTextView.textStorage addAttribute:NSFontAttributeName value:kTUIInputNoramlFont range:wholeRange];
  482. [_inputTextView setFont:kTUIInputNoramlFont];
  483. _inputTextView.textAlignment = isRTL()?NSTextAlignmentRight: NSTextAlignmentLeft;
  484. }
  485. - (void)backDelete {
  486. if (_inputTextView.textStorage.length > 0) {
  487. [_inputTextView.textStorage deleteCharactersInRange:NSMakeRange(_inputTextView.textStorage.length - 1, 1)];
  488. [self textViewDidChange:_inputTextView];
  489. }
  490. }
  491. - (void)updateTextViewFrame {
  492. [self textViewDidChange:[UITextView new]];
  493. }
  494. - (void)changeToKeyboard {
  495. [self clickKeyboardBtn:self.keyboardButton];
  496. }
  497. - (void)onThemeChanged {
  498. [self applyBorderTheme];
  499. }
  500. - (void)addDraftToInputBar:(NSAttributedString *)draft {
  501. [self addWordsToInputBar:draft];
  502. }
  503. - (void)addWordsToInputBar:(NSAttributedString *)words {
  504. NSRange selectedRange = self.inputTextView.selectedRange;
  505. if (selectedRange.length > 0) {
  506. [self.inputTextView.textStorage deleteCharactersInRange:selectedRange];
  507. }
  508. // Insert words
  509. [self.inputTextView.textStorage insertAttributedString:words atIndex:self.inputTextView.selectedRange.location];
  510. self.inputTextView.selectedRange = NSMakeRange(self.inputTextView.textStorage.length + 1, 0);
  511. [self resetTextStyle];
  512. [self updateTextViewFrame];
  513. }
  514. #pragma mark - TUIAudioRecorderDelegate
  515. - (void)audioRecorder:(TUIAudioRecorder *)recorder didCheckPermission:(BOOL)isGranted isFirstTime:(BOOL)isFirstTime {
  516. if (isFirstTime) {
  517. if (!isGranted) {
  518. [self showRequestMicAuthorizationAlert];
  519. }
  520. return;
  521. }
  522. [self setRecordStatus:TUIRecordStatus_Record];
  523. _recordStartTime = [NSDate date];
  524. [self showHapticFeedback];
  525. }
  526. - (void)showRequestMicAuthorizationAlert {
  527. UIAlertController *ac = [UIAlertController alertControllerWithTitle:TIMCommonLocalizableString(TUIKitInputNoMicTitle)
  528. message:TIMCommonLocalizableString(TUIKitInputNoMicTips)
  529. preferredStyle:UIAlertControllerStyleAlert];
  530. [ac tuitheme_addAction:[UIAlertAction actionWithTitle:TIMCommonLocalizableString(TUIKitInputNoMicOperateLater) style:UIAlertActionStyleCancel handler:nil]];
  531. [ac tuitheme_addAction:[UIAlertAction actionWithTitle:TIMCommonLocalizableString(TUIKitInputNoMicOperateEnable)
  532. style:UIAlertActionStyleDefault
  533. handler:^(UIAlertAction *_Nonnull action) {
  534. UIApplication *app = [UIApplication sharedApplication];
  535. NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
  536. if ([app canOpenURL:settingsURL]) {
  537. [app openURL:settingsURL];
  538. }
  539. }]];
  540. dispatch_async(dispatch_get_main_queue(), ^{
  541. [self.mm_viewController presentViewController:ac animated:YES completion:nil];
  542. });
  543. }
  544. - (void)audioRecorder:(TUIAudioRecorder *)recorder didRecordTimeChanged:(NSTimeInterval)time {
  545. float maxDuration = MIN(59.7, [TUIChatConfig defaultConfig].maxAudioRecordDuration);
  546. NSInteger seconds = maxDuration - time;
  547. _recordTimeLabel.text = [NSString stringWithFormat:@"%d:%.2d", (int)time / 60, (int)time % 60 + 1];
  548. CGFloat width = _recordAnimateCoverViewFrame.size.width;
  549. int interval_ms = (int)(time * 1000);
  550. int runloop_ms = 5 * 1000;
  551. CGFloat offset_x = width * (interval_ms % runloop_ms) / runloop_ms;
  552. _recordAnimateCoverView.frame = CGRectMake(_recordAnimateCoverViewFrame.origin.x + offset_x, _recordAnimateCoverViewFrame.origin.y, width - offset_x,
  553. _recordAnimateCoverViewFrame.size.height);
  554. if (time > maxDuration) {
  555. [self.recorder stop];
  556. [self setRecordStatus:TUIRecordStatus_Cancel];
  557. NSString *path = self.recorder.recordedFilePath;
  558. if (path) {
  559. if (_delegate && [_delegate respondsToSelector:@selector(inputBar:didSendVoice:)]) {
  560. [_delegate inputBar:self didSendVoice:path];
  561. }
  562. }
  563. }
  564. }
  565. #pragma mark - Getter
  566. - (TUIAudioRecorder *)recorder {
  567. if (!_recorder) {
  568. _recorder = [[TUIAudioRecorder alloc] init];
  569. _recorder.delegate = self;
  570. }
  571. return _recorder;
  572. }
  573. @end