chatCellView.m 70 KB

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