| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886 |
- //
- // chatCellView.m
- // AIIM
- //
- // Created by gan on 2025/6/24.
- //
- #import <Foundation/Foundation.h>
- #import "chatCellView.h"
- #import "ChatLongPressMenuView.h"
- #import "ChatPopoverView.h"
- #import "CryptoAES.h"
- #import <SDWebImage/UIImageView+WebCache.h>
- #import <SDWebImage/SDWebImage.h>
- #import <AVFoundation/AVFoundation.h>
- #import <AVKit/AVKit.h>
- #import "config.h"
- #import "FilePreviewer.h"
- #import "ChatRecordController.h"
- #import "QuoteDetailController.h"
- #import "UILabel+YBAttributeTextTapAction.h"
- #import <SafariServices/SFSafariViewController.h>
- // 定义一些常量
- static const CGFloat kTextVerticalMargin = 10.0f; // 内容垂直边距
- static const CGFloat kTextHorizontalMargin = 12.0f; //文本水平边距
- static const CGFloat kVerticalMargin = 18.0f; // 垂直边距
- static const CGFloat kContentMaxWidth = 220.0f; // 内容最大宽度
- static const CGFloat kAvatarSize = 36.0f; // 头像大小
- static const CGFloat kAvatarMargin = 20.0f; // 头像边距
- static const CGFloat kAvatarBatchMargin = 70.0f; // 批量操作头像边距
- static const CGFloat kBubbleTopMargin = 20.0f; // 气泡顶部距离
- static const CGFloat kBubbleBottomMargin = 40.0f; // 气泡底部距离
- static const CGFloat kBubbleArrowWidth = 6.0f; // 气泡箭头宽度
- static const CGFloat kMinBubbleHeight = 18.0f; // 气泡最小高度
- static const CGFloat kMediaCornerRadius = 4.0f;
- @interface chatCellView()
- // 基本视图
- @property (nonatomic, strong) UIImageView *avatarImageView;
- @property (nonatomic, strong) UILabel *nameLabel;
- @property (nonatomic, strong) UIImageView *bubbleImageView;
- @property (nonatomic, strong) UILabel * timeLabel;
- @property (nonatomic, strong) UILabel * readStateLbl;
- @property (nonatomic, strong) UIButton * batchStateBtn;
- // 发送进度圆环
- @property (nonatomic, strong) CAShapeLayer *progressLayer;
- @property (nonatomic, strong) CAShapeLayer *progressBackgroundLayer;
- @property (nonatomic, strong) UIView *progressContainerView;
- // 发送失败按钮
- @property (nonatomic, strong) UIButton *resendButton;
- // 内容视图
- @property (nonatomic, strong) UILabel *textLbl;
- @property (nonatomic, strong) UIImageView *imageContentView;
- @property (nonatomic, strong) UIView *videoContentView;
- @property (nonatomic, strong) UIImageView *thumbnailView;
- @property (nonatomic, strong) UIImageView *loadingImg;
- @property (nonatomic, strong) UIView *fileContentView;
- @property (nonatomic, strong) UIView *voiceContentView;
- @property (nonatomic, strong) UIView *callContentView;
- @property (nonatomic, strong) UIView *historyContentView;
- @property (nonatomic, strong) UILabel * eventLbl;
- @property (nonatomic, strong) UILabel * callbackLbl;
- @property (nonatomic, strong) UILabel * deleateLbl;
- @property (nonatomic, strong) UILabel * quoteLbl;
- //@property (nonatomic, strong) UILabel * jinduLbl;
- @property (strong, nonatomic) AVPlayer *player;
- @property (strong, nonatomic) AVPlayerViewController *playerViewController;
- // 当前显示的内容视图
- @property (nonatomic, weak) UIView *currentContentView;
- @property (nonatomic,strong) NSArray *matchesArray;
- @end
- @implementation chatCellView
- - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
- self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
- if (self) {
- [self initAlllSubviews];
- self.selectionStyle = UITableViewCellSelectionStyleNone;
- self.backgroundColor = [UIColor clearColor];
- }
- return self;
- }
- - (void)layoutSubviews {
- [super layoutSubviews];
-
- if (_currentContentView && _messageModel.isSender) {
- CGFloat offset = _messageModel.messageType == ChatMessageTypeFile ? 16 : 8;
- [_progressContainerView mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.width.height.mas_equalTo(30);
- make.right.equalTo(_currentContentView.mas_left).offset(-offset);
- make.centerY.equalTo(_currentContentView);
- }];
-
- [_resendButton mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.right.equalTo(_currentContentView.mas_left).offset(-8);
- make.centerY.equalTo(_currentContentView);
- }];
- }
- }
- -(void)initAlllSubviews{
- //复选框
- _batchStateBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- [_batchStateBtn setBackgroundImage:kImageMake(@"checkbox_unselected") forState:UIControlStateNormal];
- [_batchStateBtn setBackgroundImage:kImageMake(@"checkbox_selected") forState:UIControlStateSelected];
- _batchStateBtn.hidden = YES;
- [self.contentView addSubview:_batchStateBtn];
- [_batchStateBtn addTarget:self action:@selector(batchStateButtonClicked) forControlEvents:UIControlEventTouchUpInside];
- // 头像
- _avatarImageView = [UIImageView new];
- _avatarImageView.layer.cornerRadius = kAvatarSize / 2;
- _avatarImageView.layer.masksToBounds = YES;
- _avatarImageView.userInteractionEnabled = YES;
- _avatarImageView.contentMode = UIViewContentModeScaleAspectFill;
- [self.contentView addSubview:_avatarImageView];
-
- UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleAvatarTap)];
- [_avatarImageView addGestureRecognizer:tap];
-
- // [_avatarImageView jk_addLongPressActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
- // [self handleAvatarLongPress];
- // }];
-
- // 名字
- _nameLabel = [UILabel new];
- _nameLabel.font = SYSFONT(12);
- _nameLabel.textColor = [UIColor whiteColor];
- [self.contentView addSubview:_nameLabel];
-
- // 时间
- _timeLabel = [UILabel new];
- _timeLabel.font = SYSFONT(12);
- _timeLabel.textColor = UIColor.whiteColor;
- [self.contentView addSubview:_timeLabel];
-
- _readStateLbl = [UILabel new];
- _readStateLbl.font = SYSFONT(12);
- _readStateLbl.textColor = UIColor.whiteColor;
- [self.contentView addSubview:_readStateLbl];
-
- // 气泡背景
- _bubbleImageView = [UIImageView new];
- _bubbleImageView.userInteractionEnabled = YES;
- [self.contentView addSubview:_bubbleImageView];
-
- // 初始化所有内容视图
- [self setupTextContentView];
- [self setupImageContentView];
- [self setupVideoContentView];
- [self setupFileContentView];
- [self setupVoiceContentView];
- [self setupCallContentView];
- [self setupHistoryContentView];
- [self setupEventContentView];
- [self setupCallbackContentView];
- [self setupdeleateContentView];
-
- // 初始化进度圆环
- [self setupProgressView];
-
- // 初始化发送失败按钮
- [self setupResendButton];
-
- }
- #pragma mark - 进度圆环配置
- - (void)setupProgressView {
- // 容器视图
- _progressContainerView = [[UIView alloc] init];
- _progressContainerView.hidden = YES;
- [self.contentView addSubview:_progressContainerView];
-
- // 圆环尺寸
- CGFloat size = 30.0f;
- CGFloat lineWidth = 2.0f;
- CGRect circleFrame = CGRectMake(0, 0, size, size);
- UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(size/2, size/2)
- radius:(size - lineWidth) / 2
- startAngle:-M_PI_2
- endAngle:M_PI_2 * 3
- clockwise:YES];
-
- // 背景圆环
- _progressBackgroundLayer = [CAShapeLayer layer];
- _progressBackgroundLayer.path = circlePath.CGPath;
- _progressBackgroundLayer.fillColor = [UIColor clearColor].CGColor;
- _progressBackgroundLayer.strokeColor = [UIColor colorWithWhite:1.0 alpha:0.3].CGColor;
- _progressBackgroundLayer.lineWidth = lineWidth;
- _progressBackgroundLayer.frame = circleFrame;
- [_progressContainerView.layer addSublayer:_progressBackgroundLayer];
-
- // 进度圆环
- _progressLayer = [CAShapeLayer layer];
- _progressLayer.path = circlePath.CGPath;
- _progressLayer.fillColor = [UIColor clearColor].CGColor;
- _progressLayer.strokeColor = [UIColor whiteColor].CGColor;
- _progressLayer.lineWidth = lineWidth;
- _progressLayer.lineCap = kCALineCapRound;
- _progressLayer.strokeEnd = 0.0f;
- _progressLayer.frame = circleFrame;
- [_progressContainerView.layer addSublayer:_progressLayer];
- }
- // 更新进度圆环
- - (void)updateProgress:(CGFloat)progress {
- if (progress < 0) progress = 0;
- if (progress > 1) progress = 1;
-
- dispatch_async(dispatch_get_main_queue(), ^{
- self.progressLayer.strokeEnd = progress;
-
- // // 如果进度完成,隐藏圆环
- // if (progress >= 1.0) {
- // self.progressContainerView.hidden = YES;
- // }
- });
- }
- // 显示进度圆环
- - (void)showProgressView {
- dispatch_async(dispatch_get_main_queue(), ^{
- self.progressContainerView.hidden = NO;
- self.progressLayer.strokeEnd = 0.0f;
- });
- }
- // 隐藏进度圆环
- - (void)hideProgressView {
- dispatch_async(dispatch_get_main_queue(), ^{
- self.progressContainerView.hidden = YES;
- });
- }
- #pragma mark - 发送失败按钮配置
- - (void)setupResendButton {
- _resendButton = [UIButton buttonWithType:UIButtonTypeCustom];
- [_resendButton setImage:[UIImage imageNamed:@"icon_resend"] forState:UIControlStateNormal];
- _resendButton.hidden = YES;
- [_resendButton addTarget:self action:@selector(handleResendButtonTap) forControlEvents:UIControlEventTouchUpInside];
- [self.contentView addSubview:_resendButton];
- }
- // 显示重发按钮
- - (void)showResendButton {
- dispatch_async(dispatch_get_main_queue(), ^{
- self.resendButton.hidden = NO;
- });
- }
- // 隐藏重发按钮
- - (void)hideResendButton {
- dispatch_async(dispatch_get_main_queue(), ^{
- self.resendButton.hidden = YES;
- });
- }
- // 重发按钮点击事件
- - (void)handleResendButtonTap {
- NSLog(@"点击重发按钮: msgId=%@", _messageModel.msgId);
-
- // 触发重发回调
- if (self.resendMessageBlock) {
- self.resendMessageBlock(_messageModel);
- }
- }
- #pragma mark - 内容视图配置
- - (void)setupTextContentView {
- _textLbl = [UILabel new];
- _textLbl.numberOfLines = 0;
- _textLbl.textColor = UIColor.blackColor;
- _textLbl.userInteractionEnabled = YES; // 允许用户交互
- [self.contentView addSubview:_textLbl];
-
- // UITapGestureRecognizer *taptext = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTapped:)];
- // [_textLbl addGestureRecognizer:taptext];
-
-
- _quoteLbl = [UILabel new];
- _quoteLbl.numberOfLines = 1;
- _quoteLbl.font = SYSFONT(16);
- _quoteLbl.textColor = UIColor.whiteColor;
- _quoteLbl.layer.cornerRadius = 2.f;
- _quoteLbl.layer.masksToBounds = YES;
- _quoteLbl.userInteractionEnabled = YES; // 允许用户交互
- [self.contentView addSubview:_quoteLbl];
- // _quoteLbl.backgroundColor = [UIColor redColor];
-
- // 添加点击手势
- UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleQuoteTap)];
- [_quoteLbl addGestureRecognizer:tap];
- [self.contentView bringSubviewToFront:_quoteLbl];
- }
- - (void)setupImageContentView {
- _imageContentView = [UIImageView new];
- _imageContentView.contentMode = UIViewContentModeScaleAspectFit;
- _imageContentView.clipsToBounds = YES;
- _imageContentView.layer.cornerRadius = kMediaCornerRadius;
- _imageContentView.layer.masksToBounds = YES;
- _imageContentView.userInteractionEnabled = YES;
- [self.contentView addSubview:_imageContentView];
-
- // 添加点击手势
- UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleImageTap)];
- [_imageContentView addGestureRecognizer:tap];
- }
- - (void)setupVideoContentView {
- _videoContentView = [UIView new];
- _videoContentView.layer.cornerRadius = kMediaCornerRadius;
- _videoContentView.layer.masksToBounds = YES;
- _videoContentView.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1.0];
- _videoContentView.userInteractionEnabled = YES;
-
-
-
-
-
- _thumbnailView = [[UIImageView alloc] init];
-
- _thumbnailView.contentMode = UIViewContentModeScaleAspectFill;
- _thumbnailView.clipsToBounds = YES;
- _thumbnailView.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1.0];
- [_videoContentView addSubview:_thumbnailView];
- [_thumbnailView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.edges.equalTo(_videoContentView);
- }];
-
- NSString *gifPath = [[NSBundle mainBundle] pathForResource:@"load" ofType:@"gif"];
- NSData *gifData = [NSData dataWithContentsOfFile:gifPath];
- UIImage *placeholder = [UIImage sd_imageWithGIFData:gifData];
-
- _loadingImg = [[UIImageView alloc] initWithImage:placeholder];
- [_videoContentView addSubview:_loadingImg];
- [_loadingImg mas_makeConstraints:^(MASConstraintMaker *make) {
- make.center.equalTo(_videoContentView);
- make.width.height.mas_equalTo(100);
- }];
- _loadingImg.alpha = 0;
-
- UIImageView *playIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"play_btn"]];
- [_videoContentView addSubview:playIcon];
- [playIcon mas_makeConstraints:^(MASConstraintMaker *make) {
- make.center.equalTo(_videoContentView);
- make.width.height.mas_equalTo(40);
- }];
-
- [self.contentView addSubview:_videoContentView];
-
- // 添加点击手势
- UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleVideoTap)];
- [_videoContentView addGestureRecognizer:tap];
- }
- - (void)setupFileContentView {
- _fileContentView = [UIView new];
-
- UIImageView *iconView = [[UIImageView alloc] init];
- UILabel *nameLabel = [[UILabel alloc] init];
- nameLabel.textColor = UIColor.blackColor;
- nameLabel.font = [UIFont systemFontOfSize:14];
- nameLabel.numberOfLines = 1;
- UILabel *sizeLabel = [[UILabel alloc] init];
- sizeLabel.font = [UIFont systemFontOfSize:12];
- sizeLabel.textColor = [UIColor grayColor];
-
- [_fileContentView addSubview:iconView];
- [_fileContentView addSubview:nameLabel];
- [_fileContentView addSubview:sizeLabel];
-
- [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(_fileContentView).offset(8);
- make.centerY.equalTo(_fileContentView);
- make.width.height.mas_equalTo(40);
- }];
-
- [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(iconView.mas_right).offset(8);
- make.top.equalTo(_fileContentView).offset(8);
- make.right.equalTo(_fileContentView).offset(-8);
- }];
-
- [sizeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(nameLabel);
- make.top.equalTo(nameLabel.mas_bottom).offset(4);
- // make.right.equalTo(nameLabel);
- // make.bottom.lessThanOrEqualTo(_fileContentView).offset(-8);
- }];
-
- [self.contentView addSubview:_fileContentView];
-
- // 添加点击手势
- UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleFileTap)];
- [_fileContentView addGestureRecognizer:tap];
- }
- - (void)setupVoiceContentView {
- _voiceContentView = [UIView new];
-
- UIImageView *voiceIcon = [[UIImageView alloc] initWithImage:kImageMake(@"play_btn")];
- UILabel *durationLabel = [[UILabel alloc] init];
- durationLabel.textColor = UIColor.blackColor;
- durationLabel.font = [UIFont systemFontOfSize:14];
-
- [_voiceContentView addSubview:voiceIcon];
- [_voiceContentView addSubview:durationLabel];
-
- [voiceIcon mas_makeConstraints:^(MASConstraintMaker *make) {
- make.centerY.equalTo(_voiceContentView);
- make.width.height.mas_equalTo(20);
- }];
-
- [durationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
- make.centerY.equalTo(_voiceContentView);
- make.left.equalTo(voiceIcon.mas_right).offset(8);
- }];
-
- [self.contentView addSubview:_voiceContentView];
-
- // 添加点击手势
- UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleVoiceTap)];
- [_voiceContentView addGestureRecognizer:tap];
- }
- - (void)setupCallContentView {
- _callContentView = [UIView new];
-
- UIImageView *callIcon = [[UIImageView alloc] init];
- UILabel *callLabel = [[UILabel alloc] init];
- callLabel.textColor = UIColor.blackColor;
- callLabel.font = [UIFont systemFontOfSize:16];
- UILabel *durationLabel = [[UILabel alloc] init];
- durationLabel.font = [UIFont systemFontOfSize:16];
- durationLabel.textColor = [UIColor grayColor];
-
- [_callContentView addSubview:callIcon];
- [_callContentView addSubview:callLabel];
- [_callContentView addSubview:durationLabel];
-
- // [callIcon mas_makeConstraints:^(MASConstraintMaker *make) {
- // make.left.equalTo(_callContentView).offset(8);
- // make.centerY.equalTo(_callContentView);
- // make.width.height.mas_equalTo(20);
- // }];
- //
- // [callLabel mas_makeConstraints:^(MASConstraintMaker *make) {
- // make.left.equalTo(callIcon.mas_right).offset(8);
- // make.top.equalTo(_callContentView).offset(8);
- //// make.width.mas_lessThanOrEqualTo(70);
- //// make.right.equalTo(_callContentView).offset(-8);
- // }];
- //
- // [durationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
- // make.left.equalTo(callLabel.mas_right).offset(8);
- // make.top.equalTo(callLabel);
- // make.right.equalTo(_callContentView).offset(-8);
- // make.bottom.equalTo(_callContentView).offset(-8);
- // }];
-
- [self.contentView addSubview:_callContentView];
-
- // 添加点击手势
- // UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleCallTap)];
- // [_callContentView addGestureRecognizer:tap];
- }
- - (void)setupHistoryContentView {
- _historyContentView = [UIView new];
-
- UILabel *titleLabel = [[UILabel alloc] init];
- titleLabel.font = SYSBFONT(14);
- titleLabel.text = @"聊天记录";
- titleLabel.textColor = UIColor.blackColor;
-
- UILabel *contentLabel = [[UILabel alloc] init];
- contentLabel.font = SYSFONT(12);
- contentLabel.numberOfLines = 6;
- // contentLabel.displaysAsynchronously = YES;
-
- [_historyContentView addSubview:titleLabel];
- [_historyContentView addSubview:contentLabel];
-
- [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.top.equalTo(_historyContentView).offset(8);
- make.right.equalTo(_historyContentView).offset(-8);
- make.height.mas_equalTo(14);
- }];
-
- [contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.right.equalTo(titleLabel);
- make.top.equalTo(titleLabel.mas_bottom).offset(4);
- make.bottom.equalTo(_historyContentView).offset(-8);
- }];
-
- [self.contentView addSubview:_historyContentView];
-
- // 添加点击手势
- UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleHistoryTap)];
- [_historyContentView addGestureRecognizer:tap];
- }
- - (void)setupEventContentView {
- _eventLbl = [[UILabel alloc] init];
- _eventLbl.font = SYSFONT(14);
- _eventLbl.textColor = UIColor.whiteColor;
-
- _eventLbl.layer.cornerRadius = 2.f;
- _eventLbl.layer.masksToBounds = YES;
-
- [self.contentView addSubview:_eventLbl];
- }
- - (void)setupCallbackContentView {
- _callbackLbl = [[UILabel alloc] init];
- _callbackLbl.font = SYSFONT(14);
- _callbackLbl.textColor = UIColor.whiteColor;
- _callbackLbl.layer.cornerRadius = 2.f;
- _callbackLbl.layer.masksToBounds = YES;
-
- [self.contentView addSubview:_callbackLbl];
- }
- - (void)setupdeleateContentView {
- _deleateLbl = [[UILabel alloc] init];
- _deleateLbl.font = SYSFONT(14);
- _deleateLbl.textColor = UIColor.blackColor;
- _deleateLbl.layer.cornerRadius = 2.f;
- _deleateLbl.layer.masksToBounds = YES;
-
- [self.contentView addSubview:_deleateLbl];
- }
- #pragma mark - 数据绑定
- - (void)setMessageModel:(ChatMessageModel *)messageModel {
- // NSLog(@"messageModel:%@",messageModel);
- _messageModel = messageModel;
- // 隐藏所有内容视图
- _textLbl.hidden = YES;
- _quoteLbl.hidden = YES;
- _imageContentView.hidden = YES;
- _videoContentView.hidden = YES;
- _fileContentView.hidden = YES;
- _voiceContentView.hidden = YES;
- _callContentView.hidden = YES;
- _historyContentView.hidden = YES;
- _eventLbl.hidden = YES;
- _callbackLbl.hidden = YES;
- _deleateLbl.hidden = YES;
- _readStateLbl.hidden = YES;
-
- // 根据消息类型配置内容
- switch (messageModel.messageType) {
- case ChatMessageTypeText:
- [self setupTextContent];
- break;
- case ChatMessageTypeImage:
- [self setupImageContent];
- break;
- case ChatMessageTypeVideo:
- [self setupVideoContent];
- break;
- case ChatMessageTypeFile:
- [self setupFileContent];
- break;
- case ChatMessageTypeCallBack:
- //撤回
- [self setupCallbackContent];
- break;
- case ChatMessageTypeVoice:
- [self setupVoiceContent];
- break;
- case ChatMessageTypeCall:
- [self setupCallContent];
- break;
- case ChatMessageTypeHistory:
- [self setupHistoryContent];
- break;
- case ChatMessageTypeEvent:
- [self setupEventContent];
- break;
- case ChatMessageTypeCallBack2:
- //撤回PC
- [self setupCallbackContent];
- break;
- case ChatMessageTypeDel:
- [self setupDeleateContent];
- break;
- }
-
- // 配置头像和名字
- [self setupAvatarAndName];
-
- // 配置气泡背景
- [self setupBubbleBackground];
-
- // 更新布局
- [self layoutContentViews];
-
- //长按事件
- [self setupLongPressEvent];
-
- // 配置发送进度
- [self setupSendingProgress];
-
- }
- #pragma mark - 批量状态
- - (void)setBatchState:(BOOL)batchState{
-
- if (_messageModel.messageType == ChatMessageTypeEvent || _messageModel.messageType == ChatMessageTypeCallBack || _messageModel.messageType == ChatMessageTypeCallBack2) {
- self.batchStateBtn.hidden = YES;
- return;
- }
-
- _batchState = batchState;
- self.batchStateBtn.hidden = !batchState;
- if (!_messageModel.isSender) {
- if (batchState) {
- [_avatarImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.size.mas_equalTo(CGSizeMake(kAvatarSize, kAvatarSize));
- make.left.mas_equalTo(kAvatarBatchMargin);
- make.top.equalTo(self.contentView).offset(kVerticalMargin);
- }];
- }else{
- [_avatarImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.size.mas_equalTo(CGSizeMake(kAvatarSize, kAvatarSize));
- make.left.mas_equalTo(kAvatarMargin);
- make.top.equalTo(self.contentView).offset(kVerticalMargin);
- }];
- }
- }
-
- self.batchStateBtn.selected = NO;
- }
- - (void)setupAvatarAndName {
- self.readStateLbl.text = nil;
- // 这里应该根据实际数据设置头像和名字
- if (_messageModel.avatar) {
- [_avatarImageView sd_setImageWithURL:getURL(_messageModel.avatar) placeholderImage:kImageMake(@"Avatar")];
- }else{
- _avatarImageView.image = kImageMake(@"Avatar");
- }
-
- _timeLabel.text = _messageModel.formatterTime;
- _readStateLbl.text = _messageModel.readStatus;
- if(_messageModel.readstate==4){
- _readStateLbl.textColor = globalColor(GCTypeOrangeR);
- }
- else{
- _readStateLbl.textColor = [UIColor whiteColor];
- }
-
-
- if (_messageModel.type==1) {
- if (_messageModel.nickName && ![_messageModel.nickName isKindOfClass:NSNull.class]) {
- _nameLabel.text = _messageModel.isSender ? @"": _messageModel.nickName;
- _nameLabel.textAlignment = _messageModel.isSender ? NSTextAlignmentRight : NSTextAlignmentLeft;
- }
- }
-
-
- if (_messageModel.messageType == ChatMessageTypeEvent || _messageModel.messageType == ChatMessageTypeCallBack || _messageModel.messageType == ChatMessageTypeCallBack2) {
- _avatarImageView.hidden = YES;
- _nameLabel.hidden = YES;
- _readStateLbl.hidden = YES;
- _timeLabel.hidden = YES;
- }else{
- _avatarImageView.hidden = NO;
- _nameLabel.hidden = NO;
- _readStateLbl.hidden = NO;
- _timeLabel.hidden = NO;
- }
-
- if (_messageModel.type == 0&& _messageModel.isSender) {
- _readStateLbl.hidden = NO;
- }else{
- _readStateLbl.hidden = YES;
- }
- }
- - (void)setupBubbleBackground {
- // 图片和视频类型不需要气泡背景
- if (_messageModel.messageType == ChatMessageTypeImage || _messageModel.messageType == ChatMessageTypeVideo || _messageModel.messageType == ChatMessageTypeEvent|| _messageModel.messageType == ChatMessageTypeCallBack || _messageModel.messageType == ChatMessageTypeCallBack2) {
- _bubbleImageView.hidden = YES;
- return;
- }
-
- _bubbleImageView.hidden = NO;
- UIImage *bubbleImage = _messageModel.isSender ? [UIImage imageNamed:@"rightlt"] : [UIImage imageNamed:@"leftlt"];
-
- // 拉伸气泡图片
- UIEdgeInsets insets = UIEdgeInsetsMake(30, _messageModel.isSender ? 20 : 10, 10, _messageModel.isSender ? 10 : 20);
- bubbleImage = [bubbleImage resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch];
- _bubbleImageView.image = bubbleImage;
- }
- - (void)setupLongPressEvent{
- if (_isHistory) {
- return;
- }
- if ( _messageModel.messageType == ChatMessageTypeEvent || _messageModel.messageType == ChatMessageTypeCallBack || _messageModel.messageType == ChatMessageTypeCall || _messageModel.messageType == ChatMessageTypeCallBack2) {
- return;
- }
-
- // NSLog(@"setupLongPressEvent-------1");
- // 创建长按手势识别器并设置目标方法
- UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressEvent:)];
- longPress.minimumPressDuration = 0.3; // 设置长按的最小时间为0.3秒
- [self.currentContentView addGestureRecognizer:longPress]; // 将手势识别器添加到视图上
- if(_messageModel.messageType ==ChatMessageTypeText){
- [self.bubbleImageView addGestureRecognizer:longPress]; // 将手势识别器添加到视图上
- }
- else{
- [self.currentContentView addGestureRecognizer:longPress]; // 将手势识别器添加到视图上
- }
- }
- // 配置发送进度
- - (void)setupSendingProgress {
- // 只有发送方才显示进度或失败按钮
- if (!_messageModel.isSender || !_messageModel.isUploadFile) {
- [self hideProgressView];
- [self hideResendButton];
- return;
- }
-
- // 根据消息状态显示进度或失败按钮
- switch (_messageModel.messageSendStatus) {
- case MessageSending: {
- // 发送中,显示进度圆环,隐藏失败按钮
- [self showProgressView];
- [self hideResendButton];
-
- // 如果有上传进度回调,监听进度
- weakSelf(self);
- if (_messageModel.isUploadFile) {
- _messageModel.uploadPersentChange = ^(NSInteger persent, NSInteger localtime) {
- if (localtime == weakself.messageModel.localtime) {
- CGFloat progress = persent / 100.0f;
- [weakself updateProgress:progress];
- if (progress >= 1) {
- weakself.messageModel.messageSendStatus = MessageSent;
- }
- }
- };
- }
- break;
- }
- case MessageSendFail: {
- // 发送失败,显示失败按钮,隐藏进度圆环
- [self hideProgressView];
- [self showResendButton];
- break;
- }
- case MessageSent:
- // 其他状态隐藏进度和失败按钮
- [self hideProgressView];
- [self hideResendButton];
- break;
- }
-
- }
- - (void)longPressEvent:(UILongPressGestureRecognizer *)gestureRecognizer {
- if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
- NSLog(@"长按事件开始");
- // 在这里添加你的代码,例如显示菜单、编辑选项等
-
- if (_messageModel.messageType == ChatMessageTypeDel || _messageModel.messageType == ChatMessageTypeCallBack) {
- return;
- }
- weakSelf(self);
-
- if(weakself.bubbleCellLongPressMenuBlock){
- weakself.bubbleCellLongPressMenuBlock(99);
- }
- BOOL isSender = NO;
- if (_messageModel.isSender) {
- isSender = YES;
- }
-
- ChatLongPressMenuView * contenView = [[ChatLongPressMenuView alloc] initWithMessageType:_messageModel.messageType isSender:isSender];
-
- BOOL isMedia = NO;
- if (_messageModel.messageType == ChatMessageTypeImage || _messageModel.messageType == ChatMessageTypeVideo) {
- isMedia = YES;
- }else{
- isMedia = NO;
- }
-
- ChatPopoverView * popover = [[ChatPopoverView alloc] initWithContentView:contenView sourceView:isMedia ? self.currentContentView : self.bubbleImageView];
- [popover show];
-
-
- contenView.indexOfMenuClicked = ^(NSInteger index) {
- [popover dismiss];
- !weakself.bubbleCellLongPressMenuBlock ?: weakself.bubbleCellLongPressMenuBlock(index);
- };
- } else if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
- NSLog(@"长按事件结束");
- }
- NSLog(@"longPressEvent-------");
- }
- - (void)batchStateButtonClicked{
- self.batchStateBtn.selected = !self.batchStateBtn.isSelected;
- !self.batchSelectedStateBlock ?: self.batchSelectedStateBlock(self.batchStateBtn.isSelected);
- }
- #pragma mark - 内容配置具体实现
- - (void)setupTextContent {
- _textLbl.hidden = NO;
-
- if (_messageModel.quoteMessage) {
- self.quoteLbl.hidden = NO;
- self.quoteLbl.text = [NSString stringWithFormat:@"%@:%@",_messageModel.quoteMessage.nickName,_messageModel.quoteMessage.content];
- }else{
- _quoteLbl.hidden = YES;
- }
- // NSLog(@"_messageModel.content:%@",_messageModel.content);
- // 创建富文本
- NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:_messageModel.content ?: @""];
- // 处理特殊内容(链接、@用户等)
- [self highlightSpecialElementsInText:text];
-
- // 同步设置文本,避免闪烁
- // [UIView performWithoutAnimation:^{
- // _textLbl.attributedText = text;
- // }];
- if(_matchesArray.count>0){
- NSMutableArray *array = [NSMutableArray new];
- for (NSTextCheckingResult *match in _matchesArray) {
- if (match.resultType == NSTextCheckingTypeLink) {
- NSString *url =[NSString stringWithFormat:@"%@",match.URL];
- url=[url stringByReplacingOccurrencesOfString:@"http://" withString:@""];
- url=[url stringByReplacingOccurrencesOfString:@"https://" withString:@""];
- [array addObject:url];
- }
- }
- _textLbl.attributedText = [self getAttributeWith:array string:_messageModel.content orginFont:18 orginColor:[UIColor darkGrayColor] attributeFont:18 attributeColor:[UIColor blueColor]];
- // _textLbl.attributedText = text;
- [_textLbl yb_addAttributeTapActionWithStrings:array tapClicked:^(UILabel *label, NSString *string, NSRange range, NSInteger index) {
- NSLog(@"%ld:%@",(long)index,self->_matchesArray[index]);
- NSTextCheckingResult *match=self->_matchesArray[index];
- // [[UIApplication sharedApplication] openURL:match.URL options:@{} completionHandler:nil];
- // 点击后的操作
-
- SFSafariViewController *svc = [[SFSafariViewController alloc] initWithURL:match.URL];
- [self.parentViewController presentViewController:svc animated:YES completion:nil];
-
- // [FilePreviewer.shared previewFileWithLocalPath:@"" remoteURL:match.URL fromViewController:self.parentViewController];
- }];
- }
- else{
- _textLbl.attributedText = text;
- }
- UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressEvent:)];
- longPress.minimumPressDuration = 0.3; // 设置长按的最小时间为0.3秒
- [_textLbl addGestureRecognizer:longPress]; // 将手势识别器添加到视图上
-
- _currentContentView = _textLbl;
- }
- - (NSAttributedString *)getAttributeWith:(id)sender
- string:(NSString *)string
- orginFont:(CGFloat)orginFont
- orginColor:(UIColor *)orginColor
- attributeFont:(CGFloat)attributeFont
- attributeColor:(UIColor *)attributeColor
- {
- __block NSMutableAttributedString *totalStr = [[NSMutableAttributedString alloc] initWithString:string];
- [totalStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:orginFont] range:NSMakeRange(0, string.length)];
- [totalStr addAttribute:NSForegroundColorAttributeName value:orginColor range:NSMakeRange(0, string.length)];
- NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
- [paragraphStyle setLineSpacing:0.0f]; //设置行间距
- [paragraphStyle setLineBreakMode:NSLineBreakByTruncatingTail];
- [paragraphStyle setAlignment:NSTextAlignmentLeft];
- [paragraphStyle setLineBreakMode:NSLineBreakByCharWrapping];
- [totalStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [totalStr length])];
-
- if ([sender isKindOfClass:[NSArray class]]) {
-
- __block NSString *oringinStr = string;
- __weak typeof(self) weakSelf = self;
-
- [sender enumerateObjectsUsingBlock:^(NSString * _Nonnull str, NSUInteger idx, BOOL * _Nonnull stop) {
-
- NSRange range = [oringinStr rangeOfString:str];
- if(range.length>0){
- [totalStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:attributeFont] range:range];
- [totalStr addAttribute:NSForegroundColorAttributeName value:attributeColor range:range];
- // oringinStr = [oringinStr stringByReplacingCharactersInRange:range withString:[weakSelf getStringWithRange:range]];
- }
-
- }];
-
- }else if ([sender isKindOfClass:[NSString class]]) {
-
- NSRange range = [string rangeOfString:sender];
-
- [totalStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:attributeFont] range:range];
- [totalStr addAttribute:NSForegroundColorAttributeName value:attributeColor range:range];
- }
- return totalStr;
- }
- - (NSString *)getStringWithRange:(NSRange)range
- {
- NSMutableString *string = [NSMutableString string];
- for (int i = 0; i < range.length ; i++) {
- [string appendString:@" "];
- }
- return string;
- }
- #pragma mark - 辅助方法
- - (void)highlightSpecialElementsInText:(NSMutableAttributedString *)text {
- // 高亮链接
- NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
- NSArray *matches = [detector matchesInString:text.string options:0 range:NSMakeRange(0, text.length)];
- NSLog(@"matches:%@",matches);
- _matchesArray =matches;
- // 高亮@用户(暂不开放)
- // NSRegularExpression *atRegex = [NSRegularExpression regularExpressionWithPattern:@"@[\\u4e00-\\u9fa5a-zA-Z0-9_-]+" options:0 error:nil];
- // NSArray *atMatches = [atRegex matchesInString:text.string options:0 range:text.yy_rangeOfAll];
- //
- // for (NSTextCheckingResult *match in atMatches) {
- // [text yy_setColor:[UIColor orangeColor] range:match.range];
- // [text yy_setTextHighlightRange:match.range
- // color:[UIColor orangeColor]
- // backgroundColor:[UIColor colorWithWhite:0.9 alpha:0.5]
- // tapAction:^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
- // NSString *atUser = [text.string substringWithRange:range];
- // // 处理@用户点击
- // NSLog(@"点击了: %@", atUser);
- // }];
- // }
- }
- - (void)setupImageContent {
- _imageContentView.hidden = NO;
-
- if (_messageModel.localurl && _messageModel.localurl.length != 0) {//显示本地图片
- NSURL *localFileURL = getURL(_messageModel.localurl);
- // NSLog(@"UIImage------------1");
- // 从 URL 获取文件路径
- NSString *filePath = localFileURL.path;
- // 通过文件路径加载图片
- UIImage *image = [UIImage imageWithContentsOfFile:filePath];
- if (image) {//本地图片显示成功
- // NSLog(@"UIImage------------2");
- _imageContentView.image = image;
- CGImageRef cgImage = image.CGImage;
- CGFloat imageWidth = CGImageGetWidth(cgImage);
- CGFloat imageHeight = CGImageGetHeight(cgImage);
- CGFloat imageScale = imageWidth/imageHeight;
- CGFloat realWidth = 150 * imageScale;
- if(realWidth>220){
- realWidth=220;
- imageHeight=150;
- }
- else{
- imageHeight=150;
- }
- self.messageModel.mediaSize = CGSizeMake(realWidth, imageHeight);
- [self.imageContentView mas_updateConstraints:^(MASConstraintMaker *make) {
- make.width.mas_equalTo(realWidth);
- }];
- _currentContentView = _imageContentView;
- return;
- }
- }
-
- //没有有效本地图片 使用SDWebImage加载图片
- weakSelf(self);
- [_imageContentView sd_setImageWithURL:[NSURL URLWithString:_messageModel.url]
- placeholderImage:[UIImage imageNamed:@"pictrue_placeholder"]
- completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
- if (!error && image) {
- // 图片加载成功后可以更新布局(如果需要)
- // NSLog(@"UIImage------------5");
- CGImageRef cgImage = image.CGImage;
- CGFloat imageWidth = CGImageGetWidth(cgImage);
- CGFloat imageHeight = CGImageGetHeight(cgImage);
- CGFloat imageScale = imageWidth/imageHeight;
- CGFloat realWidth = 150 * imageScale;
- if(realWidth>220){
- realWidth=220;
- imageHeight=150;
- }
- else{
- imageHeight=150;
- }
- weakself.messageModel.mediaSize = CGSizeMake(realWidth, 150);
- [weakself.imageContentView mas_updateConstraints:^(MASConstraintMaker *make) {
- make.width.mas_equalTo(realWidth);
- }];
- }
- }];
- _currentContentView = _imageContentView;
- }
- - (void)setupVideoContent {
- _videoContentView.hidden = NO;
- // if (_thumbnailView.image) {
- // return;
- // }
- // 清除旧视图
- _thumbnailView.image = nil;
- _loadingImg.image = nil;
-
-
- weakSelf(self);
- if (_messageModel.videoThumbnailImage) {
- // NSLog(@"videoThumbnailImage----:%@",_messageModel.videoThumbnailImage);
- _thumbnailView.image = _messageModel.videoThumbnailImage;
- [_thumbnailView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.edges.equalTo(_videoContentView);
- }];
- }else{
- // NSLog(@"videoThumbnailImage----load");
- [_messageModel generateThumbnailWithCompletion:^(UIImage * _Nonnull thumbnail) {
- // NSLog(@"111111---------------");
- if (thumbnail) {
- // NSLog(@"2222---------------");
- dispatch_async(dispatch_get_main_queue(), ^{
- [weakself.thumbnailView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.edges.equalTo(self->_videoContentView);
- }];
- weakself.thumbnailView.image = thumbnail;
- });
- }
- }];
- }
-
- _currentContentView = _videoContentView;
- }
- - (void)setupFileContent {
- _fileContentView.hidden = NO;
-
- // 获取子视图
- UIImageView *iconView = _fileContentView.subviews[0];
- UILabel *nameLabel = _fileContentView.subviews[1];
- UILabel *sizeLabel = _fileContentView.subviews[2];
-
- // 设置文件内容
- iconView.image = [UIImage imageNamed: @"file_icon_black"];
- nameLabel.text = _messageModel.fileName ?: @"未知文件";
- sizeLabel.text = _messageModel.customFileSize ?: @"0KB";
-
- _currentContentView = _fileContentView;
- }
- - (void)setupVoiceContent {
- _voiceContentView.hidden = NO;
-
- // 获取子视图
- UIImageView *voiceIcon = _voiceContentView.subviews[0];
- UILabel *durationLabel = _voiceContentView.subviews[1];
- voiceIcon.animationDuration = 1.f;
- voiceIcon.animationRepeatCount = 0;
-
- // 设置语音内容
- voiceIcon.image = [UIImage imageNamed:_messageModel.isSender ? @"yuyinR" : @"yuyinL"];
- durationLabel.text = [NSString stringWithFormat:@"%ld\"", (long)_messageModel.voiceDuration];
-
- // 根据发送方调整布局
- if (_messageModel.isSender) {
- [voiceIcon mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.right.equalTo(_voiceContentView).offset(-8);
- make.centerY.equalTo(_voiceContentView);
- make.width.height.mas_equalTo(20);
- }];
-
- [durationLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.right.equalTo(voiceIcon.mas_left).offset(-8);
- make.centerY.equalTo(_voiceContentView);
- }];
-
- voiceIcon.animationImages = @[kImageMake(@"audio_sender_animate1"),kImageMake(@"audio_sender_animate2"),kImageMake(@"audio_sender_animate3")];
-
- } else {
- [voiceIcon mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(_voiceContentView).offset(8);
- make.centerY.equalTo(_voiceContentView);
- make.width.height.mas_equalTo(20);
- }];
-
- [durationLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(voiceIcon.mas_right).offset(8);
- make.centerY.equalTo(_voiceContentView);
- }];
- voiceIcon.animationImages = @[kImageMake(@"audio_receiver_animate1"),kImageMake(@"audio_receiver_animate2"),kImageMake(@"audio_receiver_animate3")];
- }
-
- _currentContentView = _voiceContentView;
- }
- - (void)setupCallContent {
- _callContentView.hidden = NO;
-
- // 获取子视图
- UIImageView *callIcon = _callContentView.subviews[0];
- UILabel *callLabel = _callContentView.subviews[1];
- UILabel *durationLabel = _callContentView.subviews[2];
-
- // 设置通话记录内容
- callIcon.image = [UIImage imageNamed:_messageModel.isVideo ? @"vedio_call_icon" : @"audio_call_icon"];
- callLabel.text = _messageModel.isCallSuccess ? @"通话时长" : @"未接通";
- durationLabel.text = _messageModel.isCallSuccess ? [self formatTimeWithMilliseconds:_messageModel.callDuration] : @"";
-
- _currentContentView = _callContentView;
- }
- - (void)setupHistoryContent {
- _historyContentView.hidden = NO;
-
- // 获取子视图
- UILabel *titleLabel = _historyContentView.subviews[0];
- UILabel *contentLabel = (UILabel *)_historyContentView.subviews[1];
-
- // 设置聊天记录内容
- titleLabel.text = @"聊天记录";
-
- NSArray * msgArray = _messageModel.forwardMsgArray;
- NSMutableArray * contentArray = [NSMutableArray array];
-
- for (NSDictionary * dict in msgArray) {
- NSString * messageType = dict[@"messageType"];
- NSString * nickName = dict[@"fromName"]?:@"";
-
- NSString * recordString = @"";
- switch ([messageType integerValue]) {
- case 0:
- // recordString = [NSString stringWithFormat:@"%@:%@\n",nickName,dict[@"content"]?:@""];
- recordString = [NSString stringWithFormat:@"%@:%@\n",nickName,[CryptoAES.shareInstance decryptDataL:dict[@"content"]]];
- break;
- case 1:
- recordString = [NSString stringWithFormat:@"%@:【图片】\n",nickName];;
- break;
- case 2:
- recordString = [NSString stringWithFormat:@"%@:【文件】\n",nickName];
- break;
- case 3:
- recordString = [NSString stringWithFormat:@"%@:【语音】\n",nickName];
- break;
- case 5:
- recordString = [NSString stringWithFormat:@"%@:【视频】\n",nickName];
- break;
- case 6:
- recordString = [NSString stringWithFormat:@"%@:【转发】\n",nickName];
- break;
- case 7:
- recordString = [NSString stringWithFormat:@"%@:【通话】\n",nickName];
- break;
- default:
- break;
- }
- [contentArray addObject:recordString];
-
- if (msgArray.lastObject == dict) {
- NSString * contentString = [contentArray componentsJoinedByString:@""];
- NSMutableAttributedString *content = [[NSMutableAttributedString alloc] initWithString:contentString ?: @""];
- contentLabel.attributedText = content;
- }
-
- }
-
- _currentContentView = _historyContentView;
- }
- - (void)setupEventContent {
- _eventLbl.hidden = NO;
- _eventLbl.text = _messageModel.content;
-
- [_eventLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.height.mas_equalTo(24);
- make.centerX.mas_equalTo(self.contentView.mas_centerX);
- make.centerY.mas_equalTo(self.contentView.mas_centerY);
- }];
- [_eventLbl sizeToFit];
- _currentContentView = _eventLbl;
- }
- - (void)setupCallbackContent {
- _callbackLbl.hidden = NO;
- _callbackLbl.text = _messageModel.isSender ? @"你撤回了一条消息":[NSString stringWithFormat:@"%@撤回了一条消息",_messageModel.nickName];
- [_callbackLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.height.mas_equalTo(24);
- make.centerX.mas_equalTo(self.contentView.mas_centerX);
- make.centerY.mas_equalTo(self.contentView.mas_centerY);
- }];
- _currentContentView = _callbackLbl;
- }
- - (void)setupDeleateContent {
- _deleateLbl.hidden = NO;
- _deleateLbl.text = @"你删除了一条消息!";
- [_deleateLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.height.mas_equalTo(24);
- make.centerX.mas_equalTo(self.contentView.mas_centerX);
- make.centerY.mas_equalTo(self.contentView.mas_centerY);
- }];
- _currentContentView = _deleateLbl;
- }
- #pragma mark - 布局方法
- - (void)layoutContentViews {
- if (_messageModel.messageType == ChatMessageTypeEvent || _messageModel.messageType == ChatMessageTypeCallBack || _messageModel.messageType == ChatMessageTypeCallBack2) {
- return;
- }
-
- //复选框布局
- // self.batchStateBtn.frame=CGRectMake(20, 10, 32, 32);
-
- [self.batchStateBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.size.mas_equalTo(CGSizeMake(32, 32));
- make.left.mas_equalTo(20);
- make.top.mas_equalTo(10);
- }];
-
- // 头像布局
- if (_messageModel.isSender) {
- [_avatarImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.right.equalTo(self.contentView).offset(-kAvatarMargin);
- make.top.equalTo(self.contentView).offset(kVerticalMargin);
- make.width.height.mas_equalTo(kAvatarSize);
- }];
-
- [_nameLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.right.equalTo(_avatarImageView.mas_left).offset(-4);
- make.top.equalTo(_avatarImageView);
- make.left.greaterThanOrEqualTo(self.contentView).offset(10);
- }];
-
- [_timeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.height.mas_equalTo(14);
- make.right.mas_equalTo(self.nameLabel.mas_left).offset(-12);
- make.top.mas_equalTo(self.avatarImageView);
- }];
-
- if (_currentContentView) {
- [_readStateLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.left.mas_equalTo(_currentContentView.mas_right).offset(6);
- make.height.mas_equalTo(14);
- make.bottom.mas_equalTo(_currentContentView.mas_bottom);
- }];
- }
- } else {
- [_avatarImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(self.contentView).offset(kAvatarMargin);
- make.top.equalTo(self.contentView).offset(kVerticalMargin);
- make.width.height.mas_equalTo(kAvatarSize);
- }];
-
- [_nameLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(_avatarImageView.mas_right).offset(4);
- make.top.equalTo(_avatarImageView);
- make.right.lessThanOrEqualTo(self.contentView).offset(-10);
- }];
-
- [_timeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.height.mas_equalTo(14);
- make.left.mas_equalTo(self.nameLabel.mas_right).offset(12);
- make.top.mas_equalTo(self.avatarImageView);
- }];
- }
-
- // 内容视图布局
- [self layoutCurrentContentView];
-
- // 气泡背景布局
- if (!_bubbleImageView.hidden) {
- NSAssert(_currentContentView, @"currentContentView 不能为 nil");
- NSAssert(_currentContentView.superview, @"currentContentView 必须已添加到父视图");
- [_bubbleImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.edges.equalTo(_currentContentView).insets(UIEdgeInsetsMake(-kTextVerticalMargin, -kTextHorizontalMargin, -kTextVerticalMargin, -kTextHorizontalMargin));
- }];
- [self.contentView sendSubviewToBack:_bubbleImageView];
- }
-
- }
- - (void)layoutCurrentContentView {
- switch (_messageModel.messageType) {
- case ChatMessageTypeText:
- [self layoutTextContent];
- break;
- case ChatMessageTypeImage:
- [self layoutImageContent];
- break;
- case ChatMessageTypeVideo:
- [self layoutVideoContent];
- break;
- case ChatMessageTypeVoice:
- [self layoutVioceContent];
- break;
- case ChatMessageTypeCall:
- [self layoutCallContent];
- break;
- case ChatMessageTypeHistory:
- [self layoutHistoryContent];
- break;
- default:
- [self layoutOtherContent];
- break;
- }
- }
- - (void)layoutTextContent {
- CGSize textSize = [self calculateTextSize];
-
- if (_messageModel.isSender) {
- [_textLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.right.equalTo(_avatarImageView.mas_left).offset(-26);
- make.top.mas_equalTo(_avatarImageView.mas_top).offset(28);
- make.width.mas_equalTo(textSize.width);
- make.height.mas_equalTo(textSize.height);
- make.bottom.equalTo(self.contentView).offset(-kVerticalMargin).priorityHigh();
- }];
-
- if (_messageModel.quoteMessage) {
- [_quoteLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.height.mas_equalTo(20);
- make.top.mas_equalTo(_textLbl.mas_bottom).offset(20);
- make.right.mas_equalTo(_textLbl.mas_right).offset(6);
- make.width.mas_lessThanOrEqualTo(kContentMaxWidth);
- }];
- }
- } else {
- [_textLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(_avatarImageView.mas_right).offset(26);
- make.top.mas_equalTo(_avatarImageView.mas_top).offset(28);
- make.width.mas_equalTo(textSize.width);
- make.height.mas_equalTo(textSize.height);
- make.bottom.equalTo(self.contentView).offset(-kVerticalMargin).priorityHigh();
- }];
-
- if (_messageModel.quoteMessage) {
- [_quoteLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.height.mas_equalTo(20);
- make.top.mas_equalTo(_textLbl.mas_bottom).offset(20);
- make.left.mas_equalTo(_textLbl.mas_left).offset(-6);
- make.width.mas_lessThanOrEqualTo(kContentMaxWidth);
- }];
- }
- }
- }
- - (void)layoutImageContent {
- // CGSize imageSize = [self adjustMediaSize:_messageModel.mediaSize];
- CGSize imageSize = _messageModel.mediaSize;
-
- if (_messageModel.isSender) {
- [_imageContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.right.equalTo(_avatarImageView.mas_left).offset(-16);
- make.top.mas_equalTo(_avatarImageView.mas_top).offset(28);
- make.width.mas_equalTo(imageSize.width);
- make.height.mas_equalTo(imageSize.height);
- make.bottom.equalTo(self.contentView).offset(-kVerticalMargin).priorityHigh();
- }];
- } else {
- [_imageContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(_avatarImageView.mas_right).offset(16);
- make.top.mas_equalTo(_avatarImageView.mas_top).offset(28);
- make.width.mas_equalTo(imageSize.width);
- make.height.mas_equalTo(imageSize.height);
- make.bottom.equalTo(self.contentView).offset(-kVerticalMargin).priorityHigh();
- }];
- }
- }
- - (void)layoutVideoContent {
- CGSize videoSize = [self adjustMediaSize:_messageModel.mediaSize];
-
- if (_messageModel.isSender) {
- [_videoContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.right.equalTo(_avatarImageView.mas_left).offset(-16);
- make.top.mas_equalTo(_avatarImageView.mas_top).offset(28);
- make.width.mas_equalTo(videoSize.width);
- make.height.mas_equalTo(videoSize.height);
- make.bottom.equalTo(self.contentView).offset(-kVerticalMargin).priorityHigh();
- }];
- } else {
- [_videoContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(_avatarImageView.mas_right).offset(16);
- make.top.mas_equalTo(_avatarImageView.mas_top).offset(28);
- make.width.mas_equalTo(videoSize.width);
- make.height.mas_equalTo(videoSize.height);
- make.bottom.equalTo(self.contentView).offset(-kVerticalMargin).priorityHigh();
- }];
- }
- }
- - (void)layoutVioceContent {
- // 设置内容视图约束
- [_voiceContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
- if (_messageModel.isSender) {
- make.right.equalTo(_avatarImageView.mas_left).offset(-26);
- } else {
- make.left.equalTo(_avatarImageView.mas_right).offset(26);
- }
- make.top.equalTo(_avatarImageView).offset(28);
- make.width.mas_equalTo(_messageModel.voiceWidth);
- make.bottom.equalTo(self.contentView).offset(-kVerticalMargin).priorityHigh();
- }];
- }
- - (void)layoutCallContent {
- // 获取子视图
- UIImageView *callIcon = _callContentView.subviews[0];
- UILabel *callLabel = _callContentView.subviews[1];
- UILabel *durationLabel = _callContentView.subviews[2];
-
- if (_messageModel.isSender) {
- [_callContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.right.equalTo(_avatarImageView.mas_left).offset(-26);
- make.top.mas_equalTo(_avatarImageView.mas_top).offset(28);
- make.width.mas_lessThanOrEqualTo(kContentMaxWidth);
- make.bottom.equalTo(self.contentView).offset(-kVerticalMargin).priorityHigh();
- }];
- } else {
- [_callContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(_avatarImageView.mas_right).offset(26);
- make.top.mas_equalTo(_avatarImageView.mas_top).offset(28);
- make.width.mas_lessThanOrEqualTo(kContentMaxWidth);
- make.bottom.equalTo(self.contentView).offset(-kVerticalMargin).priorityHigh();
- }];
- }
-
- if (_messageModel.isCallSuccess) {
-
- [callLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(callIcon.mas_right).offset(8);
- make.top.equalTo(_callContentView).offset(8);
- }];
-
- [durationLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(callLabel.mas_right).offset(8);
- make.top.equalTo(callLabel);
- make.right.equalTo(_callContentView).offset(-8);
- make.bottom.equalTo(_callContentView).offset(-8);
- }];
- }else{
-
- [callLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(callIcon.mas_right).offset(8);
- make.top.equalTo(_callContentView).offset(8);
- make.width.mas_lessThanOrEqualTo(50);
- make.right.equalTo(_callContentView).offset(-8);
- }];
- }
- }
- - (void)layoutHistoryContent {
- [_historyContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
- if (_messageModel.isSender) {
- make.right.equalTo(_avatarImageView.mas_left).offset(-26);
- } else {
- make.left.equalTo(_avatarImageView.mas_right).offset(26);
- }
- make.top.equalTo(_avatarImageView).offset(28);
- make.width.mas_equalTo(160);
- make.bottom.equalTo(self.contentView).offset(-kVerticalMargin).priorityHigh();
- }];
- }
- - (void)layoutOtherContent {
- if (_messageModel.isSender) {
- [_currentContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.right.equalTo(_avatarImageView.mas_left).offset(-26);
- make.top.mas_equalTo(_avatarImageView.mas_top).offset(28);
- make.width.mas_lessThanOrEqualTo(kContentMaxWidth);
- make.bottom.equalTo(self.contentView).offset(-kVerticalMargin).priorityHigh();
- }];
- } else {
- [_currentContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(_avatarImageView.mas_right).offset(26);
- make.top.mas_equalTo(_avatarImageView.mas_top).offset(28);
- make.width.mas_lessThanOrEqualTo(kContentMaxWidth);
- make.bottom.equalTo(self.contentView).offset(-kVerticalMargin).priorityHigh();
- }];
- }
-
- }
- - (CGSize)adjustMediaSize:(CGSize)originalSize {
- CGFloat maxWidth = kContentMaxWidth;
- CGFloat maxHeight = 200.0f;
-
- if (originalSize.width <= 0 || originalSize.height <= 0) {
- return CGSizeMake(maxWidth, maxWidth * 0.6);
- }
-
- CGFloat ratio = originalSize.width / originalSize.height;
- CGFloat adjustedWidth = maxWidth;
- CGFloat adjustedHeight = adjustedWidth / ratio;
-
- if (adjustedHeight > maxHeight) {
- adjustedHeight = maxHeight;
- adjustedWidth = adjustedHeight * ratio;
- }
-
- return CGSizeMake(adjustedWidth, adjustedHeight);
- }
- + (CGSize)otherAdjustMediaSize:(CGSize)originalSize {
- CGFloat maxWidth = kContentMaxWidth;
- CGFloat maxHeight = 200.0f;
-
- if (originalSize.width <= 0 || originalSize.height <= 0) {
- return CGSizeMake(maxWidth, maxWidth * 0.6);
- }
-
- CGFloat ratio = originalSize.width / originalSize.height;
- CGFloat adjustedWidth = maxWidth;
- CGFloat adjustedHeight = adjustedWidth / ratio;
-
- if (adjustedHeight > maxHeight) {
- adjustedHeight = maxHeight;
- adjustedWidth = adjustedHeight * ratio;
- }
-
- return CGSizeMake(adjustedWidth, adjustedHeight);
- }
- - (CGSize)calculateTextSize{
-
- NSString *text = _messageModel.content ?: @"";
-
- UILabel *label = [[UILabel alloc] init];
- label.font = [UIFont systemFontOfSize:18.0]; // 设置字体大小
- label.numberOfLines = 0; // 设置为0表示不限制行数
- label.text = text; // 设置文本内容
- label.preferredMaxLayoutWidth = kContentMaxWidth; // 设置固定宽度
- CGSize size = [label sizeThatFits:CGSizeMake(kContentMaxWidth, CGFLOAT_MAX)];
-
- CGSize textSize = size;
- // NSLog(@"文本宽度: %f, 文本高度: %f", textSize.width, textSize.height);
- return textSize;
- }
- - (NSString *)formatTimeWithMilliseconds:(NSInteger)milliseconds{
- NSInteger seconds = milliseconds / 1000.0;
-
- if (seconds >= 60) {
- // 大于等于60秒时显示为分钟,保留一位小数
- NSInteger minutes = seconds / 60.0;
- NSInteger sc = seconds%60;
- return [NSString stringWithFormat:@"%ld分%ld秒", (long)minutes,(long)sc];
- } else {
- // 小于60秒时显示为秒,保留整数
- return [NSString stringWithFormat:@"%ld秒", seconds];
- }
- }
- #pragma mark 列表事件处理
- - (void)handleImageTap {
- if (_messageModel.messageType == ChatMessageTypeImage) {
- // 处理图片点击
- NSLog(@"点击了图片: %@", _messageModel.url);
- if(_messageModel.fileError==1){
- weakSelf(self);
- if(weakself.bubbleCellLongPressMenuBlock){
- weakself.bubbleCellLongPressMenuBlock(8);
- }
- return;
- }
- if (_messageModel.url.length == 0 && !_messageModel.isSender) {
- [MBProgressHUD showWithText:@"图片正在上传"];
- return;
- }
-
- [FilePreviewer.shared previewFileWithLocalPath:_messageModel.localurl remoteURL:getURL(_messageModel.url) fromViewController:self.parentViewController];
- }
- }
- - (void)handleVideoTap {
- if (_messageModel.messageType == ChatMessageTypeVideo) {
- // 处理视频点击
- NSLog(@"点击了视频: %@", _messageModel.url);
- if(_messageModel.fileError==1){
- weakSelf(self);
- if(weakself.bubbleCellLongPressMenuBlock){
- weakself.bubbleCellLongPressMenuBlock(8);
- }
- return;
- }
- if (_messageModel.url.length == 0 && !_messageModel.isSender) {
- [MBProgressHUD showWithText:@"视频正在上传"];
- return;
- }
-
- [FilePreviewer.shared previewFileWithLocalPath:self.messageModel.localurl
- remoteURL:getURL(self.messageModel.url)
- fromViewController:self.parentViewController];
- [_messageModel downloadFileIfNeed:^(NSInteger persent) {
-
- }];
- }
- }
- - (void)handleFileTap {
- if (_messageModel.messageType == ChatMessageTypeFile) {
- // 处理视频点击
- NSLog(@"点击了文件: %@", _messageModel.url);
- if(_messageModel.fileError==1){
- weakSelf(self);
- if(weakself.bubbleCellLongPressMenuBlock){
- weakself.bubbleCellLongPressMenuBlock(8);
- }
- return;
- }
- if (_messageModel.url.length == 0 && !_messageModel.isSender) {
- [MBProgressHUD showWithText:@"文件正在上传"];
- return;
- }
-
-
- UILabel *sizeLabel = _fileContentView.subviews[2];
- weakSelf(self)
- [_messageModel downloadFileIfNeed:^(NSInteger persent) {
- dispatch_async(dispatch_get_main_queue(), ^{
- sizeLabel.text = [NSString stringWithFormat:@"%ld",(long)persent];
- if(persent>=100){
- sizeLabel.text = weakself.messageModel.customFileSize;
- [FilePreviewer.shared previewFileWithLocalPath:weakself.messageModel.localurl
- remoteURL:getURL(weakself.messageModel.url)
- fromViewController:weakself.parentViewController];
- }
- });
- }];
- }
- }
- - (void)handleVoiceTap {
- if (_messageModel.messageType == ChatMessageTypeVoice) {
- // 处理语音点击
- NSLog(@"点击了语音: %@", _messageModel.url);
- [_messageModel downloadFileIfNeed:^(NSInteger persent) {
- dispatch_main_async_safe(^{
- if (persent >= 100) {
- [self playVoice];
- }
- })
- }];
- }
- }
- - (void)playVoice {
- UIImageView *voiceIcon = _voiceContentView.subviews[0];
- if (self.player) {
- AVPlayerItem *playerItem = self.player.currentItem;
- CMTime currentTime = playerItem.currentTime;
- CMTime duration = playerItem.duration;
- [voiceIcon stopAnimating];
- if (self.player.rate == 0.0) {
- if (CMTimeCompare(currentTime, duration) == 0 ||
- CMTimeCompare(currentTime, duration) == 1) {
- // 播放完毕
- NSLog(@"Playback finished");
- } else {
- // 暂停
- NSLog(@"Playback paused");
- [self.player play];
- return;
- }
- } else {
- // 正在播放
- NSLog(@"Playback in progress");
- [self.player pause];
- return;
- }
- }
-
-
- NSString *urlstr = _messageModel.url;
- NSURL *Url = [NSURL URLWithString:urlstr];
-
- NSLog(@"本地路径:%@",_messageModel.localurl);
- if (_messageModel.localurl && [[NSFileManager defaultManager] fileExistsAtPath:_messageModel.localurl]) {
- NSLog(@"本地路径:");
- urlstr=_messageModel.localurl;
- Url = [NSURL fileURLWithPath:urlstr];
- }
- else{
-
- }
- NSLog(@"播放: %@", Url);
-
- AVAudioSession *audioSession = [AVAudioSession sharedInstance];
- [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
- [audioSession setActive:YES error:nil];
- AVPlayerItem *pitem = [AVPlayerItem playerItemWithURL:Url];
-
- //AVPlayerItem *pitem = [[AVPlayerItem alloc] initWithURL:Url];
-
- _player = [AVPlayer playerWithPlayerItem:pitem];
- [[UIDevice currentDevice] setProximityMonitoringEnabled:YES]; //建议在播放之前设置yes,播放结束设置NO。这个功能是开启红外感应
- [_player play];
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(playerItemDidReachEnd:)
- name:AVPlayerItemDidPlayToEndTimeNotification
- object:pitem];
- [voiceIcon startAnimating];
- }
- - (void)playerItemDidReachEnd:(NSNotification *)notification {
- // 播放结束时的处理代码
- UIImageView *voiceIcon = _voiceContentView.subviews[0];
- [voiceIcon stopAnimating];
- NSLog(@"播放结束:%@",notification);
- [[UIDevice currentDevice] setProximityMonitoringEnabled:NO]; //建议在播放之前设置yes,播放结束设置NO。这个功能是开启红外感应
- [[NSNotificationCenter defaultCenter] removeObserver:self name:notification.name object:notification.object];
- //可以执行继续播放下一条操作
-
- }
- - (void)handleHistoryTap {
- ChatRecordController * recordVc = [[ChatRecordController alloc] init];
- for (NSDictionary * dict in _messageModel.forwardMsgArray) {
- [recordVc.dataArray addObject:[ChatMessageModel modelWithDictionary:dict]];
- }
- UINavigationController * navi = [[UINavigationController alloc] initWithRootViewController:recordVc];
- navi.modalPresentationStyle = UIModalPresentationFullScreen;
- [self.parentViewController presentViewController:navi animated:YES completion:nil];
- }
- - (void)handleQuoteTap{
- NSLog(@"handleQuoteTap----:%@",[_messageModel.quoteMessage class]);
- if(![_messageModel.quoteMessage isKindOfClass:[ChatMessageModel class]]){
- return;
- }
- if (_messageModel.quoteMessage.messageType == ChatMessageTypeText) {
- QuoteDetailController * quoteDetailVc = [[QuoteDetailController alloc] init];
- quoteDetailVc.quotedContent = _messageModel.quoteMessage.content;
- quoteDetailVc.quotedSenderName = _messageModel.quoteMessage.nickName;
-
- UINavigationController * navi = [[UINavigationController alloc] initWithRootViewController:quoteDetailVc];
- navi.modalPresentationStyle = UIModalPresentationFullScreen;
- [self.parentViewController presentViewController:navi animated:YES completion:nil];
- }else if (_messageModel.quoteMessage.messageType == ChatMessageTypeImage) {
- [FilePreviewer.shared previewFileWithLocalPath:_messageModel.quoteMessage.url remoteURL:getURL(_messageModel.quoteMessage.url) fromViewController:self.parentViewController];
- }else if (_messageModel.quoteMessage.messageType == ChatMessageTypeVoice) {
- [FilePreviewer.shared previewFileWithLocalPath:_messageModel.quoteMessage.url remoteURL:getURL(_messageModel.quoteMessage.url) fromViewController:self.parentViewController];
- }else if (_messageModel.quoteMessage.messageType == ChatMessageTypeFile) {
- [FilePreviewer.shared previewFileWithLocalPath:_messageModel.quoteMessage.url remoteURL:getURL(_messageModel.quoteMessage.url) fromViewController:self.parentViewController];
- }else if (_messageModel.quoteMessage.messageType == ChatMessageTypeVoice) {
-
- }
- }
- - (void)handleAvatarTap {
- !self.avatarTapBlock?:self.avatarTapBlock(_messageModel);
- }
- #pragma mark - 高度计算
- + (CGFloat)cellHeightForMessage:(ChatMessageModel *)message {
- // 如果有缓存高度,直接返回
- if (message.cellHeight > 0) {
- return message.cellHeight;
- }
-
- CGFloat height = 0;
- CGFloat contentHeight = 0;
- CGFloat horizontalMargin = 12.0f;
- CGFloat verticalMargin = 10.0f;
- CGFloat maxWidth = kContentMaxWidth - 2 * horizontalMargin;
- // 计算内容高度
- switch (message.messageType) {
- case ChatMessageTypeText: {
- NSString *text = message.content ?: @"";
- UIFont *font = [UIFont systemFontOfSize:17]; // 设置字体大小
- CGSize maximumSize = CGSizeMake(maxWidth, CGFLOAT_MAX); // 设置最大宽度,高度设为CGFLOAT_MAX表示不限制高度
-
- NSDictionary *attributes = @{NSFontAttributeName: font};
- CGRect rect = [text boundingRectWithSize:maximumSize
- options:NSStringDrawingUsesLineFragmentOrigin
- attributes:attributes
- context:nil];
-
- CGFloat height = rect.size.height;
-
- contentHeight = height + 2 * verticalMargin;
- contentHeight = MAX(contentHeight, kMinBubbleHeight);
-
- if (message.quoteMessage) {
- contentHeight = contentHeight + 30;
- }
-
- break;
- }
- case ChatMessageTypeImage:
- case ChatMessageTypeVideo: {
- CGSize mediaSize = [self otherAdjustMediaSize:message.mediaSize];
- contentHeight = mediaSize.height;
- break;
- }
- case ChatMessageTypeFile:
- contentHeight = 60.0f;
- break;
- case ChatMessageTypeCallBack:
- contentHeight = 20.f;
- break;
-
- case ChatMessageTypeVoice:
- contentHeight = 30.0f;
- break;
- case ChatMessageTypeCall:
- contentHeight = 40.0f;
- break;
- case ChatMessageTypeHistory:
- contentHeight = 80.0f;
- break;
- case ChatMessageTypeEvent:
- contentHeight = 20.f;
- break;
- case ChatMessageTypeCallBack2:
- contentHeight = 20.f;
- break;
- case ChatMessageTypeDel:
- contentHeight = 20.f;
- break;
- }
-
- // 总高度 = 内容高度 + 上下边距
- height = contentHeight + kBubbleTopMargin+kBubbleBottomMargin;
-
- // 缓存高度
- message.cellHeight = height;
-
- return height;
- }
- + (void)precalculateHeightForMessage:(ChatMessageModel *)message completion:(void(^)(CGFloat height))completion {
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
- CGFloat height = [self cellHeightForMessage:message];
- dispatch_async(dispatch_get_main_queue(), ^{
- if (completion) completion(height);
- });
- });
- }
- @end
|