chatCellView.m 70 KB

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