DKMessageInputView.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. //
  2. // DKMessageInputView.m
  3. // MessageDisplayExample
  4. //
  5. // Created by HUAJIE-1 on 14-4-24.
  6. // Copyright (c) 2014年 曾宪华 开发团队(http://iyilunba.com ) 本人QQ:543413507 本人QQ群(142557668). All rights reserved.
  7. //
  8. #import <QuartzCore/QuartzCore.h>
  9. #import "DKMessageInputView.h"
  10. #import "NSString+MessageInputView.h"
  11. #import "DKMacro.h"
  12. #import "DKConfigurationHelper.h"
  13. @interface DKMessageInputView () <UITextViewDelegate>
  14. @property (nonatomic, weak ,readwrite) UIButton *voiceChangeButton;
  15. @property (nonatomic, weak,readwrite) UIButton *cameraButton;
  16. @property (nonatomic, weak,readwrite ) UIButton *holdDownButton;
  17. @property(nonatomic,weak,readwrite )UIButton *photoButton;
  18. @property (nonatomic, weak, readwrite) UIButton *keyboardButton;
  19. @property (nonatomic, weak, readwrite) UIButton *positionButton;
  20. /**
  21. * 是否取消錄音
  22. */
  23. @property (nonatomic, assign, readwrite) BOOL isCancelled;
  24. /**
  25. * 是否正在錄音
  26. */
  27. @property (nonatomic, assign, readwrite) BOOL isRecording;
  28. /**
  29. * 在切换语音和文本消息的时候,需要保存原本已经输入的文本,这样达到一个好的UE
  30. */
  31. @property (nonatomic, copy) NSString *inputedText;
  32. /**
  33. * 输入框内的所有按钮,点击事件所触发的方法
  34. *
  35. * @param sender 被点击的按钮对象
  36. */
  37. - (void)messageStyleButtonClicked:(UIButton *)sender;
  38. /**
  39. * 当录音按钮被按下所触发的事件,这时候是开始录音
  40. */
  41. - (void)holdDownButtonTouchDown;
  42. /**
  43. * 当手指在录音按钮范围之外离开屏幕所触发的事件,这时候是取消录音
  44. */
  45. - (void)holdDownButtonTouchUpOutside;
  46. /**
  47. * 当手指在录音按钮范围之内离开屏幕所触发的事件,这时候是完成录音
  48. */
  49. - (void)holdDownButtonTouchUpInside;
  50. /**
  51. * 当手指滑动到录音按钮的范围之外所触发的事件
  52. */
  53. - (void)holdDownDragOutside;
  54. /**
  55. * 当手指滑动到录音按钮的范围之内所触发的时间
  56. */
  57. - (void)holdDownDragInside;
  58. #pragma mark - layout subViews UI
  59. /**
  60. * 根据正常显示和高亮状态创建一个按钮对象
  61. *
  62. * @param image 正常显示图
  63. * @param hlImage 高亮显示图
  64. *
  65. * @return 返回按钮对象
  66. */
  67. - (UIButton *)createButtonWithImage:(UIImage *)image HLImage:(UIImage *)hlImage ;
  68. /**
  69. * 根据输入框的样式类型配置输入框的样式和UI布局
  70. *
  71. * @param style 输入框样式类型
  72. */
  73. - (void)setupMessageInputViewBarWithStyle:(DKMessageInputViewStyle)style ;
  74. /**
  75. * 配置默认参数
  76. */
  77. - (void)setup ;
  78. @end
  79. @implementation DKMessageInputView
  80. #pragma mark - Action
  81. - (void)messageStyleButtonClicked:(UIButton *)sender {
  82. NSInteger index = sender.tag;
  83. switch (index) {
  84. case 0: {
  85. if ([self.delegate respondsToSelector:@selector(saveLog)]) {
  86. [self.delegate saveLog];
  87. }
  88. break;
  89. }
  90. case 1: {
  91. _voiceButton.hidden=YES;
  92. _switchButton.hidden=YES;
  93. _saveButton.hidden=NO;
  94. break;
  95. }
  96. default:
  97. break;
  98. }
  99. }
  100. - (void)holdDownButtonTouchDown {
  101. self.isCancelled = NO;
  102. self.isRecording = NO;
  103. if ([self.delegate respondsToSelector:@selector(prepareRecordingVoiceActionWithCompletion:)]) {
  104. WEAKSELF
  105. //這邊回調 return 的 YES, 或 NO, 可以讓底層知道該次錄音是否成功, 進而處理無用的 record 對象
  106. [self.delegate prepareRecordingVoiceActionWithCompletion:^BOOL{
  107. STRONGSELF
  108. //這邊要判斷回調回來的時候, 使用者是不是已經早就鬆開手了
  109. if (strongSelf && !strongSelf.isCancelled) {
  110. strongSelf.isRecording = YES;
  111. [strongSelf.delegate didStartRecordingVoiceAction];
  112. return YES;
  113. } else {
  114. return NO;
  115. }
  116. }];
  117. }
  118. }
  119. - (void)holdDownButtonTouchUpOutside {
  120. //如果已經開始錄音了, 才需要做取消的動作, 否則只要切換 isCancelled, 不讓錄音開始.
  121. if (self.isRecording) {
  122. if ([self.delegate respondsToSelector:@selector(didCancelRecordingVoiceAction)]) {
  123. [self.delegate didCancelRecordingVoiceAction];
  124. }
  125. } else {
  126. self.isCancelled = YES;
  127. }
  128. }
  129. - (void)holdDownButtonTouchUpInside {
  130. //如果已經開始錄音了, 才需要做結束的動作, 否則只要切換 isCancelled, 不讓錄音開始.
  131. if (self.isRecording) {
  132. if ([self.delegate respondsToSelector:@selector(didFinishRecordingVoiceAction)]) {
  133. [self.delegate didFinishRecordingVoiceAction];
  134. }
  135. } else {
  136. self.isCancelled = YES;
  137. }
  138. }
  139. - (void)holdDownDragOutside {
  140. //如果已經開始錄音了, 才需要做拖曳出去的動作, 否則只要切換 isCancelled, 不讓錄音開始.
  141. if (self.isRecording) {
  142. if ([self.delegate respondsToSelector:@selector(didDragOutsideAction)]) {
  143. [self.delegate didDragOutsideAction];
  144. }
  145. } else {
  146. self.isCancelled = YES;
  147. }
  148. }
  149. - (void)holdDownDragInside {
  150. //如果已經開始錄音了, 才需要做拖曳回來的動作, 否則只要切換 isCancelled, 不讓錄音開始.
  151. if (self.isRecording) {
  152. if ([self.delegate respondsToSelector:@selector(didDragInsideAction)]) {
  153. [self.delegate didDragInsideAction];
  154. }
  155. } else {
  156. self.isCancelled = YES;
  157. }
  158. }
  159. #pragma mark - layout subViews UI
  160. - (UIButton *)createButtonWithImage:(UIImage *)image HLImage:(UIImage *)hlImage {
  161. UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, [DKMessageInputView textViewLineHeight], [DKMessageInputView textViewLineHeight])];
  162. if (image)
  163. [button setBackgroundImage:image forState:UIControlStateNormal];
  164. if (hlImage)
  165. [button setBackgroundImage:hlImage forState:UIControlStateHighlighted];
  166. return button;
  167. }
  168. #pragma mark - layout subViews UI
  169. - (UIButton *)createVoiceButtonWithImage:(UIImage *)image HLImage:(UIImage *)hlImage {
  170. UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 5,200, 36)];
  171. button.layer.borderWidth=1;
  172. button.layer.borderColor=[UIColor lightGrayColor].CGColor;
  173. button.layer.cornerRadius=10;
  174. // if (image)
  175. // [button setBackgroundImage:image forState:UIControlStateNormal];
  176. // if (hlImage)
  177. // [button setBackgroundImage:hlImage forState:UIControlStateHighlighted];
  178. return button;
  179. }
  180. #pragma mark - layout subViews UI
  181. - (UIButton *)createSaveButtonWithImage:(UIImage *)image HLImage:(UIImage *)hlImage {
  182. float width =[DKMessageInputView textViewLineHeight]*image.size.width/image.size.height;
  183. UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake((Screen_Width-width)/2, 5,width, [DKMessageInputView textViewLineHeight])];
  184. if (image)
  185. [button setBackgroundImage:image forState:UIControlStateNormal];
  186. if (hlImage)
  187. [button setBackgroundImage:hlImage forState:UIControlStateHighlighted];
  188. return button;
  189. }
  190. - (void)setupMessageInputViewBarWithStyle:(DKMessageInputViewStyle)style {
  191. // 配置输入工具条的样式和布局
  192. // 需要显示按钮的总宽度,包括间隔在内
  193. CGFloat allButtonWidth = 0.0;
  194. // 垂直间隔
  195. CGFloat verticalPadding = 5;
  196. // 输入框
  197. CGFloat textViewLeftMargin = ((style == DKMessageInputViewStyleFlat) ? 6.0 : 4.0);
  198. // 每个按钮统一使用的frame变量
  199. CGRect buttonFrame;
  200. // 允许发送语音
  201. NSString *voiceNormalImageName = [[DKConfigurationHelper appearance].messageInputViewStyle objectForKey:kDKMessageInputViewVoiceNormalImageNameKey];
  202. if (!voiceNormalImageName) {
  203. voiceNormalImageName = @"keyboard";
  204. }
  205. NSString *voiceHLImageName = [[DKConfigurationHelper appearance].messageInputViewStyle objectForKey:kDKMessageInputViewVoiceHLImageNameKey];
  206. if (!voiceHLImageName) {
  207. voiceHLImageName = @"keyboard";
  208. }
  209. NSString *voiceSaveImageName=@"log_save";
  210. _saveButton =[self createSaveButtonWithImage:[UIImage imageNamed:voiceSaveImageName] HLImage:[UIImage imageNamed:voiceSaveImageName]];
  211. _saveButton.tag = 0;
  212. [_saveButton addTarget:self action:@selector(messageStyleButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
  213. [self addSubview:_saveButton];
  214. _switchButton = [self createButtonWithImage:[UIImage imageNamed:voiceNormalImageName] HLImage:[UIImage imageNamed:voiceHLImageName]];
  215. [_switchButton addTarget:self action:@selector(messageStyleButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
  216. _switchButton.tag = 1;
  217. _switchButton.hidden=YES;
  218. buttonFrame = _switchButton.frame;
  219. buttonFrame.origin = CGPointMake(20, verticalPadding);
  220. _switchButton.frame = buttonFrame;
  221. [self addSubview:_switchButton];
  222. allButtonWidth += CGRectGetMaxX(buttonFrame);
  223. textViewLeftMargin += CGRectGetMaxX(buttonFrame);
  224. self.voiceChangeButton = _switchButton;
  225. // // 输入框的高度和宽度
  226. // CGFloat width = CGRectGetWidth(self.bounds) - (allButtonWidth ? allButtonWidth : (textViewLeftMargin * 2));
  227. // 配置不同iOS SDK版本的样式
  228. switch (style) {
  229. case DKMessageInputViewStyleFlat: {
  230. UIColor *inputBackgroundColor = [DKConfigurationHelper appearance].messageInputViewStyle[kDKMessageInputViewBackgroundColorKey];
  231. NSString *inputViewBackgroundImageName = nil;
  232. if (!inputBackgroundColor) {
  233. inputViewBackgroundImageName = [[DKConfigurationHelper appearance].messageInputViewStyle objectForKey:kDKMessageInputViewBackgroundImageNameKey];
  234. if (!inputViewBackgroundImageName) {
  235. inputViewBackgroundImageName = @"input-bar-flat";
  236. }
  237. }
  238. UIColor *borderColor = [[DKConfigurationHelper appearance].messageInputViewStyle objectForKey:kDKMessageInputViewBorderColorKey];
  239. if (!borderColor) {
  240. borderColor=LineBackgroundColor;
  241. }
  242. CGFloat borderWidth = [[[DKConfigurationHelper appearance].messageInputViewStyle objectForKey:kDKMessageInputViewBorderWidthKey] floatValue];
  243. if (borderWidth == 0) {
  244. borderWidth = 0.65f;
  245. }
  246. CGFloat cornerRadius = [[[DKConfigurationHelper appearance].messageInputViewStyle objectForKey:kDKMessageInputViewCornerRadiusKey] floatValue];
  247. if (cornerRadius == 0) {
  248. cornerRadius = 6.0f;
  249. }
  250. if (inputBackgroundColor) {
  251. self.backgroundColor = inputBackgroundColor;
  252. } else {
  253. self.image = [[UIImage imageNamed:inputViewBackgroundImageName] resizableImageWithCapInsets:UIEdgeInsetsMake(2.0f, 0.0f, 0.0f, 0.0f)
  254. resizingMode:UIImageResizingModeTile];
  255. }
  256. break;
  257. }
  258. default:
  259. break;
  260. }
  261. // 如果是可以发送语音的,那就需要一个按钮录音的按钮,事件可以在外部添加
  262. NSString *voiceHolderImageName = [[DKConfigurationHelper appearance].messageInputViewStyle objectForKey:kDKMessageInputViewVoiceHolderImageNameKey];
  263. voiceHolderImageName = @"voicebtn_black";
  264. NSString *voiceHolderHLImageName = [[DKConfigurationHelper appearance].messageInputViewStyle objectForKey:kDKMessageInputViewVoiceHolderHLImageNameKey];
  265. voiceHolderHLImageName = @"voicebtn_blackhl";
  266. UIEdgeInsets edgeInsets = UIEdgeInsetsMake(9, 9, 9, 9);
  267. _voiceButton = [self createVoiceButtonWithImage:DK_STRETCH_IMAGE([UIImage imageNamed:voiceHolderImageName], edgeInsets) HLImage:DK_STRETCH_IMAGE([UIImage imageNamed:voiceHolderHLImageName], edgeInsets)];
  268. _voiceButton.hidden=YES;
  269. //按住说话
  270. [_voiceButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
  271. [_voiceButton setTitle:@"按住说话" forState:UIControlStateNormal];
  272. [_voiceButton setTitle:@"放开发送" forState:UIControlStateHighlighted];
  273. CGRect btnFrame = CGRectMake(CGRectGetMaxX(_switchButton.frame)+30,5,Screen_Width-CGRectGetMaxX(_switchButton.frame)-60,36);
  274. _voiceButton.frame = btnFrame;
  275. _voiceButton.tag=2;
  276. [_voiceButton addTarget:self action:@selector(holdDownButtonTouchDown) forControlEvents:UIControlEventTouchDown];
  277. [_voiceButton addTarget:self action:@selector(holdDownButtonTouchUpOutside) forControlEvents:UIControlEventTouchUpOutside];
  278. [_voiceButton addTarget:self action:@selector(holdDownButtonTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
  279. [_voiceButton addTarget:self action:@selector(holdDownDragOutside) forControlEvents:UIControlEventTouchDragExit];
  280. [_voiceButton addTarget:self action:@selector(holdDownDragInside) forControlEvents:UIControlEventTouchDragEnter];
  281. [self addSubview:_voiceButton];
  282. self.holdDownButton =_voiceButton;
  283. }
  284. #pragma mark - Life cycle
  285. - (void)setup {
  286. // 配置自适应
  287. self.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin);
  288. self.opaque = YES;
  289. // 由于继承UIImageView,所以需要这个属性设置
  290. self.userInteractionEnabled = YES;
  291. _allowsPosition=YES;
  292. _messageInputViewStyle = DKMessageInputViewStyleFlat;
  293. }
  294. - (void)awakeFromNib {
  295. [super awakeFromNib];
  296. [self setup];
  297. }
  298. - (instancetype)initWithFrame:(CGRect)frame {
  299. self = [super initWithFrame:frame];
  300. if (self) {
  301. [self setup];
  302. }
  303. return self;
  304. }
  305. - (void)dealloc {
  306. self.inputedText = nil;
  307. _voiceChangeButton = nil;
  308. _cameraButton = nil;
  309. _photoButton = nil;
  310. _holdDownButton = nil;
  311. _positionButton=nil;
  312. _keyboardButton=nil;
  313. }
  314. - (void)willMoveToSuperview:(UIView *)newSuperview {
  315. // 当别的地方需要add的时候,就会调用这里
  316. if (newSuperview) {
  317. [self setupMessageInputViewBarWithStyle:self.messageInputViewStyle];
  318. }
  319. }
  320. + (CGFloat)textViewLineHeight {
  321. return 36.0f; // for fontSize 16.0f
  322. }
  323. + (CGFloat)textViewLineWidth {
  324. return Screen_Width-40; // for fontSize 16.0f
  325. }
  326. + (CGFloat)maxLines {
  327. return ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone) ? 3.0f : 8.0f;
  328. }
  329. + (CGFloat)maDKeight {
  330. return ([DKMessageInputView maxLines] + 1.0f) * [DKMessageInputView textViewLineHeight];
  331. }
  332. @end