chatCellView.m 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775
  1. //
  2. // chatCellView.m
  3. // AIIM
  4. //
  5. // Created by gan on 2025/6/24.
  6. //
  7. #import <Foundation/Foundation.h>
  8. #import "chatCellView.h"
  9. #import "ChatLongPressMenuView.h"
  10. #import "ChatPopoverView.h"
  11. #import "CryptoAES.h"
  12. #import <SDWebImage/UIImageView+WebCache.h>
  13. #import <SDWebImage/SDWebImage.h>
  14. #import <AVFoundation/AVFoundation.h>
  15. #import <AVKit/AVKit.h>
  16. #import "config.h"
  17. #import "FilePreviewer.h"
  18. #import "ChatRecordController.h"
  19. #import "QuoteDetailController.h"
  20. #import "UILabel+YBAttributeTextTapAction.h"
  21. #import <SafariServices/SFSafariViewController.h>
  22. // 定义一些常量
  23. static const CGFloat kTextVerticalMargin = 10.0f; // 内容垂直边距
  24. static const CGFloat kTextHorizontalMargin = 12.0f; //文本水平边距
  25. static const CGFloat kVerticalMargin = 18.0f; // 垂直边距
  26. static const CGFloat kContentMaxWidth = 220.0f; // 内容最大宽度
  27. static const CGFloat kAvatarSize = 36.0f; // 头像大小
  28. static const CGFloat kAvatarMargin = 20.0f; // 头像边距
  29. static const CGFloat kAvatarBatchMargin = 70.0f; // 批量操作头像边距
  30. static const CGFloat kBubbleTopMargin = 20.0f; // 气泡顶部距离
  31. static const CGFloat kBubbleBottomMargin = 40.0f; // 气泡底部距离
  32. static const CGFloat kBubbleArrowWidth = 6.0f; // 气泡箭头宽度
  33. static const CGFloat kMinBubbleHeight = 18.0f; // 气泡最小高度
  34. static const CGFloat kMediaCornerRadius = 4.0f;
  35. @interface chatCellView()
  36. // 基本视图
  37. @property (nonatomic, strong) UIImageView *avatarImageView;
  38. @property (nonatomic, strong) UILabel *nameLabel;
  39. @property (nonatomic, strong) UIImageView *bubbleImageView;
  40. @property (nonatomic, strong) UILabel * timeLabel;
  41. @property (nonatomic, strong) UILabel * readStateLbl;
  42. @property (nonatomic, strong) UIButton * batchStateBtn;
  43. // 内容视图
  44. @property (nonatomic, strong) UILabel *textLbl;
  45. @property (nonatomic, strong) UIImageView *imageContentView;
  46. @property (nonatomic, strong) UIView *videoContentView;
  47. @property (nonatomic, strong) UIImageView *thumbnailView;
  48. @property (nonatomic, strong) UIImageView *loadingImg;
  49. @property (nonatomic, strong) UIView *fileContentView;
  50. @property (nonatomic, strong) UIView *voiceContentView;
  51. @property (nonatomic, strong) UIView *callContentView;
  52. @property (nonatomic, strong) UIView *historyContentView;
  53. @property (nonatomic, strong) UILabel * eventLbl;
  54. @property (nonatomic, strong) UILabel * callbackLbl;
  55. @property (nonatomic, strong) UILabel * deleateLbl;
  56. @property (nonatomic, strong) UILabel * quoteLbl;
  57. @property (nonatomic, strong) UILabel * jinduLbl;
  58. @property (strong, nonatomic) AVPlayer *player;
  59. @property (strong, nonatomic) AVPlayerViewController *playerViewController;
  60. // 当前显示的内容视图
  61. @property (nonatomic, weak) UIView *currentContentView;
  62. @property (nonatomic,strong) NSArray *matchesArray;
  63. @end
  64. @implementation chatCellView
  65. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  66. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  67. if (self) {
  68. [self initAlllSubviews];
  69. self.selectionStyle = UITableViewCellSelectionStyleNone;
  70. self.backgroundColor = [UIColor clearColor];
  71. }
  72. return self;
  73. }
  74. -(void)initAlllSubviews{
  75. //复选框
  76. _batchStateBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  77. [_batchStateBtn setBackgroundImage:kImageMake(@"checkbox_unselected") forState:UIControlStateNormal];
  78. [_batchStateBtn setBackgroundImage:kImageMake(@"checkbox_selected") forState:UIControlStateSelected];
  79. _batchStateBtn.hidden = YES;
  80. [self.contentView addSubview:_batchStateBtn];
  81. [_batchStateBtn addTarget:self action:@selector(batchStateButtonClicked) forControlEvents:UIControlEventTouchUpInside];
  82. // 头像
  83. _avatarImageView = [UIImageView new];
  84. _avatarImageView.layer.cornerRadius = kAvatarSize / 2;
  85. _avatarImageView.layer.masksToBounds = YES;
  86. _avatarImageView.userInteractionEnabled = YES;
  87. _avatarImageView.contentMode = UIViewContentModeScaleAspectFill;
  88. [self.contentView addSubview:_avatarImageView];
  89. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleAvatarTap)];
  90. [_avatarImageView addGestureRecognizer:tap];
  91. // [_avatarImageView jk_addLongPressActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
  92. // [self handleAvatarLongPress];
  93. // }];
  94. // 名字
  95. _nameLabel = [UILabel new];
  96. _nameLabel.font = SYSFONT(12);
  97. _nameLabel.textColor = [UIColor whiteColor];
  98. [self.contentView addSubview:_nameLabel];
  99. // 时间
  100. _timeLabel = [UILabel new];
  101. _timeLabel.font = SYSFONT(12);
  102. _timeLabel.textColor = UIColor.whiteColor;
  103. [self.contentView addSubview:_timeLabel];
  104. _readStateLbl = [UILabel new];
  105. _readStateLbl.font = SYSFONT(12);
  106. _readStateLbl.textColor = UIColor.whiteColor;
  107. [self.contentView addSubview:_readStateLbl];
  108. // 气泡背景
  109. _bubbleImageView = [UIImageView new];
  110. _bubbleImageView.userInteractionEnabled = YES;
  111. [self.contentView addSubview:_bubbleImageView];
  112. // 初始化所有内容视图
  113. [self setupTextContentView];
  114. [self setupImageContentView];
  115. [self setupVideoContentView];
  116. [self setupFileContentView];
  117. [self setupVoiceContentView];
  118. [self setupCallContentView];
  119. [self setupHistoryContentView];
  120. [self setupEventContentView];
  121. [self setupCallbackContentView];
  122. [self setupdeleateContentView];
  123. }
  124. #pragma mark - 内容视图配置
  125. - (void)setupTextContentView {
  126. _textLbl = [UILabel new];
  127. _textLbl.numberOfLines = 0;
  128. _textLbl.textColor = UIColor.blackColor;
  129. _textLbl.userInteractionEnabled = YES; // 允许用户交互
  130. [self.contentView addSubview:_textLbl];
  131. // UITapGestureRecognizer *taptext = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTapped:)];
  132. // [_textLbl addGestureRecognizer:taptext];
  133. _quoteLbl = [UILabel new];
  134. _quoteLbl.numberOfLines = 1;
  135. _quoteLbl.font = SYSFONT(16);
  136. _quoteLbl.textColor = UIColor.whiteColor;
  137. _quoteLbl.layer.cornerRadius = 2.f;
  138. _quoteLbl.layer.masksToBounds = YES;
  139. _quoteLbl.userInteractionEnabled = YES; // 允许用户交互
  140. [self.contentView addSubview:_quoteLbl];
  141. // _quoteLbl.backgroundColor = [UIColor redColor];
  142. // 添加点击手势
  143. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleQuoteTap)];
  144. [_quoteLbl addGestureRecognizer:tap];
  145. [self.contentView bringSubviewToFront:_quoteLbl];
  146. }
  147. - (void)setupImageContentView {
  148. _imageContentView = [UIImageView new];
  149. _imageContentView.contentMode = UIViewContentModeScaleAspectFit;
  150. _imageContentView.clipsToBounds = YES;
  151. _imageContentView.layer.cornerRadius = kMediaCornerRadius;
  152. _imageContentView.layer.masksToBounds = YES;
  153. _imageContentView.userInteractionEnabled = YES;
  154. [self.contentView addSubview:_imageContentView];
  155. // 添加点击手势
  156. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleImageTap)];
  157. [_imageContentView addGestureRecognizer:tap];
  158. }
  159. - (void)setupVideoContentView {
  160. _videoContentView = [UIView new];
  161. _videoContentView.layer.cornerRadius = kMediaCornerRadius;
  162. _videoContentView.layer.masksToBounds = YES;
  163. _videoContentView.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1.0];
  164. _videoContentView.userInteractionEnabled = YES;
  165. _thumbnailView = [[UIImageView alloc] init];
  166. _thumbnailView.contentMode = UIViewContentModeScaleAspectFill;
  167. _thumbnailView.clipsToBounds = YES;
  168. _thumbnailView.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1.0];
  169. [_videoContentView addSubview:_thumbnailView];
  170. [_thumbnailView mas_makeConstraints:^(MASConstraintMaker *make) {
  171. make.edges.equalTo(_videoContentView);
  172. }];
  173. NSString *gifPath = [[NSBundle mainBundle] pathForResource:@"load" ofType:@"gif"];
  174. NSData *gifData = [NSData dataWithContentsOfFile:gifPath];
  175. UIImage *placeholder = [UIImage sd_imageWithGIFData:gifData];
  176. _loadingImg = [[UIImageView alloc] initWithImage:placeholder];
  177. [_videoContentView addSubview:_loadingImg];
  178. [_loadingImg mas_makeConstraints:^(MASConstraintMaker *make) {
  179. make.center.equalTo(_videoContentView);
  180. make.width.height.mas_equalTo(100);
  181. }];
  182. _loadingImg.alpha = 0;
  183. UIImageView *playIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"play_btn"]];
  184. [_videoContentView addSubview:playIcon];
  185. [playIcon mas_makeConstraints:^(MASConstraintMaker *make) {
  186. make.center.equalTo(_videoContentView);
  187. make.width.height.mas_equalTo(40);
  188. }];
  189. _jinduLbl = [UILabel new];
  190. [_videoContentView addSubview:_jinduLbl];
  191. _jinduLbl.alpha = 0;
  192. _jinduLbl.numberOfLines = 1;
  193. _jinduLbl.font = SYSFONT(16);
  194. _jinduLbl.textColor = UIColor.whiteColor;
  195. _jinduLbl.layer.cornerRadius = 20.f;
  196. _jinduLbl.layer.masksToBounds = YES;
  197. _jinduLbl.textAlignment = NSTextAlignmentCenter;
  198. _jinduLbl.backgroundColor =[UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
  199. _jinduLbl.text = @"99";
  200. [_jinduLbl mas_makeConstraints:^(MASConstraintMaker *make) {
  201. make.left.equalTo(_videoContentView).offset(8);
  202. make.top.equalTo(_videoContentView).offset(8);
  203. make.width.height.mas_equalTo(40);
  204. }];
  205. [self.contentView addSubview:_videoContentView];
  206. // 添加点击手势
  207. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleVideoTap)];
  208. [_videoContentView addGestureRecognizer:tap];
  209. }
  210. - (void)setupFileContentView {
  211. _fileContentView = [UIView new];
  212. UIImageView *iconView = [[UIImageView alloc] init];
  213. UILabel *nameLabel = [[UILabel alloc] init];
  214. nameLabel.textColor = UIColor.blackColor;
  215. nameLabel.font = [UIFont systemFontOfSize:14];
  216. nameLabel.numberOfLines = 1;
  217. UILabel *sizeLabel = [[UILabel alloc] init];
  218. sizeLabel.font = [UIFont systemFontOfSize:12];
  219. sizeLabel.textColor = [UIColor grayColor];
  220. [_fileContentView addSubview:iconView];
  221. [_fileContentView addSubview:nameLabel];
  222. [_fileContentView addSubview:sizeLabel];
  223. [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
  224. make.left.equalTo(_fileContentView).offset(8);
  225. make.centerY.equalTo(_fileContentView);
  226. make.width.height.mas_equalTo(40);
  227. }];
  228. [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  229. make.left.equalTo(iconView.mas_right).offset(8);
  230. make.top.equalTo(_fileContentView).offset(8);
  231. make.right.equalTo(_fileContentView).offset(-8);
  232. }];
  233. [sizeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  234. make.left.equalTo(nameLabel);
  235. make.top.equalTo(nameLabel.mas_bottom).offset(4);
  236. // make.right.equalTo(nameLabel);
  237. // make.bottom.lessThanOrEqualTo(_fileContentView).offset(-8);
  238. }];
  239. [self.contentView addSubview:_fileContentView];
  240. // 添加点击手势
  241. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleFileTap)];
  242. [_fileContentView addGestureRecognizer:tap];
  243. }
  244. - (void)setupVoiceContentView {
  245. _voiceContentView = [UIView new];
  246. UIImageView *voiceIcon = [[UIImageView alloc] initWithImage:kImageMake(@"play_btn")];
  247. UILabel *durationLabel = [[UILabel alloc] init];
  248. durationLabel.textColor = UIColor.blackColor;
  249. durationLabel.font = [UIFont systemFontOfSize:14];
  250. [_voiceContentView addSubview:voiceIcon];
  251. [_voiceContentView addSubview:durationLabel];
  252. [voiceIcon mas_makeConstraints:^(MASConstraintMaker *make) {
  253. make.centerY.equalTo(_voiceContentView);
  254. make.width.height.mas_equalTo(20);
  255. }];
  256. [durationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  257. make.centerY.equalTo(_voiceContentView);
  258. make.left.equalTo(voiceIcon.mas_right).offset(8);
  259. }];
  260. [self.contentView addSubview:_voiceContentView];
  261. // 添加点击手势
  262. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleVoiceTap)];
  263. [_voiceContentView addGestureRecognizer:tap];
  264. }
  265. - (void)setupCallContentView {
  266. _callContentView = [UIView new];
  267. UIImageView *callIcon = [[UIImageView alloc] init];
  268. UILabel *callLabel = [[UILabel alloc] init];
  269. callLabel.textColor = UIColor.blackColor;
  270. callLabel.font = [UIFont systemFontOfSize:16];
  271. UILabel *durationLabel = [[UILabel alloc] init];
  272. durationLabel.font = [UIFont systemFontOfSize:16];
  273. durationLabel.textColor = [UIColor grayColor];
  274. [_callContentView addSubview:callIcon];
  275. [_callContentView addSubview:callLabel];
  276. [_callContentView addSubview:durationLabel];
  277. // [callIcon mas_makeConstraints:^(MASConstraintMaker *make) {
  278. // make.left.equalTo(_callContentView).offset(8);
  279. // make.centerY.equalTo(_callContentView);
  280. // make.width.height.mas_equalTo(20);
  281. // }];
  282. //
  283. // [callLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  284. // make.left.equalTo(callIcon.mas_right).offset(8);
  285. // make.top.equalTo(_callContentView).offset(8);
  286. //// make.width.mas_lessThanOrEqualTo(70);
  287. //// make.right.equalTo(_callContentView).offset(-8);
  288. // }];
  289. //
  290. // [durationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  291. // make.left.equalTo(callLabel.mas_right).offset(8);
  292. // make.top.equalTo(callLabel);
  293. // make.right.equalTo(_callContentView).offset(-8);
  294. // make.bottom.equalTo(_callContentView).offset(-8);
  295. // }];
  296. [self.contentView addSubview:_callContentView];
  297. // 添加点击手势
  298. // UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleCallTap)];
  299. // [_callContentView addGestureRecognizer:tap];
  300. }
  301. - (void)setupHistoryContentView {
  302. _historyContentView = [UIView new];
  303. UILabel *titleLabel = [[UILabel alloc] init];
  304. titleLabel.font = SYSBFONT(14);
  305. titleLabel.text = @"聊天记录";
  306. titleLabel.textColor = UIColor.blackColor;
  307. UILabel *contentLabel = [[UILabel alloc] init];
  308. contentLabel.font = SYSFONT(12);
  309. contentLabel.numberOfLines = 6;
  310. // contentLabel.displaysAsynchronously = YES;
  311. [_historyContentView addSubview:titleLabel];
  312. [_historyContentView addSubview:contentLabel];
  313. [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  314. make.left.top.equalTo(_historyContentView).offset(8);
  315. make.right.equalTo(_historyContentView).offset(-8);
  316. make.height.mas_equalTo(14);
  317. }];
  318. [contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  319. make.left.right.equalTo(titleLabel);
  320. make.top.equalTo(titleLabel.mas_bottom).offset(4);
  321. make.bottom.equalTo(_historyContentView).offset(-8);
  322. }];
  323. [self.contentView addSubview:_historyContentView];
  324. // 添加点击手势
  325. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleHistoryTap)];
  326. [_historyContentView addGestureRecognizer:tap];
  327. }
  328. - (void)setupEventContentView {
  329. _eventLbl = [[UILabel alloc] init];
  330. _eventLbl.font = SYSFONT(14);
  331. _eventLbl.textColor = UIColor.whiteColor;
  332. _eventLbl.layer.cornerRadius = 2.f;
  333. _eventLbl.layer.masksToBounds = YES;
  334. [self.contentView addSubview:_eventLbl];
  335. }
  336. - (void)setupCallbackContentView {
  337. _callbackLbl = [[UILabel alloc] init];
  338. _callbackLbl.font = SYSFONT(14);
  339. _callbackLbl.textColor = UIColor.whiteColor;
  340. _callbackLbl.layer.cornerRadius = 2.f;
  341. _callbackLbl.layer.masksToBounds = YES;
  342. [self.contentView addSubview:_callbackLbl];
  343. }
  344. - (void)setupdeleateContentView {
  345. _deleateLbl = [[UILabel alloc] init];
  346. _deleateLbl.font = SYSFONT(14);
  347. _deleateLbl.textColor = UIColor.blackColor;
  348. _deleateLbl.layer.cornerRadius = 2.f;
  349. _deleateLbl.layer.masksToBounds = YES;
  350. [self.contentView addSubview:_deleateLbl];
  351. }
  352. #pragma mark - 数据绑定
  353. - (void)setMessageModel:(ChatMessageModel *)messageModel {
  354. // NSLog(@"messageModel:%@",messageModel);
  355. _messageModel = messageModel;
  356. // 隐藏所有内容视图
  357. _textLbl.hidden = YES;
  358. _quoteLbl.hidden = YES;
  359. _imageContentView.hidden = YES;
  360. _videoContentView.hidden = YES;
  361. _fileContentView.hidden = YES;
  362. _voiceContentView.hidden = YES;
  363. _callContentView.hidden = YES;
  364. _historyContentView.hidden = YES;
  365. _eventLbl.hidden = YES;
  366. _callbackLbl.hidden = YES;
  367. _deleateLbl.hidden = YES;
  368. _readStateLbl.hidden = YES;
  369. // 根据消息类型配置内容
  370. switch (messageModel.messageType) {
  371. case ChatMessageTypeText:
  372. [self setupTextContent];
  373. break;
  374. case ChatMessageTypeImage:
  375. [self setupImageContent];
  376. break;
  377. case ChatMessageTypeVideo:
  378. [self setupVideoContent];
  379. break;
  380. case ChatMessageTypeFile:
  381. [self setupFileContent];
  382. break;
  383. case ChatMessageTypeCallBack:
  384. //撤回
  385. [self setupCallbackContent];
  386. break;
  387. case ChatMessageTypeVoice:
  388. [self setupVoiceContent];
  389. break;
  390. case ChatMessageTypeCall:
  391. [self setupCallContent];
  392. break;
  393. case ChatMessageTypeHistory:
  394. [self setupHistoryContent];
  395. break;
  396. case ChatMessageTypeEvent:
  397. [self setupEventContent];
  398. break;
  399. case ChatMessageTypeCallBack2:
  400. //撤回PC
  401. [self setupCallbackContent];
  402. break;
  403. case ChatMessageTypeDel:
  404. [self setupDeleateContent];
  405. break;
  406. }
  407. // 配置头像和名字
  408. [self setupAvatarAndName];
  409. // 配置气泡背景
  410. [self setupBubbleBackground];
  411. // 更新布局
  412. [self layoutContentViews];
  413. //长按事件
  414. [self setupLongPressEvent];
  415. }
  416. #pragma mark - 批量状态
  417. - (void)setBatchState:(BOOL)batchState{
  418. if (_messageModel.messageType == ChatMessageTypeEvent || _messageModel.messageType == ChatMessageTypeCallBack || _messageModel.messageType == ChatMessageTypeCallBack2) {
  419. self.batchStateBtn.hidden = YES;
  420. return;
  421. }
  422. _batchState = batchState;
  423. self.batchStateBtn.hidden = !batchState;
  424. if (!_messageModel.isSender) {
  425. if (batchState) {
  426. [_avatarImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  427. make.size.mas_equalTo(CGSizeMake(kAvatarSize, kAvatarSize));
  428. make.left.mas_equalTo(kAvatarBatchMargin);
  429. make.top.equalTo(self.contentView).offset(kVerticalMargin);
  430. }];
  431. }else{
  432. [_avatarImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  433. make.size.mas_equalTo(CGSizeMake(kAvatarSize, kAvatarSize));
  434. make.left.mas_equalTo(kAvatarMargin);
  435. make.top.equalTo(self.contentView).offset(kVerticalMargin);
  436. }];
  437. }
  438. }
  439. self.batchStateBtn.selected = NO;
  440. }
  441. - (void)setupAvatarAndName {
  442. self.readStateLbl.text = nil;
  443. // 这里应该根据实际数据设置头像和名字
  444. if (_messageModel.avatar) {
  445. [_avatarImageView sd_setImageWithURL:getURL(_messageModel.avatar) placeholderImage:kImageMake(@"Avatar")];
  446. }else{
  447. _avatarImageView.image = kImageMake(@"Avatar");
  448. }
  449. _timeLabel.text = _messageModel.formatterTime;
  450. _readStateLbl.text = _messageModel.readStatus;
  451. if(_messageModel.readstate==4){
  452. _readStateLbl.textColor = globalColor(GCTypeOrangeR);
  453. }
  454. else{
  455. _readStateLbl.textColor = [UIColor whiteColor];
  456. }
  457. if (_messageModel.type==1) {
  458. if (_messageModel.nickName && ![_messageModel.nickName isKindOfClass:NSNull.class]) {
  459. _nameLabel.text = _messageModel.isSender ? @"": _messageModel.nickName;
  460. _nameLabel.textAlignment = _messageModel.isSender ? NSTextAlignmentRight : NSTextAlignmentLeft;
  461. }
  462. }
  463. if (_messageModel.messageType == ChatMessageTypeEvent || _messageModel.messageType == ChatMessageTypeCallBack || _messageModel.messageType == ChatMessageTypeCallBack2) {
  464. _avatarImageView.hidden = YES;
  465. _nameLabel.hidden = YES;
  466. _readStateLbl.hidden = YES;
  467. _timeLabel.hidden = YES;
  468. }else{
  469. _avatarImageView.hidden = NO;
  470. _nameLabel.hidden = NO;
  471. _readStateLbl.hidden = NO;
  472. _timeLabel.hidden = NO;
  473. }
  474. if (_messageModel.type == 0&& _messageModel.isSender) {
  475. _readStateLbl.hidden = NO;
  476. }else{
  477. _readStateLbl.hidden = YES;
  478. }
  479. }
  480. - (void)setupBubbleBackground {
  481. // 图片和视频类型不需要气泡背景
  482. if (_messageModel.messageType == ChatMessageTypeImage || _messageModel.messageType == ChatMessageTypeVideo || _messageModel.messageType == ChatMessageTypeEvent|| _messageModel.messageType == ChatMessageTypeCallBack || _messageModel.messageType == ChatMessageTypeCallBack2) {
  483. _bubbleImageView.hidden = YES;
  484. return;
  485. }
  486. _bubbleImageView.hidden = NO;
  487. UIImage *bubbleImage = _messageModel.isSender ? [UIImage imageNamed:@"rightlt"] : [UIImage imageNamed:@"leftlt"];
  488. // 拉伸气泡图片
  489. UIEdgeInsets insets = UIEdgeInsetsMake(30, _messageModel.isSender ? 20 : 10, 10, _messageModel.isSender ? 10 : 20);
  490. bubbleImage = [bubbleImage resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch];
  491. _bubbleImageView.image = bubbleImage;
  492. }
  493. - (void)setupLongPressEvent{
  494. if (_isHistory) {
  495. return;
  496. }
  497. if ( _messageModel.messageType == ChatMessageTypeEvent || _messageModel.messageType == ChatMessageTypeCallBack || _messageModel.messageType == ChatMessageTypeCall || _messageModel.messageType == ChatMessageTypeCallBack2) {
  498. return;
  499. }
  500. // NSLog(@"setupLongPressEvent-------1");
  501. // 创建长按手势识别器并设置目标方法
  502. UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressEvent:)];
  503. longPress.minimumPressDuration = 0.3; // 设置长按的最小时间为0.3秒
  504. [self.currentContentView addGestureRecognizer:longPress]; // 将手势识别器添加到视图上
  505. if(_messageModel.messageType ==ChatMessageTypeText){
  506. [self.bubbleImageView addGestureRecognizer:longPress]; // 将手势识别器添加到视图上
  507. }
  508. else{
  509. [self.currentContentView addGestureRecognizer:longPress]; // 将手势识别器添加到视图上
  510. }
  511. }
  512. - (void)longPressEvent:(UILongPressGestureRecognizer *)gestureRecognizer {
  513. if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
  514. NSLog(@"长按事件开始");
  515. // 在这里添加你的代码,例如显示菜单、编辑选项等
  516. if (_messageModel.messageType == ChatMessageTypeDel || _messageModel.messageType == ChatMessageTypeCallBack) {
  517. return;
  518. }
  519. weakSelf(self);
  520. if(weakself.bubbleCellLongPressMenuBlock){
  521. weakself.bubbleCellLongPressMenuBlock(99);
  522. }
  523. BOOL isSender = NO;
  524. if (_messageModel.isSender) {
  525. isSender = YES;
  526. }
  527. ChatLongPressMenuView * contenView = [[ChatLongPressMenuView alloc] initWithMessageType:_messageModel.messageType isSender:isSender];
  528. BOOL isMedia = NO;
  529. if (_messageModel.messageType == ChatMessageTypeImage || _messageModel.messageType == ChatMessageTypeVideo) {
  530. isMedia = YES;
  531. }else{
  532. isMedia = NO;
  533. }
  534. ChatPopoverView * popover = [[ChatPopoverView alloc] initWithContentView:contenView sourceView:isMedia ? self.currentContentView : self.bubbleImageView];
  535. [popover show];
  536. contenView.indexOfMenuClicked = ^(NSInteger index) {
  537. [popover dismiss];
  538. !weakself.bubbleCellLongPressMenuBlock ?: weakself.bubbleCellLongPressMenuBlock(index);
  539. };
  540. } else if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
  541. NSLog(@"长按事件结束");
  542. }
  543. NSLog(@"longPressEvent-------");
  544. }
  545. - (void)batchStateButtonClicked{
  546. self.batchStateBtn.selected = !self.batchStateBtn.isSelected;
  547. !self.batchSelectedStateBlock ?: self.batchSelectedStateBlock(self.batchStateBtn.isSelected);
  548. }
  549. #pragma mark - 内容配置具体实现
  550. - (void)setupTextContent {
  551. _textLbl.hidden = NO;
  552. if (_messageModel.quoteMessage) {
  553. self.quoteLbl.hidden = NO;
  554. self.quoteLbl.text = [NSString stringWithFormat:@"%@:%@",_messageModel.quoteMessage.nickName,_messageModel.quoteMessage.content];
  555. }else{
  556. _quoteLbl.hidden = YES;
  557. }
  558. // NSLog(@"_messageModel.content:%@",_messageModel.content);
  559. // 创建富文本
  560. NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:_messageModel.content ?: @""];
  561. // 处理特殊内容(链接、@用户等)
  562. [self highlightSpecialElementsInText:text];
  563. // 同步设置文本,避免闪烁
  564. // [UIView performWithoutAnimation:^{
  565. // _textLbl.attributedText = text;
  566. // }];
  567. if(_matchesArray.count>0){
  568. NSMutableArray *array = [NSMutableArray new];
  569. for (NSTextCheckingResult *match in _matchesArray) {
  570. if (match.resultType == NSTextCheckingTypeLink) {
  571. NSString *url =[NSString stringWithFormat:@"%@",match.URL];
  572. url=[url stringByReplacingOccurrencesOfString:@"http://" withString:@""];
  573. url=[url stringByReplacingOccurrencesOfString:@"https://" withString:@""];
  574. [array addObject:url];
  575. }
  576. }
  577. _textLbl.attributedText = [self getAttributeWith:array string:_messageModel.content orginFont:18 orginColor:[UIColor darkGrayColor] attributeFont:18 attributeColor:[UIColor blueColor]];
  578. // _textLbl.attributedText = text;
  579. [_textLbl yb_addAttributeTapActionWithStrings:array tapClicked:^(UILabel *label, NSString *string, NSRange range, NSInteger index) {
  580. NSLog(@"%ld:%@",(long)index,self->_matchesArray[index]);
  581. NSTextCheckingResult *match=self->_matchesArray[index];
  582. // [[UIApplication sharedApplication] openURL:match.URL options:@{} completionHandler:nil];
  583. // 点击后的操作
  584. SFSafariViewController *svc = [[SFSafariViewController alloc] initWithURL:match.URL];
  585. [self.parentViewController presentViewController:svc animated:YES completion:nil];
  586. // [FilePreviewer.shared previewFileWithLocalPath:@"" remoteURL:match.URL fromViewController:self.parentViewController];
  587. }];
  588. }
  589. else{
  590. _textLbl.attributedText = text;
  591. }
  592. UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressEvent:)];
  593. longPress.minimumPressDuration = 0.3; // 设置长按的最小时间为0.3秒
  594. [_textLbl addGestureRecognizer:longPress]; // 将手势识别器添加到视图上
  595. _currentContentView = _textLbl;
  596. }
  597. - (NSAttributedString *)getAttributeWith:(id)sender
  598. string:(NSString *)string
  599. orginFont:(CGFloat)orginFont
  600. orginColor:(UIColor *)orginColor
  601. attributeFont:(CGFloat)attributeFont
  602. attributeColor:(UIColor *)attributeColor
  603. {
  604. __block NSMutableAttributedString *totalStr = [[NSMutableAttributedString alloc] initWithString:string];
  605. [totalStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:orginFont] range:NSMakeRange(0, string.length)];
  606. [totalStr addAttribute:NSForegroundColorAttributeName value:orginColor range:NSMakeRange(0, string.length)];
  607. NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
  608. [paragraphStyle setLineSpacing:0.0f]; //设置行间距
  609. [paragraphStyle setLineBreakMode:NSLineBreakByTruncatingTail];
  610. [paragraphStyle setAlignment:NSTextAlignmentLeft];
  611. [paragraphStyle setLineBreakMode:NSLineBreakByCharWrapping];
  612. [totalStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [totalStr length])];
  613. if ([sender isKindOfClass:[NSArray class]]) {
  614. __block NSString *oringinStr = string;
  615. __weak typeof(self) weakSelf = self;
  616. [sender enumerateObjectsUsingBlock:^(NSString * _Nonnull str, NSUInteger idx, BOOL * _Nonnull stop) {
  617. NSRange range = [oringinStr rangeOfString:str];
  618. if(range.length>0){
  619. [totalStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:attributeFont] range:range];
  620. [totalStr addAttribute:NSForegroundColorAttributeName value:attributeColor range:range];
  621. // oringinStr = [oringinStr stringByReplacingCharactersInRange:range withString:[weakSelf getStringWithRange:range]];
  622. }
  623. }];
  624. }else if ([sender isKindOfClass:[NSString class]]) {
  625. NSRange range = [string rangeOfString:sender];
  626. [totalStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:attributeFont] range:range];
  627. [totalStr addAttribute:NSForegroundColorAttributeName value:attributeColor range:range];
  628. }
  629. return totalStr;
  630. }
  631. - (NSString *)getStringWithRange:(NSRange)range
  632. {
  633. NSMutableString *string = [NSMutableString string];
  634. for (int i = 0; i < range.length ; i++) {
  635. [string appendString:@" "];
  636. }
  637. return string;
  638. }
  639. #pragma mark - 辅助方法
  640. - (void)highlightSpecialElementsInText:(NSMutableAttributedString *)text {
  641. // 高亮链接
  642. NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
  643. NSArray *matches = [detector matchesInString:text.string options:0 range:NSMakeRange(0, text.length)];
  644. NSLog(@"matches:%@",matches);
  645. _matchesArray =matches;
  646. // 高亮@用户(暂不开放)
  647. // NSRegularExpression *atRegex = [NSRegularExpression regularExpressionWithPattern:@"@[\\u4e00-\\u9fa5a-zA-Z0-9_-]+" options:0 error:nil];
  648. // NSArray *atMatches = [atRegex matchesInString:text.string options:0 range:text.yy_rangeOfAll];
  649. //
  650. // for (NSTextCheckingResult *match in atMatches) {
  651. // [text yy_setColor:[UIColor orangeColor] range:match.range];
  652. // [text yy_setTextHighlightRange:match.range
  653. // color:[UIColor orangeColor]
  654. // backgroundColor:[UIColor colorWithWhite:0.9 alpha:0.5]
  655. // tapAction:^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
  656. // NSString *atUser = [text.string substringWithRange:range];
  657. // // 处理@用户点击
  658. // NSLog(@"点击了: %@", atUser);
  659. // }];
  660. // }
  661. }
  662. - (void)setupImageContent {
  663. _imageContentView.hidden = NO;
  664. if (_messageModel.localurl && _messageModel.localurl.length != 0) {//显示本地图片
  665. NSURL *localFileURL = getURL(_messageModel.localurl);
  666. // NSLog(@"UIImage------------1");
  667. // 从 URL 获取文件路径
  668. NSString *filePath = localFileURL.path;
  669. // 通过文件路径加载图片
  670. UIImage *image = [UIImage imageWithContentsOfFile:filePath];
  671. if (image) {//本地图片显示成功
  672. // NSLog(@"UIImage------------2");
  673. _imageContentView.image = image;
  674. CGImageRef cgImage = image.CGImage;
  675. CGFloat imageWidth = CGImageGetWidth(cgImage);
  676. CGFloat imageHeight = CGImageGetHeight(cgImage);
  677. CGFloat imageScale = imageWidth/imageHeight;
  678. CGFloat realWidth = 150 * imageScale;
  679. if(realWidth>220){
  680. realWidth=220;
  681. imageHeight=150;
  682. }
  683. else{
  684. imageHeight=150;
  685. }
  686. self.messageModel.mediaSize = CGSizeMake(realWidth, imageHeight);
  687. [self.imageContentView mas_updateConstraints:^(MASConstraintMaker *make) {
  688. make.width.mas_equalTo(realWidth);
  689. }];
  690. _currentContentView = _imageContentView;
  691. return;
  692. }
  693. }
  694. //没有有效本地图片 使用SDWebImage加载图片
  695. weakSelf(self);
  696. [_imageContentView sd_setImageWithURL:[NSURL URLWithString:_messageModel.url]
  697. placeholderImage:[UIImage imageNamed:@"pictrue_placeholder"]
  698. completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
  699. if (!error && image) {
  700. // 图片加载成功后可以更新布局(如果需要)
  701. // NSLog(@"UIImage------------5");
  702. CGImageRef cgImage = image.CGImage;
  703. CGFloat imageWidth = CGImageGetWidth(cgImage);
  704. CGFloat imageHeight = CGImageGetHeight(cgImage);
  705. CGFloat imageScale = imageWidth/imageHeight;
  706. CGFloat realWidth = 150 * imageScale;
  707. if(realWidth>220){
  708. realWidth=220;
  709. imageHeight=150;
  710. }
  711. else{
  712. imageHeight=150;
  713. }
  714. weakself.messageModel.mediaSize = CGSizeMake(realWidth, 150);
  715. [weakself.imageContentView mas_updateConstraints:^(MASConstraintMaker *make) {
  716. make.width.mas_equalTo(realWidth);
  717. }];
  718. }
  719. }];
  720. _currentContentView = _imageContentView;
  721. }
  722. - (void)setupVideoContent {
  723. _videoContentView.hidden = NO;
  724. // if (_thumbnailView.image) {
  725. // return;
  726. // }
  727. // 清除旧视图
  728. _thumbnailView.image = nil;
  729. _loadingImg.image = nil;
  730. weakSelf(self);
  731. if (_messageModel.videoThumbnailImage) {
  732. // NSLog(@"videoThumbnailImage----:%@",_messageModel.videoThumbnailImage);
  733. _thumbnailView.image = _messageModel.videoThumbnailImage;
  734. [_thumbnailView mas_makeConstraints:^(MASConstraintMaker *make) {
  735. make.edges.equalTo(_videoContentView);
  736. }];
  737. }else{
  738. // NSLog(@"videoThumbnailImage----load");
  739. [_messageModel generateThumbnailWithCompletion:^(UIImage * _Nonnull thumbnail) {
  740. // NSLog(@"111111---------------");
  741. if (thumbnail) {
  742. // NSLog(@"2222---------------");
  743. dispatch_async(dispatch_get_main_queue(), ^{
  744. [weakself.thumbnailView mas_makeConstraints:^(MASConstraintMaker *make) {
  745. make.edges.equalTo(self->_videoContentView);
  746. }];
  747. weakself.thumbnailView.image = thumbnail;
  748. });
  749. }
  750. }];
  751. }
  752. _messageModel.uploadPersentChange = ^(NSInteger index,NSInteger localtime) {
  753. if(localtime==weakself.messageModel.localtime){
  754. if(index>=100){
  755. dispatch_async(dispatch_get_main_queue(), ^{
  756. if(weakself.jinduLbl){
  757. weakself.jinduLbl.alpha = 0;
  758. weakself.loadingImg.alpha = 0;
  759. }
  760. });
  761. }
  762. else{
  763. dispatch_async(dispatch_get_main_queue(), ^{
  764. if(weakself.jinduLbl){
  765. if(weakself.loadingImg.alpha!=1||!weakself.loadingImg.image){
  766. NSString *gifPath = [[NSBundle mainBundle] pathForResource:@"load" ofType:@"gif"];
  767. NSData *gifData = [NSData dataWithContentsOfFile:gifPath];
  768. UIImage *placeholder = [UIImage sd_imageWithGIFData:gifData];
  769. weakself.loadingImg.image = placeholder;
  770. weakself.loadingImg.alpha = 1;
  771. }
  772. weakself.jinduLbl.alpha = 1;
  773. weakself.jinduLbl.text = [NSString stringWithFormat:@"%ld",(long)index];
  774. }
  775. });
  776. }
  777. }
  778. else{
  779. dispatch_async(dispatch_get_main_queue(), ^{
  780. if(weakself.jinduLbl){
  781. weakself.jinduLbl.alpha = 0;
  782. weakself.loadingImg.alpha = 0;
  783. }
  784. });
  785. }
  786. };
  787. _currentContentView = _videoContentView;
  788. }
  789. - (void)setupFileContent {
  790. _fileContentView.hidden = NO;
  791. // 获取子视图
  792. UIImageView *iconView = _fileContentView.subviews[0];
  793. UILabel *nameLabel = _fileContentView.subviews[1];
  794. UILabel *sizeLabel = _fileContentView.subviews[2];
  795. // 设置文件内容
  796. iconView.image = [UIImage imageNamed: @"file_icon_black"];
  797. nameLabel.text = _messageModel.fileName ?: @"未知文件";
  798. sizeLabel.text = _messageModel.customFileSize ?: @"0KB";
  799. _currentContentView = _fileContentView;
  800. }
  801. - (void)setupVoiceContent {
  802. _voiceContentView.hidden = NO;
  803. // 获取子视图
  804. UIImageView *voiceIcon = _voiceContentView.subviews[0];
  805. UILabel *durationLabel = _voiceContentView.subviews[1];
  806. voiceIcon.animationDuration = 1.f;
  807. voiceIcon.animationRepeatCount = 0;
  808. // 设置语音内容
  809. voiceIcon.image = [UIImage imageNamed:_messageModel.isSender ? @"yuyinR" : @"yuyinL"];
  810. durationLabel.text = [NSString stringWithFormat:@"%ld\"", (long)_messageModel.voiceDuration];
  811. // 根据发送方调整布局
  812. if (_messageModel.isSender) {
  813. [voiceIcon mas_remakeConstraints:^(MASConstraintMaker *make) {
  814. make.right.equalTo(_voiceContentView).offset(-8);
  815. make.centerY.equalTo(_voiceContentView);
  816. make.width.height.mas_equalTo(20);
  817. }];
  818. [durationLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  819. make.right.equalTo(voiceIcon.mas_left).offset(-8);
  820. make.centerY.equalTo(_voiceContentView);
  821. }];
  822. voiceIcon.animationImages = @[kImageMake(@"audio_sender_animate1"),kImageMake(@"audio_sender_animate2"),kImageMake(@"audio_sender_animate3")];
  823. } else {
  824. [voiceIcon mas_remakeConstraints:^(MASConstraintMaker *make) {
  825. make.left.equalTo(_voiceContentView).offset(8);
  826. make.centerY.equalTo(_voiceContentView);
  827. make.width.height.mas_equalTo(20);
  828. }];
  829. [durationLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  830. make.left.equalTo(voiceIcon.mas_right).offset(8);
  831. make.centerY.equalTo(_voiceContentView);
  832. }];
  833. voiceIcon.animationImages = @[kImageMake(@"audio_receiver_animate1"),kImageMake(@"audio_receiver_animate2"),kImageMake(@"audio_receiver_animate3")];
  834. }
  835. _currentContentView = _voiceContentView;
  836. }
  837. - (void)setupCallContent {
  838. _callContentView.hidden = NO;
  839. // 获取子视图
  840. UIImageView *callIcon = _callContentView.subviews[0];
  841. UILabel *callLabel = _callContentView.subviews[1];
  842. UILabel *durationLabel = _callContentView.subviews[2];
  843. // 设置通话记录内容
  844. callIcon.image = [UIImage imageNamed:_messageModel.isVideo ? @"vedio_call_icon" : @"audio_call_icon"];
  845. callLabel.text = _messageModel.isCallSuccess ? @"通话时长" : @"未接通";
  846. durationLabel.text = _messageModel.isCallSuccess ? [self formatTimeWithMilliseconds:_messageModel.callDuration] : @"";
  847. _currentContentView = _callContentView;
  848. }
  849. - (void)setupHistoryContent {
  850. _historyContentView.hidden = NO;
  851. // 获取子视图
  852. UILabel *titleLabel = _historyContentView.subviews[0];
  853. UILabel *contentLabel = (UILabel *)_historyContentView.subviews[1];
  854. // 设置聊天记录内容
  855. titleLabel.text = @"聊天记录";
  856. NSArray * msgArray = _messageModel.forwardMsgArray;
  857. NSMutableArray * contentArray = [NSMutableArray array];
  858. for (NSDictionary * dict in msgArray) {
  859. NSString * messageType = dict[@"messageType"];
  860. NSString * nickName = dict[@"fromName"]?:@"";
  861. NSString * recordString = @"";
  862. switch ([messageType integerValue]) {
  863. case 0:
  864. // recordString = [NSString stringWithFormat:@"%@:%@\n",nickName,dict[@"content"]?:@""];
  865. recordString = [NSString stringWithFormat:@"%@:%@\n",nickName,[CryptoAES.shareInstance decryptDataL:dict[@"content"]]];
  866. break;
  867. case 1:
  868. recordString = [NSString stringWithFormat:@"%@:【图片】\n",nickName];;
  869. break;
  870. case 2:
  871. recordString = [NSString stringWithFormat:@"%@:【文件】\n",nickName];
  872. break;
  873. case 3:
  874. recordString = [NSString stringWithFormat:@"%@:【语音】\n",nickName];
  875. break;
  876. case 5:
  877. recordString = [NSString stringWithFormat:@"%@:【视频】\n",nickName];
  878. break;
  879. case 6:
  880. recordString = [NSString stringWithFormat:@"%@:【转发】\n",nickName];
  881. break;
  882. case 7:
  883. recordString = [NSString stringWithFormat:@"%@:【通话】\n",nickName];
  884. break;
  885. default:
  886. break;
  887. }
  888. [contentArray addObject:recordString];
  889. if (msgArray.lastObject == dict) {
  890. NSString * contentString = [contentArray componentsJoinedByString:@""];
  891. NSMutableAttributedString *content = [[NSMutableAttributedString alloc] initWithString:contentString ?: @""];
  892. contentLabel.attributedText = content;
  893. }
  894. }
  895. _currentContentView = _historyContentView;
  896. }
  897. - (void)setupEventContent {
  898. _eventLbl.hidden = NO;
  899. _eventLbl.text = _messageModel.content;
  900. [_eventLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
  901. make.height.mas_equalTo(24);
  902. make.centerX.mas_equalTo(self.contentView.mas_centerX);
  903. make.centerY.mas_equalTo(self.contentView.mas_centerY);
  904. }];
  905. [_eventLbl sizeToFit];
  906. _currentContentView = _eventLbl;
  907. }
  908. - (void)setupCallbackContent {
  909. _callbackLbl.hidden = NO;
  910. _callbackLbl.text = _messageModel.isSender ? @"你撤回了一条消息":[NSString stringWithFormat:@"%@撤回了一条消息",_messageModel.nickName];
  911. [_callbackLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
  912. make.height.mas_equalTo(24);
  913. make.centerX.mas_equalTo(self.contentView.mas_centerX);
  914. make.centerY.mas_equalTo(self.contentView.mas_centerY);
  915. }];
  916. _currentContentView = _callbackLbl;
  917. }
  918. - (void)setupDeleateContent {
  919. _deleateLbl.hidden = NO;
  920. _deleateLbl.text = @"你删除了一条消息!";
  921. [_deleateLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
  922. make.height.mas_equalTo(24);
  923. make.centerX.mas_equalTo(self.contentView.mas_centerX);
  924. make.centerY.mas_equalTo(self.contentView.mas_centerY);
  925. }];
  926. _currentContentView = _deleateLbl;
  927. }
  928. #pragma mark - 布局方法
  929. - (void)layoutContentViews {
  930. if (_messageModel.messageType == ChatMessageTypeEvent || _messageModel.messageType == ChatMessageTypeCallBack || _messageModel.messageType == ChatMessageTypeCallBack2) {
  931. return;
  932. }
  933. //复选框布局
  934. // self.batchStateBtn.frame=CGRectMake(20, 10, 32, 32);
  935. [self.batchStateBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
  936. make.size.mas_equalTo(CGSizeMake(32, 32));
  937. make.left.mas_equalTo(20);
  938. make.top.mas_equalTo(10);
  939. }];
  940. // 头像布局
  941. if (_messageModel.isSender) {
  942. [_avatarImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  943. make.right.equalTo(self.contentView).offset(-kAvatarMargin);
  944. make.top.equalTo(self.contentView).offset(kVerticalMargin);
  945. make.width.height.mas_equalTo(kAvatarSize);
  946. }];
  947. [_nameLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  948. make.right.equalTo(_avatarImageView.mas_left).offset(-4);
  949. make.top.equalTo(_avatarImageView);
  950. make.left.greaterThanOrEqualTo(self.contentView).offset(10);
  951. }];
  952. [_timeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  953. make.height.mas_equalTo(14);
  954. make.right.mas_equalTo(self.nameLabel.mas_left).offset(-12);
  955. make.top.mas_equalTo(self.avatarImageView);
  956. }];
  957. if (_messageModel.messageType == ChatMessageTypeImage || _messageModel.messageType == ChatMessageTypeVideo) {
  958. [_readStateLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
  959. make.right.mas_equalTo(self.contentView).offset(-30);
  960. make.height.mas_equalTo(14);
  961. make.top.mas_equalTo(self.currentContentView.mas_bottom).offset(2);
  962. }];
  963. }else{
  964. if (!_quoteLbl.hidden) {
  965. [_readStateLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
  966. make.right.mas_equalTo(self.contentView).offset(-30);
  967. make.height.mas_equalTo(14);
  968. make.top.mas_equalTo(self.quoteLbl.mas_bottom);
  969. }];
  970. }else{
  971. [_readStateLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
  972. make.right.mas_equalTo(self.contentView).offset(-30);
  973. make.height.mas_equalTo(14);
  974. make.top.mas_equalTo(self.bubbleImageView.mas_bottom);
  975. }];
  976. }
  977. }
  978. } else {
  979. [_avatarImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  980. make.left.equalTo(self.contentView).offset(kAvatarMargin);
  981. make.top.equalTo(self.contentView).offset(kVerticalMargin);
  982. make.width.height.mas_equalTo(kAvatarSize);
  983. }];
  984. [_nameLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  985. make.left.equalTo(_avatarImageView.mas_right).offset(4);
  986. make.top.equalTo(_avatarImageView);
  987. make.right.lessThanOrEqualTo(self.contentView).offset(-10);
  988. }];
  989. [_timeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  990. make.height.mas_equalTo(14);
  991. make.left.mas_equalTo(self.nameLabel.mas_right).offset(12);
  992. make.top.mas_equalTo(self.avatarImageView);
  993. }];
  994. }
  995. // 内容视图布局
  996. [self layoutCurrentContentView];
  997. // 气泡背景布局
  998. if (!_bubbleImageView.hidden) {
  999. NSAssert(_currentContentView, @"currentContentView 不能为 nil");
  1000. NSAssert(_currentContentView.superview, @"currentContentView 必须已添加到父视图");
  1001. [_bubbleImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  1002. make.edges.equalTo(_currentContentView).insets(UIEdgeInsetsMake(-kTextVerticalMargin, -kTextHorizontalMargin, -kTextVerticalMargin, -kTextHorizontalMargin));
  1003. }];
  1004. [self.contentView sendSubviewToBack:_bubbleImageView];
  1005. }
  1006. }
  1007. - (void)layoutCurrentContentView {
  1008. switch (_messageModel.messageType) {
  1009. case ChatMessageTypeText:
  1010. [self layoutTextContent];
  1011. break;
  1012. case ChatMessageTypeImage:
  1013. [self layoutImageContent];
  1014. break;
  1015. case ChatMessageTypeVideo:
  1016. [self layoutVideoContent];
  1017. break;
  1018. case ChatMessageTypeVoice:
  1019. [self layoutVioceContent];
  1020. break;
  1021. case ChatMessageTypeCall:
  1022. [self layoutCallContent];
  1023. break;
  1024. case ChatMessageTypeHistory:
  1025. [self layoutHistoryContent];
  1026. break;
  1027. default:
  1028. [self layoutOtherContent];
  1029. break;
  1030. }
  1031. }
  1032. - (void)layoutTextContent {
  1033. CGSize textSize = [self calculateTextSize];
  1034. if (_messageModel.isSender) {
  1035. [_textLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
  1036. make.right.equalTo(_avatarImageView.mas_left).offset(-26);
  1037. make.top.mas_equalTo(_avatarImageView.mas_top).offset(28);
  1038. make.width.mas_equalTo(textSize.width);
  1039. make.height.mas_equalTo(textSize.height);
  1040. make.bottom.equalTo(self.contentView).offset(-kVerticalMargin).priorityHigh();
  1041. }];
  1042. if (_messageModel.quoteMessage) {
  1043. [_quoteLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
  1044. make.height.mas_equalTo(20);
  1045. make.top.mas_equalTo(_textLbl.mas_bottom).offset(20);
  1046. make.right.mas_equalTo(_textLbl.mas_right).offset(6);
  1047. make.width.mas_lessThanOrEqualTo(kContentMaxWidth);
  1048. }];
  1049. }
  1050. } else {
  1051. [_textLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
  1052. make.left.equalTo(_avatarImageView.mas_right).offset(26);
  1053. make.top.mas_equalTo(_avatarImageView.mas_top).offset(28);
  1054. make.width.mas_equalTo(textSize.width);
  1055. make.height.mas_equalTo(textSize.height);
  1056. make.bottom.equalTo(self.contentView).offset(-kVerticalMargin).priorityHigh();
  1057. }];
  1058. if (_messageModel.quoteMessage) {
  1059. [_quoteLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
  1060. make.height.mas_equalTo(20);
  1061. make.top.mas_equalTo(_textLbl.mas_bottom).offset(20);
  1062. make.left.mas_equalTo(_textLbl.mas_left).offset(-6);
  1063. make.width.mas_lessThanOrEqualTo(kContentMaxWidth);
  1064. }];
  1065. }
  1066. }
  1067. }
  1068. - (void)layoutImageContent {
  1069. // CGSize imageSize = [self adjustMediaSize:_messageModel.mediaSize];
  1070. CGSize imageSize = _messageModel.mediaSize;
  1071. if (_messageModel.isSender) {
  1072. [_imageContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
  1073. make.right.equalTo(_avatarImageView.mas_left).offset(-16);
  1074. make.top.mas_equalTo(_avatarImageView.mas_top).offset(28);
  1075. make.width.mas_equalTo(imageSize.width);
  1076. make.height.mas_equalTo(imageSize.height);
  1077. make.bottom.equalTo(self.contentView).offset(-kVerticalMargin).priorityHigh();
  1078. }];
  1079. } else {
  1080. [_imageContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
  1081. make.left.equalTo(_avatarImageView.mas_right).offset(16);
  1082. make.top.mas_equalTo(_avatarImageView.mas_top).offset(28);
  1083. make.width.mas_equalTo(imageSize.width);
  1084. make.height.mas_equalTo(imageSize.height);
  1085. make.bottom.equalTo(self.contentView).offset(-kVerticalMargin).priorityHigh();
  1086. }];
  1087. }
  1088. }
  1089. - (void)layoutVideoContent {
  1090. CGSize videoSize = [self adjustMediaSize:_messageModel.mediaSize];
  1091. if (_messageModel.isSender) {
  1092. [_videoContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
  1093. make.right.equalTo(_avatarImageView.mas_left).offset(-16);
  1094. make.top.mas_equalTo(_avatarImageView.mas_top).offset(28);
  1095. make.width.mas_equalTo(videoSize.width);
  1096. make.height.mas_equalTo(videoSize.height);
  1097. make.bottom.equalTo(self.contentView).offset(-kVerticalMargin).priorityHigh();
  1098. }];
  1099. } else {
  1100. [_videoContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
  1101. make.left.equalTo(_avatarImageView.mas_right).offset(16);
  1102. make.top.mas_equalTo(_avatarImageView.mas_top).offset(28);
  1103. make.width.mas_equalTo(videoSize.width);
  1104. make.height.mas_equalTo(videoSize.height);
  1105. make.bottom.equalTo(self.contentView).offset(-kVerticalMargin).priorityHigh();
  1106. }];
  1107. }
  1108. }
  1109. - (void)layoutVioceContent {
  1110. // 设置内容视图约束
  1111. [_voiceContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
  1112. if (_messageModel.isSender) {
  1113. make.right.equalTo(_avatarImageView.mas_left).offset(-26);
  1114. } else {
  1115. make.left.equalTo(_avatarImageView.mas_right).offset(26);
  1116. }
  1117. make.top.equalTo(_avatarImageView).offset(28);
  1118. make.width.mas_equalTo(_messageModel.voiceWidth);
  1119. make.bottom.equalTo(self.contentView).offset(-kVerticalMargin).priorityHigh();
  1120. }];
  1121. }
  1122. - (void)layoutCallContent {
  1123. // 获取子视图
  1124. UIImageView *callIcon = _callContentView.subviews[0];
  1125. UILabel *callLabel = _callContentView.subviews[1];
  1126. UILabel *durationLabel = _callContentView.subviews[2];
  1127. if (_messageModel.isSender) {
  1128. [_callContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
  1129. make.right.equalTo(_avatarImageView.mas_left).offset(-26);
  1130. make.top.mas_equalTo(_avatarImageView.mas_top).offset(28);
  1131. make.width.mas_lessThanOrEqualTo(kContentMaxWidth);
  1132. make.bottom.equalTo(self.contentView).offset(-kVerticalMargin).priorityHigh();
  1133. }];
  1134. } else {
  1135. [_callContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
  1136. make.left.equalTo(_avatarImageView.mas_right).offset(26);
  1137. make.top.mas_equalTo(_avatarImageView.mas_top).offset(28);
  1138. make.width.mas_lessThanOrEqualTo(kContentMaxWidth);
  1139. make.bottom.equalTo(self.contentView).offset(-kVerticalMargin).priorityHigh();
  1140. }];
  1141. }
  1142. if (_messageModel.isCallSuccess) {
  1143. [callLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  1144. make.left.equalTo(callIcon.mas_right).offset(8);
  1145. make.top.equalTo(_callContentView).offset(8);
  1146. }];
  1147. [durationLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  1148. make.left.equalTo(callLabel.mas_right).offset(8);
  1149. make.top.equalTo(callLabel);
  1150. make.right.equalTo(_callContentView).offset(-8);
  1151. make.bottom.equalTo(_callContentView).offset(-8);
  1152. }];
  1153. }else{
  1154. [callLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  1155. make.left.equalTo(callIcon.mas_right).offset(8);
  1156. make.top.equalTo(_callContentView).offset(8);
  1157. make.width.mas_lessThanOrEqualTo(50);
  1158. make.right.equalTo(_callContentView).offset(-8);
  1159. }];
  1160. }
  1161. }
  1162. - (void)layoutHistoryContent {
  1163. [_historyContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
  1164. if (_messageModel.isSender) {
  1165. make.right.equalTo(_avatarImageView.mas_left).offset(-26);
  1166. } else {
  1167. make.left.equalTo(_avatarImageView.mas_right).offset(26);
  1168. }
  1169. make.top.equalTo(_avatarImageView).offset(28);
  1170. make.width.mas_equalTo(160);
  1171. make.bottom.equalTo(self.contentView).offset(-kVerticalMargin).priorityHigh();
  1172. }];
  1173. }
  1174. - (void)layoutOtherContent {
  1175. if (_messageModel.isSender) {
  1176. [_currentContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
  1177. make.right.equalTo(_avatarImageView.mas_left).offset(-26);
  1178. make.top.mas_equalTo(_avatarImageView.mas_top).offset(28);
  1179. make.width.mas_lessThanOrEqualTo(kContentMaxWidth);
  1180. make.bottom.equalTo(self.contentView).offset(-kVerticalMargin).priorityHigh();
  1181. }];
  1182. } else {
  1183. [_currentContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
  1184. make.left.equalTo(_avatarImageView.mas_right).offset(26);
  1185. make.top.mas_equalTo(_avatarImageView.mas_top).offset(28);
  1186. make.width.mas_lessThanOrEqualTo(kContentMaxWidth);
  1187. make.bottom.equalTo(self.contentView).offset(-kVerticalMargin).priorityHigh();
  1188. }];
  1189. }
  1190. }
  1191. - (CGSize)adjustMediaSize:(CGSize)originalSize {
  1192. CGFloat maxWidth = kContentMaxWidth;
  1193. CGFloat maxHeight = 200.0f;
  1194. if (originalSize.width <= 0 || originalSize.height <= 0) {
  1195. return CGSizeMake(maxWidth, maxWidth * 0.6);
  1196. }
  1197. CGFloat ratio = originalSize.width / originalSize.height;
  1198. CGFloat adjustedWidth = maxWidth;
  1199. CGFloat adjustedHeight = adjustedWidth / ratio;
  1200. if (adjustedHeight > maxHeight) {
  1201. adjustedHeight = maxHeight;
  1202. adjustedWidth = adjustedHeight * ratio;
  1203. }
  1204. return CGSizeMake(adjustedWidth, adjustedHeight);
  1205. }
  1206. + (CGSize)otherAdjustMediaSize:(CGSize)originalSize {
  1207. CGFloat maxWidth = kContentMaxWidth;
  1208. CGFloat maxHeight = 200.0f;
  1209. if (originalSize.width <= 0 || originalSize.height <= 0) {
  1210. return CGSizeMake(maxWidth, maxWidth * 0.6);
  1211. }
  1212. CGFloat ratio = originalSize.width / originalSize.height;
  1213. CGFloat adjustedWidth = maxWidth;
  1214. CGFloat adjustedHeight = adjustedWidth / ratio;
  1215. if (adjustedHeight > maxHeight) {
  1216. adjustedHeight = maxHeight;
  1217. adjustedWidth = adjustedHeight * ratio;
  1218. }
  1219. return CGSizeMake(adjustedWidth, adjustedHeight);
  1220. }
  1221. - (CGSize)calculateTextSize{
  1222. NSString *text = _messageModel.content ?: @"";
  1223. UILabel *label = [[UILabel alloc] init];
  1224. label.font = [UIFont systemFontOfSize:18.0]; // 设置字体大小
  1225. label.numberOfLines = 0; // 设置为0表示不限制行数
  1226. label.text = text; // 设置文本内容
  1227. label.preferredMaxLayoutWidth = kContentMaxWidth; // 设置固定宽度
  1228. CGSize size = [label sizeThatFits:CGSizeMake(kContentMaxWidth, CGFLOAT_MAX)];
  1229. CGSize textSize = size;
  1230. // NSLog(@"文本宽度: %f, 文本高度: %f", textSize.width, textSize.height);
  1231. return textSize;
  1232. }
  1233. - (NSString *)formatTimeWithMilliseconds:(NSInteger)milliseconds{
  1234. NSInteger seconds = milliseconds / 1000.0;
  1235. if (seconds >= 60) {
  1236. // 大于等于60秒时显示为分钟,保留一位小数
  1237. NSInteger minutes = seconds / 60.0;
  1238. NSInteger sc = seconds%60;
  1239. return [NSString stringWithFormat:@"%ld分%ld秒", (long)minutes,(long)sc];
  1240. } else {
  1241. // 小于60秒时显示为秒,保留整数
  1242. return [NSString stringWithFormat:@"%ld秒", seconds];
  1243. }
  1244. }
  1245. #pragma mark 列表事件处理
  1246. - (void)handleImageTap {
  1247. if (_messageModel.messageType == ChatMessageTypeImage) {
  1248. // 处理图片点击
  1249. NSLog(@"点击了图片: %@", _messageModel.url);
  1250. if(_messageModel.fileError==1){
  1251. weakSelf(self);
  1252. if(weakself.bubbleCellLongPressMenuBlock){
  1253. weakself.bubbleCellLongPressMenuBlock(8);
  1254. }
  1255. return;
  1256. }
  1257. if (_messageModel.url.length == 0 && !_messageModel.isSender) {
  1258. [MBProgressHUD showWithText:@"图片正在上传"];
  1259. return;
  1260. }
  1261. [FilePreviewer.shared previewFileWithLocalPath:_messageModel.localurl remoteURL:getURL(_messageModel.url) fromViewController:self.parentViewController];
  1262. }
  1263. }
  1264. - (void)handleVideoTap {
  1265. if (_messageModel.messageType == ChatMessageTypeVideo) {
  1266. // 处理视频点击
  1267. NSLog(@"点击了视频: %@", _messageModel.url);
  1268. if(_messageModel.fileError==1){
  1269. weakSelf(self);
  1270. if(weakself.bubbleCellLongPressMenuBlock){
  1271. weakself.bubbleCellLongPressMenuBlock(8);
  1272. }
  1273. return;
  1274. }
  1275. if (_messageModel.url.length == 0 && !_messageModel.isSender) {
  1276. [MBProgressHUD showWithText:@"视频正在上传"];
  1277. return;
  1278. }
  1279. [FilePreviewer.shared previewFileWithLocalPath:self.messageModel.localurl
  1280. remoteURL:getURL(self.messageModel.url)
  1281. fromViewController:self.parentViewController];
  1282. [_messageModel downloadFileIfNeed:^(NSInteger persent) {
  1283. }];
  1284. }
  1285. }
  1286. - (void)handleFileTap {
  1287. if (_messageModel.messageType == ChatMessageTypeFile) {
  1288. // 处理视频点击
  1289. NSLog(@"点击了文件: %@", _messageModel.url);
  1290. if(_messageModel.fileError==1){
  1291. weakSelf(self);
  1292. if(weakself.bubbleCellLongPressMenuBlock){
  1293. weakself.bubbleCellLongPressMenuBlock(8);
  1294. }
  1295. return;
  1296. }
  1297. if (_messageModel.url.length == 0 && !_messageModel.isSender) {
  1298. [MBProgressHUD showWithText:@"文件正在上传"];
  1299. return;
  1300. }
  1301. UILabel *sizeLabel = _fileContentView.subviews[2];
  1302. weakSelf(self)
  1303. [_messageModel downloadFileIfNeed:^(NSInteger persent) {
  1304. dispatch_async(dispatch_get_main_queue(), ^{
  1305. sizeLabel.text = [NSString stringWithFormat:@"%ld",(long)persent];
  1306. if(persent>=100){
  1307. sizeLabel.text = weakself.messageModel.customFileSize;
  1308. [FilePreviewer.shared previewFileWithLocalPath:weakself.messageModel.localurl
  1309. remoteURL:getURL(weakself.messageModel.url)
  1310. fromViewController:weakself.parentViewController];
  1311. }
  1312. });
  1313. }];
  1314. }
  1315. }
  1316. - (void)handleVoiceTap {
  1317. if (_messageModel.messageType == ChatMessageTypeVoice) {
  1318. // 处理语音点击
  1319. NSLog(@"点击了语音: %@", _messageModel.url);
  1320. [_messageModel downloadFileIfNeed:^(NSInteger persent) {
  1321. dispatch_main_async_safe(^{
  1322. if (persent >= 100) {
  1323. [self playVoice];
  1324. }
  1325. })
  1326. }];
  1327. }
  1328. }
  1329. - (void)playVoice {
  1330. UIImageView *voiceIcon = _voiceContentView.subviews[0];
  1331. if (self.player) {
  1332. AVPlayerItem *playerItem = self.player.currentItem;
  1333. CMTime currentTime = playerItem.currentTime;
  1334. CMTime duration = playerItem.duration;
  1335. [voiceIcon stopAnimating];
  1336. if (self.player.rate == 0.0) {
  1337. if (CMTimeCompare(currentTime, duration) == 0 ||
  1338. CMTimeCompare(currentTime, duration) == 1) {
  1339. // 播放完毕
  1340. NSLog(@"Playback finished");
  1341. } else {
  1342. // 暂停
  1343. NSLog(@"Playback paused");
  1344. [self.player play];
  1345. return;
  1346. }
  1347. } else {
  1348. // 正在播放
  1349. NSLog(@"Playback in progress");
  1350. [self.player pause];
  1351. return;
  1352. }
  1353. }
  1354. NSString *urlstr = _messageModel.url;
  1355. NSURL *Url = [NSURL URLWithString:urlstr];
  1356. NSLog(@"本地路径:%@",_messageModel.localurl);
  1357. if (_messageModel.localurl && [[NSFileManager defaultManager] fileExistsAtPath:_messageModel.localurl]) {
  1358. NSLog(@"本地路径:");
  1359. urlstr=_messageModel.localurl;
  1360. Url = [NSURL fileURLWithPath:urlstr];
  1361. }
  1362. else{
  1363. }
  1364. NSLog(@"播放: %@", Url);
  1365. AVAudioSession *audioSession = [AVAudioSession sharedInstance];
  1366. [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
  1367. [audioSession setActive:YES error:nil];
  1368. AVPlayerItem *pitem = [AVPlayerItem playerItemWithURL:Url];
  1369. //AVPlayerItem *pitem = [[AVPlayerItem alloc] initWithURL:Url];
  1370. _player = [AVPlayer playerWithPlayerItem:pitem];
  1371. [[UIDevice currentDevice] setProximityMonitoringEnabled:YES]; //建议在播放之前设置yes,播放结束设置NO。这个功能是开启红外感应
  1372. [_player play];
  1373. [[NSNotificationCenter defaultCenter] addObserver:self
  1374. selector:@selector(playerItemDidReachEnd:)
  1375. name:AVPlayerItemDidPlayToEndTimeNotification
  1376. object:pitem];
  1377. [voiceIcon startAnimating];
  1378. }
  1379. - (void)playerItemDidReachEnd:(NSNotification *)notification {
  1380. // 播放结束时的处理代码
  1381. UIImageView *voiceIcon = _voiceContentView.subviews[0];
  1382. [voiceIcon stopAnimating];
  1383. NSLog(@"播放结束:%@",notification);
  1384. [[UIDevice currentDevice] setProximityMonitoringEnabled:NO]; //建议在播放之前设置yes,播放结束设置NO。这个功能是开启红外感应
  1385. [[NSNotificationCenter defaultCenter] removeObserver:self name:notification.name object:notification.object];
  1386. //可以执行继续播放下一条操作
  1387. }
  1388. - (void)handleHistoryTap {
  1389. ChatRecordController * recordVc = [[ChatRecordController alloc] init];
  1390. for (NSDictionary * dict in _messageModel.forwardMsgArray) {
  1391. [recordVc.dataArray addObject:[ChatMessageModel modelWithDictionary:dict]];
  1392. }
  1393. UINavigationController * navi = [[UINavigationController alloc] initWithRootViewController:recordVc];
  1394. navi.modalPresentationStyle = UIModalPresentationFullScreen;
  1395. [self.parentViewController presentViewController:navi animated:YES completion:nil];
  1396. }
  1397. - (void)handleQuoteTap{
  1398. NSLog(@"handleQuoteTap----:%@",[_messageModel.quoteMessage class]);
  1399. if(![_messageModel.quoteMessage isKindOfClass:[ChatMessageModel class]]){
  1400. return;
  1401. }
  1402. if (_messageModel.quoteMessage.messageType == ChatMessageTypeText) {
  1403. QuoteDetailController * quoteDetailVc = [[QuoteDetailController alloc] init];
  1404. quoteDetailVc.quotedContent = _messageModel.quoteMessage.content;
  1405. quoteDetailVc.quotedSenderName = _messageModel.quoteMessage.nickName;
  1406. UINavigationController * navi = [[UINavigationController alloc] initWithRootViewController:quoteDetailVc];
  1407. navi.modalPresentationStyle = UIModalPresentationFullScreen;
  1408. [self.parentViewController presentViewController:navi animated:YES completion:nil];
  1409. }else if (_messageModel.quoteMessage.messageType == ChatMessageTypeImage) {
  1410. [FilePreviewer.shared previewFileWithLocalPath:_messageModel.quoteMessage.url remoteURL:getURL(_messageModel.quoteMessage.url) fromViewController:self.parentViewController];
  1411. }else if (_messageModel.quoteMessage.messageType == ChatMessageTypeVoice) {
  1412. [FilePreviewer.shared previewFileWithLocalPath:_messageModel.quoteMessage.url remoteURL:getURL(_messageModel.quoteMessage.url) fromViewController:self.parentViewController];
  1413. }else if (_messageModel.quoteMessage.messageType == ChatMessageTypeFile) {
  1414. [FilePreviewer.shared previewFileWithLocalPath:_messageModel.quoteMessage.url remoteURL:getURL(_messageModel.quoteMessage.url) fromViewController:self.parentViewController];
  1415. }else if (_messageModel.quoteMessage.messageType == ChatMessageTypeVoice) {
  1416. }
  1417. }
  1418. - (void)handleAvatarTap {
  1419. !self.avatarTapBlock?:self.avatarTapBlock(_messageModel);
  1420. }
  1421. #pragma mark - 高度计算
  1422. + (CGFloat)cellHeightForMessage:(ChatMessageModel *)message {
  1423. // 如果有缓存高度,直接返回
  1424. if (message.cellHeight > 0) {
  1425. return message.cellHeight;
  1426. }
  1427. CGFloat height = 0;
  1428. CGFloat contentHeight = 0;
  1429. CGFloat horizontalMargin = 12.0f;
  1430. CGFloat verticalMargin = 10.0f;
  1431. CGFloat maxWidth = kContentMaxWidth - 2 * horizontalMargin;
  1432. // 计算内容高度
  1433. switch (message.messageType) {
  1434. case ChatMessageTypeText: {
  1435. NSString *text = message.content ?: @"";
  1436. UIFont *font = [UIFont systemFontOfSize:17]; // 设置字体大小
  1437. CGSize maximumSize = CGSizeMake(maxWidth, CGFLOAT_MAX); // 设置最大宽度,高度设为CGFLOAT_MAX表示不限制高度
  1438. NSDictionary *attributes = @{NSFontAttributeName: font};
  1439. CGRect rect = [text boundingRectWithSize:maximumSize
  1440. options:NSStringDrawingUsesLineFragmentOrigin
  1441. attributes:attributes
  1442. context:nil];
  1443. CGFloat height = rect.size.height;
  1444. contentHeight = height + 2 * verticalMargin;
  1445. contentHeight = MAX(contentHeight, kMinBubbleHeight);
  1446. if (message.quoteMessage) {
  1447. contentHeight = contentHeight + 30;
  1448. }
  1449. break;
  1450. }
  1451. case ChatMessageTypeImage:
  1452. case ChatMessageTypeVideo: {
  1453. CGSize mediaSize = [self otherAdjustMediaSize:message.mediaSize];
  1454. contentHeight = mediaSize.height;
  1455. break;
  1456. }
  1457. case ChatMessageTypeFile:
  1458. contentHeight = 60.0f;
  1459. break;
  1460. case ChatMessageTypeCallBack:
  1461. contentHeight = 20.f;
  1462. break;
  1463. case ChatMessageTypeVoice:
  1464. contentHeight = 30.0f;
  1465. break;
  1466. case ChatMessageTypeCall:
  1467. contentHeight = 40.0f;
  1468. break;
  1469. case ChatMessageTypeHistory:
  1470. contentHeight = 80.0f;
  1471. break;
  1472. case ChatMessageTypeEvent:
  1473. contentHeight = 20.f;
  1474. break;
  1475. case ChatMessageTypeCallBack2:
  1476. contentHeight = 20.f;
  1477. break;
  1478. case ChatMessageTypeDel:
  1479. contentHeight = 20.f;
  1480. break;
  1481. }
  1482. // 总高度 = 内容高度 + 上下边距
  1483. height = contentHeight + kBubbleTopMargin+kBubbleBottomMargin;
  1484. // 缓存高度
  1485. message.cellHeight = height;
  1486. return height;
  1487. }
  1488. + (void)precalculateHeightForMessage:(ChatMessageModel *)message completion:(void(^)(CGFloat height))completion {
  1489. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1490. CGFloat height = [self cellHeightForMessage:message];
  1491. dispatch_async(dispatch_get_main_queue(), ^{
  1492. if (completion) completion(height);
  1493. });
  1494. });
  1495. }
  1496. @end