// // YOUPAILCOneImageContentView.m // LiveChat // // Created by 张灿 on 2018/9/22. // Copyright © 2018年 caicai. All rights reserved. // #import "YOUPAILCOneImageContentView.h" #import "YOUPAILCOneImageAttachment.h" @interface YOUPAILCOneImageContentView() @property(nonatomic,strong)UIView* backView; @property(nonatomic,strong)UIImageView* imgView; @property(nonatomic,strong)UILabel* topTitle; @property(nonatomic,strong)UILabel* txtTitle; @property(nonatomic,assign)CGFloat currentY; @end @implementation YOUPAILCOneImageContentView - (instancetype)initSessionMessageContentView{ self = [super initSessionMessageContentView]; if (self) { self.opaque = YES; UIView* testView = [[UIView alloc] initWithFrame:CGRectZero]; testView.backgroundColor= [UIColor whiteColor]; testView.layer.cornerRadius = 6.0; UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapHandel:)]; [testView addGestureRecognizer:tap]; self.backView = testView; self.bubbleImageView.hidden = YES; [self addSubview:testView]; } return self; } - (void)refresh:(NIMMessageModel *)data{ [super refresh:data]; NIMCustomObject *customObject = (NIMCustomObject*)data.message.messageObject; id attachment = customObject.attachment; if ([attachment isKindOfClass:[YOUPAILCOneImageAttachment class]]) { for (UIView* subView in self.backView.subviews) { [subView removeFromSuperview]; } self.currentY = (KScreenWidth-30)*9/16; YOUPAILCOneImageAttachment* attachment = (YOUPAILCOneImageAttachment*)customObject.attachment; UIImageView* imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth-30, (KScreenWidth-30)*9/16)]; imgView.backgroundColor = LCGray; self.imgView = imgView; if (attachment.act_type==1) { [LCTools clipCorner:UIRectCornerTopLeft|UIRectCornerTopRight|UIRectCornerBottomLeft|UIRectCornerBottomRight View:imgView size:CGSizeMake(6, 6)]; if (attachment.act_string.length>0) { UILabel* logoLabel = [[UILabel alloc]init]; logoLabel.text = attachment.act_string; logoLabel.font = LCFont14; logoLabel.textColor = [UIColor whiteColor]; logoLabel.backgroundColor = HexColorFromRGBA(0x000000, 0.5); logoLabel.layer.cornerRadius = 10; logoLabel.textAlignment = NSTextAlignmentCenter; logoLabel.layer.masksToBounds = YES; CGFloat logoWidth =[attachment.act_string widthWithFont:LCFont14 constrainedToHeight:20]; logoLabel.frame = CGRectMake(KScreenWidth-30-logoWidth-20-20,20,logoWidth+20, 20); [imgView addSubview:logoLabel]; } }else{ [LCTools clipCorner:UIRectCornerTopLeft|UIRectCornerTopRight View:imgView size:CGSizeMake(6, 6)]; } imgView.layer.masksToBounds = YES; [self.backView addSubview:imgView]; [self.imgView sd_setImageWithURL:[NSURL URLWithString:attachment.image] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) { if (image) { self.imgView.image = image; } }]; if (attachment.act_type==2) { if (![attachment.title isEqualToString:@""]) { UILabel* txtOneLabel = [[UILabel alloc]init]; txtOneLabel.numberOfLines = 0; txtOneLabel.text = attachment.title; txtOneLabel.font = [UIFont boldSystemFontOfSize:16]; txtOneLabel.textColor = LCTextBlack; CGFloat txtOneHeight =[attachment.title heightWithFont:[UIFont boldSystemFontOfSize:16] padding:3.0 constrainedToWidth:KScreenWidth-70]; txtOneLabel.frame = CGRectMake(20, self.currentY+20, KScreenWidth-70, txtOneHeight+2); [UILabel changeLineSpaceForLabel:txtOneLabel WithSpace:3.0]; [self.backView addSubview:txtOneLabel]; self.currentY +=txtOneHeight+22; } if (![attachment.txt isEqualToString:@""]) { UILabel* txtTwoLabel = [[UILabel alloc]init]; txtTwoLabel.text = attachment.txt; txtTwoLabel.numberOfLines = 0; txtTwoLabel.font = [UIFont systemFontOfSize:15]; txtTwoLabel.textColor = LCTextNormal; CGFloat txtTwoHeight =[attachment.txt heightWithFont:[UIFont systemFontOfSize:15] padding:3.0 constrainedToWidth:KScreenWidth-70]; txtTwoLabel.frame = CGRectMake(20, self.currentY+10, KScreenWidth-70, txtTwoHeight+2); [UILabel changeLineSpaceForLabel:txtTwoLabel WithSpace:3.0]; [self.backView addSubview:txtTwoLabel]; self.currentY +=txtTwoHeight+32; } } } } - (void)tapHandel:(UITapGestureRecognizer*)tap{ if (tap.state == UIGestureRecognizerStateEnded) { NIMKitEvent *event = [[NIMKitEvent alloc] init]; event.eventName = NIMKitEventNameTapContent; event.messageModel = self.model; [self.delegate onCatchEvent:event]; } } - (void)onTouchUpInside:(id)sender { NIMKitEvent *event = [[NIMKitEvent alloc] init]; event.eventName = NIMKitEventNameTapContent; event.messageModel = self.model; [self.delegate onCatchEvent:event]; } - (void)layoutSubviews{ [super layoutSubviews]; UIEdgeInsets contentInsets = self.model.contentViewInsets; // CGFloat tableViewWidth = self.superview.nim_width; CGSize contentsize = [self.model contentSize:KScreenWidth]; CGRect imageViewFrame = CGRectMake(contentInsets.left, contentInsets.top, contentsize.width, contentsize.height); self.backView.frame = imageViewFrame; } @end