chatCellView.m 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814
  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. // if(_messageModel.fileError==1){
  732. //
  733. // self.thumbnailView.image = [UIImage imageNamed:@"pictrue_load_fail"];
  734. // _currentContentView = _videoContentView;
  735. // return;
  736. // }
  737. weakSelf(self);
  738. if (_messageModel.videoThumbnailImage) {
  739. // NSLog(@"videoThumbnailImage----:%@",_messageModel.videoThumbnailImage);
  740. _thumbnailView.image = _messageModel.videoThumbnailImage;
  741. [_thumbnailView mas_makeConstraints:^(MASConstraintMaker *make) {
  742. make.edges.equalTo(_videoContentView);
  743. }];
  744. }else{
  745. // NSLog(@"videoThumbnailImage----load");
  746. [_messageModel generateThumbnailWithCompletion:^(UIImage * _Nonnull thumbnail) {
  747. // NSLog(@"111111---------------");
  748. if (thumbnail) {
  749. // NSLog(@"2222---------------");
  750. dispatch_async(dispatch_get_main_queue(), ^{
  751. [weakself.thumbnailView mas_makeConstraints:^(MASConstraintMaker *make) {
  752. make.edges.equalTo(self->_videoContentView);
  753. }];
  754. weakself.thumbnailView.image = thumbnail;
  755. });
  756. }
  757. }];
  758. }
  759. [_messageModel xiazaishipin:^(NSInteger persent) {
  760. if(persent>=100){
  761. dispatch_async(dispatch_get_main_queue(), ^{
  762. if(weakself.jinduLbl){
  763. weakself.jinduLbl.alpha = 0;
  764. weakself.loadingImg.alpha = 0;
  765. }
  766. });
  767. NSLog(@"下载完成:%@",self->_messageModel.localurl);
  768. }
  769. else{
  770. // NSLog(@"下载中:%ld",(long)persent);
  771. dispatch_async(dispatch_get_main_queue(), ^{
  772. if(weakself.jinduLbl){
  773. if(weakself.loadingImg.alpha!=1||!weakself.loadingImg.image){
  774. NSString *gifPath = [[NSBundle mainBundle] pathForResource:@"load" ofType:@"gif"];
  775. NSData *gifData = [NSData dataWithContentsOfFile:gifPath];
  776. UIImage *placeholder = [UIImage sd_imageWithGIFData:gifData];
  777. weakself.loadingImg.image = placeholder;
  778. weakself.loadingImg.alpha = 1;
  779. }
  780. weakself.jinduLbl.alpha = 1;
  781. weakself.jinduLbl.text = [NSString stringWithFormat:@"%ld",(long)persent];
  782. }
  783. });
  784. }
  785. }];
  786. _messageModel.uploadPersentChange = ^(NSInteger index,NSInteger localtime) {
  787. if(localtime==weakself.messageModel.localtime){
  788. if(index>=100){
  789. dispatch_async(dispatch_get_main_queue(), ^{
  790. if(weakself.jinduLbl){
  791. weakself.jinduLbl.alpha = 0;
  792. weakself.loadingImg.alpha = 0;
  793. }
  794. });
  795. }
  796. else{
  797. dispatch_async(dispatch_get_main_queue(), ^{
  798. if(weakself.jinduLbl){
  799. if(weakself.loadingImg.alpha!=1||!weakself.loadingImg.image){
  800. NSString *gifPath = [[NSBundle mainBundle] pathForResource:@"load" ofType:@"gif"];
  801. NSData *gifData = [NSData dataWithContentsOfFile:gifPath];
  802. UIImage *placeholder = [UIImage sd_imageWithGIFData:gifData];
  803. weakself.loadingImg.image = placeholder;
  804. weakself.loadingImg.alpha = 1;
  805. }
  806. weakself.jinduLbl.alpha = 1;
  807. weakself.jinduLbl.text = [NSString stringWithFormat:@"%ld",(long)index];
  808. }
  809. });
  810. }
  811. }
  812. else{
  813. dispatch_async(dispatch_get_main_queue(), ^{
  814. if(weakself.jinduLbl){
  815. weakself.jinduLbl.alpha = 0;
  816. weakself.loadingImg.alpha = 0;
  817. }
  818. });
  819. }
  820. };
  821. _currentContentView = _videoContentView;
  822. }
  823. - (void)setupFileContent {
  824. _fileContentView.hidden = NO;
  825. // 获取子视图
  826. UIImageView *iconView = _fileContentView.subviews[0];
  827. UILabel *nameLabel = _fileContentView.subviews[1];
  828. UILabel *sizeLabel = _fileContentView.subviews[2];
  829. // 设置文件内容
  830. iconView.image = [UIImage imageNamed: @"file_icon_black"];
  831. nameLabel.text = _messageModel.fileName ?: @"未知文件";
  832. sizeLabel.alpha = 0;
  833. [_messageModel xiazaiwenjian:^(NSInteger persent) {
  834. // NSLog(@"setupFileContent----------");
  835. if(persent>=100){
  836. NSLog(@"setupFileContent:下载完成:%@",self->_messageModel.localurl);
  837. dispatch_async(dispatch_get_main_queue(), ^{
  838. if(sizeLabel){
  839. sizeLabel.alpha = 0;
  840. }
  841. });
  842. }
  843. else{
  844. // NSLog(@"setupFileContent 下载中:%ld",(long)persent);
  845. dispatch_async(dispatch_get_main_queue(), ^{
  846. if(sizeLabel){
  847. sizeLabel.alpha = 1;
  848. sizeLabel.text = [NSString stringWithFormat:@"%ld",(long)persent];
  849. }
  850. });
  851. }
  852. }];
  853. _currentContentView = _fileContentView;
  854. }
  855. - (void)setupVoiceContent {
  856. _voiceContentView.hidden = NO;
  857. // 获取子视图
  858. UIImageView *voiceIcon = _voiceContentView.subviews[0];
  859. UILabel *durationLabel = _voiceContentView.subviews[1];
  860. voiceIcon.animationDuration = 1.f;
  861. voiceIcon.animationRepeatCount = 0;
  862. // 设置语音内容
  863. voiceIcon.image = [UIImage imageNamed:_messageModel.isSender ? @"yuyinR" : @"yuyinL"];
  864. durationLabel.text = [NSString stringWithFormat:@"%ld\"", (long)_messageModel.voiceDuration];
  865. // 根据发送方调整布局
  866. if (_messageModel.isSender) {
  867. [voiceIcon mas_remakeConstraints:^(MASConstraintMaker *make) {
  868. make.right.equalTo(_voiceContentView).offset(-8);
  869. make.centerY.equalTo(_voiceContentView);
  870. make.width.height.mas_equalTo(20);
  871. }];
  872. [durationLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  873. make.right.equalTo(voiceIcon.mas_left).offset(-8);
  874. make.centerY.equalTo(_voiceContentView);
  875. }];
  876. voiceIcon.animationImages = @[kImageMake(@"audio_sender_animate1"),kImageMake(@"audio_sender_animate2"),kImageMake(@"audio_sender_animate3")];
  877. } else {
  878. [voiceIcon mas_remakeConstraints:^(MASConstraintMaker *make) {
  879. make.left.equalTo(_voiceContentView).offset(8);
  880. make.centerY.equalTo(_voiceContentView);
  881. make.width.height.mas_equalTo(20);
  882. }];
  883. [durationLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  884. make.left.equalTo(voiceIcon.mas_right).offset(8);
  885. make.centerY.equalTo(_voiceContentView);
  886. }];
  887. voiceIcon.animationImages = @[kImageMake(@"audio_receiver_animate1"),kImageMake(@"audio_receiver_animate2"),kImageMake(@"audio_receiver_animate3")];
  888. }
  889. [_messageModel xiazaiyuyin:^(NSInteger persent) {
  890. // NSLog(@"setupFileContent----------");
  891. if(persent>=100){
  892. NSLog(@"setupFileContent:下载完成:%@",self->_messageModel.localurl);
  893. }
  894. else{
  895. // NSLog(@"setupFileContent 下载中:%ld",(long)persent);
  896. }
  897. }];
  898. _currentContentView = _voiceContentView;
  899. }
  900. - (void)setupCallContent {
  901. _callContentView.hidden = NO;
  902. // 获取子视图
  903. UIImageView *callIcon = _callContentView.subviews[0];
  904. UILabel *callLabel = _callContentView.subviews[1];
  905. UILabel *durationLabel = _callContentView.subviews[2];
  906. // 设置通话记录内容
  907. callIcon.image = [UIImage imageNamed:_messageModel.isVideo ? @"vedio_call_icon" : @"audio_call_icon"];
  908. callLabel.text = _messageModel.isCallSuccess ? @"通话时长" : @"未接通";
  909. durationLabel.text = _messageModel.isCallSuccess ? [self formatTimeWithMilliseconds:_messageModel.callDuration] : @"";
  910. _currentContentView = _callContentView;
  911. }
  912. - (void)setupHistoryContent {
  913. _historyContentView.hidden = NO;
  914. // 获取子视图
  915. UILabel *titleLabel = _historyContentView.subviews[0];
  916. UILabel *contentLabel = (UILabel *)_historyContentView.subviews[1];
  917. // 设置聊天记录内容
  918. titleLabel.text = @"聊天记录";
  919. NSArray * msgArray = _messageModel.forwardMsgArray;
  920. NSMutableArray * contentArray = [NSMutableArray array];
  921. for (NSDictionary * dict in msgArray) {
  922. NSString * messageType = dict[@"messageType"];
  923. NSString * nickName = dict[@"fromName"]?:@"";
  924. NSString * recordString = @"";
  925. switch ([messageType integerValue]) {
  926. case 0:
  927. // recordString = [NSString stringWithFormat:@"%@:%@\n",nickName,dict[@"content"]?:@""];
  928. recordString = [NSString stringWithFormat:@"%@:%@\n",nickName,[CryptoAES.shareInstance decryptDataL:dict[@"content"]]];
  929. break;
  930. case 1:
  931. recordString = [NSString stringWithFormat:@"%@:【图片】\n",nickName];;
  932. break;
  933. case 2:
  934. recordString = [NSString stringWithFormat:@"%@:【文件】\n",nickName];
  935. break;
  936. case 3:
  937. recordString = [NSString stringWithFormat:@"%@:【语音】\n",nickName];
  938. break;
  939. case 5:
  940. recordString = [NSString stringWithFormat:@"%@:【视频】\n",nickName];
  941. break;
  942. case 6:
  943. recordString = [NSString stringWithFormat:@"%@:【转发】\n",nickName];
  944. break;
  945. case 7:
  946. recordString = [NSString stringWithFormat:@"%@:【通话】\n",nickName];
  947. break;
  948. default:
  949. break;
  950. }
  951. [contentArray addObject:recordString];
  952. if (msgArray.lastObject == dict) {
  953. NSString * contentString = [contentArray componentsJoinedByString:@""];
  954. NSMutableAttributedString *content = [[NSMutableAttributedString alloc] initWithString:contentString ?: @""];
  955. contentLabel.attributedText = content;
  956. }
  957. }
  958. _currentContentView = _historyContentView;
  959. }
  960. - (void)setupEventContent {
  961. _eventLbl.hidden = NO;
  962. _eventLbl.text = _messageModel.content;
  963. [_eventLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
  964. make.height.mas_equalTo(24);
  965. make.centerX.mas_equalTo(self.contentView.mas_centerX);
  966. make.centerY.mas_equalTo(self.contentView.mas_centerY);
  967. }];
  968. [_eventLbl sizeToFit];
  969. _currentContentView = _eventLbl;
  970. }
  971. - (void)setupCallbackContent {
  972. _callbackLbl.hidden = NO;
  973. _callbackLbl.text = _messageModel.isSender ? @"你撤回了一条消息":[NSString stringWithFormat:@"%@撤回了一条消息",_messageModel.nickName];
  974. [_callbackLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
  975. make.height.mas_equalTo(24);
  976. make.centerX.mas_equalTo(self.contentView.mas_centerX);
  977. make.centerY.mas_equalTo(self.contentView.mas_centerY);
  978. }];
  979. _currentContentView = _callbackLbl;
  980. }
  981. - (void)setupDeleateContent {
  982. _deleateLbl.hidden = NO;
  983. _deleateLbl.text = @"你删除了一条消息!";
  984. [_deleateLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
  985. make.height.mas_equalTo(24);
  986. make.centerX.mas_equalTo(self.contentView.mas_centerX);
  987. make.centerY.mas_equalTo(self.contentView.mas_centerY);
  988. }];
  989. _currentContentView = _deleateLbl;
  990. }
  991. #pragma mark - 布局方法
  992. - (void)layoutContentViews {
  993. if (_messageModel.messageType == ChatMessageTypeEvent || _messageModel.messageType == ChatMessageTypeCallBack || _messageModel.messageType == ChatMessageTypeCallBack2) {
  994. return;
  995. }
  996. //复选框布局
  997. // self.batchStateBtn.frame=CGRectMake(20, 10, 32, 32);
  998. [self.batchStateBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
  999. make.size.mas_equalTo(CGSizeMake(32, 32));
  1000. make.left.mas_equalTo(20);
  1001. make.top.mas_equalTo(10);
  1002. }];
  1003. // 头像布局
  1004. if (_messageModel.isSender) {
  1005. [_avatarImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  1006. make.right.equalTo(self.contentView).offset(-kAvatarMargin);
  1007. make.top.equalTo(self.contentView).offset(kVerticalMargin);
  1008. make.width.height.mas_equalTo(kAvatarSize);
  1009. }];
  1010. [_nameLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  1011. make.right.equalTo(_avatarImageView.mas_left).offset(-4);
  1012. make.top.equalTo(_avatarImageView);
  1013. make.left.greaterThanOrEqualTo(self.contentView).offset(10);
  1014. }];
  1015. [_timeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  1016. make.height.mas_equalTo(14);
  1017. make.right.mas_equalTo(self.nameLabel.mas_left).offset(-12);
  1018. make.top.mas_equalTo(self.avatarImageView);
  1019. }];
  1020. if (_messageModel.messageType == ChatMessageTypeImage || _messageModel.messageType == ChatMessageTypeVideo) {
  1021. [_readStateLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
  1022. make.right.mas_equalTo(self.contentView).offset(-30);
  1023. make.height.mas_equalTo(14);
  1024. make.top.mas_equalTo(self.currentContentView.mas_bottom).offset(2);
  1025. }];
  1026. }else{
  1027. if (!_quoteLbl.hidden) {
  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.quoteLbl.mas_bottom);
  1032. }];
  1033. }else{
  1034. [_readStateLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
  1035. make.right.mas_equalTo(self.contentView).offset(-30);
  1036. make.height.mas_equalTo(14);
  1037. make.top.mas_equalTo(self.bubbleImageView.mas_bottom);
  1038. }];
  1039. }
  1040. }
  1041. } else {
  1042. [_avatarImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  1043. make.left.equalTo(self.contentView).offset(kAvatarMargin);
  1044. make.top.equalTo(self.contentView).offset(kVerticalMargin);
  1045. make.width.height.mas_equalTo(kAvatarSize);
  1046. }];
  1047. [_nameLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  1048. make.left.equalTo(_avatarImageView.mas_right).offset(4);
  1049. make.top.equalTo(_avatarImageView);
  1050. make.right.lessThanOrEqualTo(self.contentView).offset(-10);
  1051. }];
  1052. [_timeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  1053. make.height.mas_equalTo(14);
  1054. make.left.mas_equalTo(self.nameLabel.mas_right).offset(12);
  1055. make.top.mas_equalTo(self.avatarImageView);
  1056. }];
  1057. }
  1058. // 内容视图布局
  1059. [self layoutCurrentContentView];
  1060. // 气泡背景布局
  1061. if (!_bubbleImageView.hidden) {
  1062. NSAssert(_currentContentView, @"currentContentView 不能为 nil");
  1063. NSAssert(_currentContentView.superview, @"currentContentView 必须已添加到父视图");
  1064. [_bubbleImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  1065. make.edges.equalTo(_currentContentView).insets(UIEdgeInsetsMake(-kTextVerticalMargin, -kTextHorizontalMargin, -kTextVerticalMargin, -kTextHorizontalMargin));
  1066. }];
  1067. [self.contentView sendSubviewToBack:_bubbleImageView];
  1068. }
  1069. }
  1070. - (void)layoutCurrentContentView {
  1071. switch (_messageModel.messageType) {
  1072. case ChatMessageTypeText:
  1073. [self layoutTextContent];
  1074. break;
  1075. case ChatMessageTypeImage:
  1076. [self layoutImageContent];
  1077. break;
  1078. case ChatMessageTypeVideo:
  1079. [self layoutVideoContent];
  1080. break;
  1081. case ChatMessageTypeVoice:
  1082. [self layoutVioceContent];
  1083. break;
  1084. case ChatMessageTypeCall:
  1085. [self layoutCallContent];
  1086. break;
  1087. case ChatMessageTypeHistory:
  1088. [self layoutHistoryContent];
  1089. break;
  1090. default:
  1091. [self layoutOtherContent];
  1092. break;
  1093. }
  1094. }
  1095. - (void)layoutTextContent {
  1096. CGSize textSize = [self calculateTextSize];
  1097. if (_messageModel.isSender) {
  1098. [_textLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
  1099. make.right.equalTo(_avatarImageView.mas_left).offset(-26);
  1100. make.top.mas_equalTo(_avatarImageView.mas_top).offset(28);
  1101. make.width.mas_equalTo(textSize.width);
  1102. make.height.mas_equalTo(textSize.height);
  1103. make.bottom.equalTo(self.contentView).offset(-kVerticalMargin).priorityHigh();
  1104. }];
  1105. if (_messageModel.quoteMessage) {
  1106. [_quoteLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
  1107. make.height.mas_equalTo(20);
  1108. make.top.mas_equalTo(_textLbl.mas_bottom).offset(20);
  1109. make.right.mas_equalTo(_textLbl.mas_right).offset(6);
  1110. make.width.mas_lessThanOrEqualTo(kContentMaxWidth);
  1111. }];
  1112. }
  1113. } else {
  1114. [_textLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
  1115. make.left.equalTo(_avatarImageView.mas_right).offset(26);
  1116. make.top.mas_equalTo(_avatarImageView.mas_top).offset(28);
  1117. make.width.mas_equalTo(textSize.width);
  1118. make.height.mas_equalTo(textSize.height);
  1119. make.bottom.equalTo(self.contentView).offset(-kVerticalMargin).priorityHigh();
  1120. }];
  1121. if (_messageModel.quoteMessage) {
  1122. [_quoteLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
  1123. make.height.mas_equalTo(20);
  1124. make.top.mas_equalTo(_textLbl.mas_bottom).offset(20);
  1125. make.left.mas_equalTo(_textLbl.mas_left).offset(-6);
  1126. make.width.mas_lessThanOrEqualTo(kContentMaxWidth);
  1127. }];
  1128. }
  1129. }
  1130. }
  1131. - (void)layoutImageContent {
  1132. // CGSize imageSize = [self adjustMediaSize:_messageModel.mediaSize];
  1133. CGSize imageSize = _messageModel.mediaSize;
  1134. if (_messageModel.isSender) {
  1135. [_imageContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
  1136. make.right.equalTo(_avatarImageView.mas_left).offset(-16);
  1137. make.top.mas_equalTo(_avatarImageView.mas_top).offset(28);
  1138. make.width.mas_equalTo(imageSize.width);
  1139. make.height.mas_equalTo(imageSize.height);
  1140. make.bottom.equalTo(self.contentView).offset(-kVerticalMargin).priorityHigh();
  1141. }];
  1142. } else {
  1143. [_imageContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
  1144. make.left.equalTo(_avatarImageView.mas_right).offset(16);
  1145. make.top.mas_equalTo(_avatarImageView.mas_top).offset(28);
  1146. make.width.mas_equalTo(imageSize.width);
  1147. make.height.mas_equalTo(imageSize.height);
  1148. make.bottom.equalTo(self.contentView).offset(-kVerticalMargin).priorityHigh();
  1149. }];
  1150. }
  1151. }
  1152. - (void)layoutVideoContent {
  1153. CGSize videoSize = [self adjustMediaSize:_messageModel.mediaSize];
  1154. if (_messageModel.isSender) {
  1155. [_videoContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
  1156. make.right.equalTo(_avatarImageView.mas_left).offset(-16);
  1157. make.top.mas_equalTo(_avatarImageView.mas_top).offset(28);
  1158. make.width.mas_equalTo(videoSize.width);
  1159. make.height.mas_equalTo(videoSize.height);
  1160. make.bottom.equalTo(self.contentView).offset(-kVerticalMargin).priorityHigh();
  1161. }];
  1162. } else {
  1163. [_videoContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
  1164. make.left.equalTo(_avatarImageView.mas_right).offset(16);
  1165. make.top.mas_equalTo(_avatarImageView.mas_top).offset(28);
  1166. make.width.mas_equalTo(videoSize.width);
  1167. make.height.mas_equalTo(videoSize.height);
  1168. make.bottom.equalTo(self.contentView).offset(-kVerticalMargin).priorityHigh();
  1169. }];
  1170. }
  1171. }
  1172. - (void)layoutVioceContent {
  1173. // 设置内容视图约束
  1174. [_voiceContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
  1175. if (_messageModel.isSender) {
  1176. make.right.equalTo(_avatarImageView.mas_left).offset(-26);
  1177. } else {
  1178. make.left.equalTo(_avatarImageView.mas_right).offset(26);
  1179. }
  1180. make.top.equalTo(_avatarImageView).offset(28);
  1181. make.width.mas_equalTo(_messageModel.voiceWidth);
  1182. make.bottom.equalTo(self.contentView).offset(-kVerticalMargin).priorityHigh();
  1183. }];
  1184. }
  1185. - (void)layoutCallContent {
  1186. // 获取子视图
  1187. UIImageView *callIcon = _callContentView.subviews[0];
  1188. UILabel *callLabel = _callContentView.subviews[1];
  1189. UILabel *durationLabel = _callContentView.subviews[2];
  1190. if (_messageModel.isSender) {
  1191. [_callContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
  1192. make.right.equalTo(_avatarImageView.mas_left).offset(-26);
  1193. make.top.mas_equalTo(_avatarImageView.mas_top).offset(28);
  1194. make.width.mas_lessThanOrEqualTo(kContentMaxWidth);
  1195. make.bottom.equalTo(self.contentView).offset(-kVerticalMargin).priorityHigh();
  1196. }];
  1197. } else {
  1198. [_callContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
  1199. make.left.equalTo(_avatarImageView.mas_right).offset(26);
  1200. make.top.mas_equalTo(_avatarImageView.mas_top).offset(28);
  1201. make.width.mas_lessThanOrEqualTo(kContentMaxWidth);
  1202. make.bottom.equalTo(self.contentView).offset(-kVerticalMargin).priorityHigh();
  1203. }];
  1204. }
  1205. if (_messageModel.isCallSuccess) {
  1206. [callLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  1207. make.left.equalTo(callIcon.mas_right).offset(8);
  1208. make.top.equalTo(_callContentView).offset(8);
  1209. }];
  1210. [durationLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  1211. make.left.equalTo(callLabel.mas_right).offset(8);
  1212. make.top.equalTo(callLabel);
  1213. make.right.equalTo(_callContentView).offset(-8);
  1214. make.bottom.equalTo(_callContentView).offset(-8);
  1215. }];
  1216. }else{
  1217. [callLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  1218. make.left.equalTo(callIcon.mas_right).offset(8);
  1219. make.top.equalTo(_callContentView).offset(8);
  1220. make.width.mas_lessThanOrEqualTo(50);
  1221. make.right.equalTo(_callContentView).offset(-8);
  1222. }];
  1223. }
  1224. }
  1225. - (void)layoutHistoryContent {
  1226. [_historyContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
  1227. if (_messageModel.isSender) {
  1228. make.right.equalTo(_avatarImageView.mas_left).offset(-26);
  1229. } else {
  1230. make.left.equalTo(_avatarImageView.mas_right).offset(26);
  1231. }
  1232. make.top.equalTo(_avatarImageView).offset(28);
  1233. make.width.mas_equalTo(160);
  1234. make.bottom.equalTo(self.contentView).offset(-kVerticalMargin).priorityHigh();
  1235. }];
  1236. }
  1237. - (void)layoutOtherContent {
  1238. if (_messageModel.isSender) {
  1239. [_currentContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
  1240. make.right.equalTo(_avatarImageView.mas_left).offset(-26);
  1241. make.top.mas_equalTo(_avatarImageView.mas_top).offset(28);
  1242. make.width.mas_lessThanOrEqualTo(kContentMaxWidth);
  1243. make.bottom.equalTo(self.contentView).offset(-kVerticalMargin).priorityHigh();
  1244. }];
  1245. } else {
  1246. [_currentContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
  1247. make.left.equalTo(_avatarImageView.mas_right).offset(26);
  1248. make.top.mas_equalTo(_avatarImageView.mas_top).offset(28);
  1249. make.width.mas_lessThanOrEqualTo(kContentMaxWidth);
  1250. make.bottom.equalTo(self.contentView).offset(-kVerticalMargin).priorityHigh();
  1251. }];
  1252. }
  1253. }
  1254. - (CGSize)adjustMediaSize:(CGSize)originalSize {
  1255. CGFloat maxWidth = kContentMaxWidth;
  1256. CGFloat maxHeight = 200.0f;
  1257. if (originalSize.width <= 0 || originalSize.height <= 0) {
  1258. return CGSizeMake(maxWidth, maxWidth * 0.6);
  1259. }
  1260. CGFloat ratio = originalSize.width / originalSize.height;
  1261. CGFloat adjustedWidth = maxWidth;
  1262. CGFloat adjustedHeight = adjustedWidth / ratio;
  1263. if (adjustedHeight > maxHeight) {
  1264. adjustedHeight = maxHeight;
  1265. adjustedWidth = adjustedHeight * ratio;
  1266. }
  1267. return CGSizeMake(adjustedWidth, adjustedHeight);
  1268. }
  1269. + (CGSize)otherAdjustMediaSize:(CGSize)originalSize {
  1270. CGFloat maxWidth = kContentMaxWidth;
  1271. CGFloat maxHeight = 200.0f;
  1272. if (originalSize.width <= 0 || originalSize.height <= 0) {
  1273. return CGSizeMake(maxWidth, maxWidth * 0.6);
  1274. }
  1275. CGFloat ratio = originalSize.width / originalSize.height;
  1276. CGFloat adjustedWidth = maxWidth;
  1277. CGFloat adjustedHeight = adjustedWidth / ratio;
  1278. if (adjustedHeight > maxHeight) {
  1279. adjustedHeight = maxHeight;
  1280. adjustedWidth = adjustedHeight * ratio;
  1281. }
  1282. return CGSizeMake(adjustedWidth, adjustedHeight);
  1283. }
  1284. - (CGSize)calculateTextSize{
  1285. NSString *text = _messageModel.content ?: @"";
  1286. UILabel *label = [[UILabel alloc] init];
  1287. label.font = [UIFont systemFontOfSize:18.0]; // 设置字体大小
  1288. label.numberOfLines = 0; // 设置为0表示不限制行数
  1289. label.text = text; // 设置文本内容
  1290. label.preferredMaxLayoutWidth = kContentMaxWidth; // 设置固定宽度
  1291. CGSize size = [label sizeThatFits:CGSizeMake(kContentMaxWidth, CGFLOAT_MAX)];
  1292. CGSize textSize = size;
  1293. // NSLog(@"文本宽度: %f, 文本高度: %f", textSize.width, textSize.height);
  1294. return textSize;
  1295. }
  1296. - (NSString *)formatTimeWithMilliseconds:(NSInteger)milliseconds{
  1297. NSInteger seconds = milliseconds / 1000.0;
  1298. if (seconds >= 60) {
  1299. // 大于等于60秒时显示为分钟,保留一位小数
  1300. NSInteger minutes = seconds / 60.0;
  1301. NSInteger sc = seconds%60;
  1302. return [NSString stringWithFormat:@"%ld分%ld秒", (long)minutes,(long)sc];
  1303. } else {
  1304. // 小于60秒时显示为秒,保留整数
  1305. return [NSString stringWithFormat:@"%ld秒", seconds];
  1306. }
  1307. }
  1308. #pragma mark 列表事件处理
  1309. - (void)handleImageTap {
  1310. if (_messageModel.messageType == ChatMessageTypeImage) {
  1311. // 处理图片点击
  1312. NSLog(@"点击了图片: %@", _messageModel.url);
  1313. if(_messageModel.fileError==1){
  1314. weakSelf(self);
  1315. if(weakself.bubbleCellLongPressMenuBlock){
  1316. weakself.bubbleCellLongPressMenuBlock(8);
  1317. }
  1318. return;
  1319. }
  1320. if (_messageModel.url.length == 0 && !_messageModel.isSender) {
  1321. [MBProgressHUD showWithText:@"图片正在上传"];
  1322. return;
  1323. }
  1324. [FilePreviewer.shared previewFileWithLocalPath:_messageModel.localurl remoteURL:getURL(_messageModel.url) fromViewController:self.parentViewController];
  1325. }
  1326. }
  1327. - (void)handleVideoTap {
  1328. if (_messageModel.messageType == ChatMessageTypeVideo) {
  1329. // 处理视频点击
  1330. NSLog(@"点击了视频: %@", _messageModel.url);
  1331. if(_messageModel.fileError==1){
  1332. weakSelf(self);
  1333. if(weakself.bubbleCellLongPressMenuBlock){
  1334. weakself.bubbleCellLongPressMenuBlock(8);
  1335. }
  1336. return;
  1337. }
  1338. if (_messageModel.url.length == 0 && !_messageModel.isSender) {
  1339. [MBProgressHUD showWithText:@"视频正在上传"];
  1340. return;
  1341. }
  1342. [FilePreviewer.shared previewFileWithLocalPath:_messageModel.localurl remoteURL:getURL(_messageModel.url) fromViewController:self.parentViewController];
  1343. }
  1344. }
  1345. - (void)handleFileTap {
  1346. if (_messageModel.messageType == ChatMessageTypeFile) {
  1347. // 处理视频点击
  1348. NSLog(@"点击了文件: %@", _messageModel.url);
  1349. if(_messageModel.fileError==1){
  1350. weakSelf(self);
  1351. if(weakself.bubbleCellLongPressMenuBlock){
  1352. weakself.bubbleCellLongPressMenuBlock(8);
  1353. }
  1354. return;
  1355. }
  1356. if (_messageModel.url.length == 0 && !_messageModel.isSender) {
  1357. [MBProgressHUD showWithText:@"文件正在上传"];
  1358. return;
  1359. }
  1360. [FilePreviewer.shared previewFileWithLocalPath:_messageModel.localurl remoteURL:getURL(_messageModel.url) fromViewController:self.parentViewController];
  1361. }
  1362. }
  1363. - (void)handleVoiceTap {
  1364. UIImageView *voiceIcon = _voiceContentView.subviews[0];
  1365. if (_messageModel.messageType == ChatMessageTypeVoice) {
  1366. // 处理语音点击
  1367. NSLog(@"点击了语音: %@", _messageModel.url);
  1368. if (self.player) {
  1369. AVPlayerItem *playerItem = self.player.currentItem;
  1370. CMTime currentTime = playerItem.currentTime;
  1371. CMTime duration = playerItem.duration;
  1372. [voiceIcon stopAnimating];
  1373. if (self.player.rate == 0.0) {
  1374. if (CMTimeCompare(currentTime, duration) == 0 ||
  1375. CMTimeCompare(currentTime, duration) == 1) {
  1376. // 播放完毕
  1377. NSLog(@"Playback finished");
  1378. } else {
  1379. // 暂停
  1380. NSLog(@"Playback paused");
  1381. [self.player play];
  1382. return;
  1383. }
  1384. } else {
  1385. // 正在播放
  1386. NSLog(@"Playback in progress");
  1387. [self.player pause];
  1388. return;
  1389. }
  1390. }
  1391. NSString *urlstr = _messageModel.url;
  1392. NSURL *Url = [NSURL URLWithString:urlstr];
  1393. NSLog(@"本地路径:%@",_messageModel.localurl);
  1394. if (_messageModel.localurl && [[NSFileManager defaultManager] fileExistsAtPath:_messageModel.localurl]) {
  1395. NSLog(@"本地路径:");
  1396. urlstr=_messageModel.localurl;
  1397. Url = [NSURL fileURLWithPath:urlstr];
  1398. }
  1399. else{
  1400. }
  1401. NSLog(@"播放: %@", Url);
  1402. AVAudioSession *audioSession = [AVAudioSession sharedInstance];
  1403. [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
  1404. [audioSession setActive:YES error:nil];
  1405. AVPlayerItem *pitem = [AVPlayerItem playerItemWithURL:Url];
  1406. //AVPlayerItem *pitem = [[AVPlayerItem alloc] initWithURL:Url];
  1407. _player = [AVPlayer playerWithPlayerItem:pitem];
  1408. [[UIDevice currentDevice] setProximityMonitoringEnabled:YES]; //建议在播放之前设置yes,播放结束设置NO。这个功能是开启红外感应
  1409. [_player play];
  1410. [[NSNotificationCenter defaultCenter] addObserver:self
  1411. selector:@selector(playerItemDidReachEnd:)
  1412. name:AVPlayerItemDidPlayToEndTimeNotification
  1413. object:pitem];
  1414. [voiceIcon startAnimating];
  1415. }
  1416. }
  1417. - (void)playerItemDidReachEnd:(NSNotification *)notification {
  1418. // 播放结束时的处理代码
  1419. UIImageView *voiceIcon = _voiceContentView.subviews[0];
  1420. [voiceIcon stopAnimating];
  1421. NSLog(@"播放结束:%@",notification);
  1422. [[UIDevice currentDevice] setProximityMonitoringEnabled:NO]; //建议在播放之前设置yes,播放结束设置NO。这个功能是开启红外感应
  1423. [[NSNotificationCenter defaultCenter] removeObserver:self name:notification.name object:notification.object];
  1424. //可以执行继续播放下一条操作
  1425. }
  1426. - (void)handleHistoryTap {
  1427. ChatRecordController * recordVc = [[ChatRecordController alloc] init];
  1428. for (NSDictionary * dict in _messageModel.forwardMsgArray) {
  1429. [recordVc.dataArray addObject:[ChatMessageModel modelWithDictionary:dict]];
  1430. }
  1431. UINavigationController * navi = [[UINavigationController alloc] initWithRootViewController:recordVc];
  1432. navi.modalPresentationStyle = UIModalPresentationFullScreen;
  1433. [self.parentViewController presentViewController:navi animated:YES completion:nil];
  1434. }
  1435. - (void)handleQuoteTap{
  1436. NSLog(@"handleQuoteTap----:%@",[_messageModel.quoteMessage class]);
  1437. if(![_messageModel.quoteMessage isKindOfClass:[ChatMessageModel class]]){
  1438. return;
  1439. }
  1440. if (_messageModel.quoteMessage.messageType == ChatMessageTypeText) {
  1441. QuoteDetailController * quoteDetailVc = [[QuoteDetailController alloc] init];
  1442. quoteDetailVc.quotedContent = _messageModel.quoteMessage.content;
  1443. quoteDetailVc.quotedSenderName = _messageModel.quoteMessage.nickName;
  1444. UINavigationController * navi = [[UINavigationController alloc] initWithRootViewController:quoteDetailVc];
  1445. navi.modalPresentationStyle = UIModalPresentationFullScreen;
  1446. [self.parentViewController presentViewController:navi animated:YES completion:nil];
  1447. }else if (_messageModel.quoteMessage.messageType == ChatMessageTypeImage) {
  1448. [FilePreviewer.shared previewFileWithLocalPath:_messageModel.quoteMessage.url remoteURL:getURL(_messageModel.quoteMessage.url) fromViewController:self.parentViewController];
  1449. }else if (_messageModel.quoteMessage.messageType == ChatMessageTypeVoice) {
  1450. [FilePreviewer.shared previewFileWithLocalPath:_messageModel.quoteMessage.url remoteURL:getURL(_messageModel.quoteMessage.url) fromViewController:self.parentViewController];
  1451. }else if (_messageModel.quoteMessage.messageType == ChatMessageTypeFile) {
  1452. [FilePreviewer.shared previewFileWithLocalPath:_messageModel.quoteMessage.url remoteURL:getURL(_messageModel.quoteMessage.url) fromViewController:self.parentViewController];
  1453. }else if (_messageModel.quoteMessage.messageType == ChatMessageTypeVoice) {
  1454. }
  1455. }
  1456. - (void)handleAvatarTap {
  1457. !self.avatarTapBlock?:self.avatarTapBlock(_messageModel);
  1458. }
  1459. #pragma mark - 高度计算
  1460. + (CGFloat)cellHeightForMessage:(ChatMessageModel *)message {
  1461. // 如果有缓存高度,直接返回
  1462. if (message.cellHeight > 0) {
  1463. return message.cellHeight;
  1464. }
  1465. CGFloat height = 0;
  1466. CGFloat contentHeight = 0;
  1467. CGFloat horizontalMargin = 12.0f;
  1468. CGFloat verticalMargin = 10.0f;
  1469. CGFloat maxWidth = kContentMaxWidth - 2 * horizontalMargin;
  1470. // 计算内容高度
  1471. switch (message.messageType) {
  1472. case ChatMessageTypeText: {
  1473. NSString *text = message.content ?: @"";
  1474. UIFont *font = [UIFont systemFontOfSize:17]; // 设置字体大小
  1475. CGSize maximumSize = CGSizeMake(maxWidth, CGFLOAT_MAX); // 设置最大宽度,高度设为CGFLOAT_MAX表示不限制高度
  1476. NSDictionary *attributes = @{NSFontAttributeName: font};
  1477. CGRect rect = [text boundingRectWithSize:maximumSize
  1478. options:NSStringDrawingUsesLineFragmentOrigin
  1479. attributes:attributes
  1480. context:nil];
  1481. CGFloat height = rect.size.height;
  1482. contentHeight = height + 2 * verticalMargin;
  1483. contentHeight = MAX(contentHeight, kMinBubbleHeight);
  1484. if (message.quoteMessage) {
  1485. contentHeight = contentHeight + 30;
  1486. }
  1487. break;
  1488. }
  1489. case ChatMessageTypeImage:
  1490. case ChatMessageTypeVideo: {
  1491. CGSize mediaSize = [self otherAdjustMediaSize:message.mediaSize];
  1492. contentHeight = mediaSize.height;
  1493. break;
  1494. }
  1495. case ChatMessageTypeFile:
  1496. contentHeight = 60.0f;
  1497. break;
  1498. case ChatMessageTypeCallBack:
  1499. contentHeight = 20.f;
  1500. break;
  1501. case ChatMessageTypeVoice:
  1502. contentHeight = 30.0f;
  1503. break;
  1504. case ChatMessageTypeCall:
  1505. contentHeight = 40.0f;
  1506. break;
  1507. case ChatMessageTypeHistory:
  1508. contentHeight = 80.0f;
  1509. break;
  1510. case ChatMessageTypeEvent:
  1511. contentHeight = 20.f;
  1512. break;
  1513. case ChatMessageTypeCallBack2:
  1514. contentHeight = 20.f;
  1515. break;
  1516. case ChatMessageTypeDel:
  1517. contentHeight = 20.f;
  1518. break;
  1519. }
  1520. // 总高度 = 内容高度 + 上下边距
  1521. height = contentHeight + kBubbleTopMargin+kBubbleBottomMargin;
  1522. // 缓存高度
  1523. message.cellHeight = height;
  1524. return height;
  1525. }
  1526. + (void)precalculateHeightForMessage:(ChatMessageModel *)message completion:(void(^)(CGFloat height))completion {
  1527. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1528. CGFloat height = [self cellHeightForMessage:message];
  1529. dispatch_async(dispatch_get_main_queue(), ^{
  1530. if (completion) completion(height);
  1531. });
  1532. });
  1533. }
  1534. @end