chatCellView.m 69 KB

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