// // KBShareGoodsView.m // YouHuiProject // // Created by jcymac on 2018/5/18. // Copyright © 2018年 kuxuan. All rights reserved. // #import "KBShareGoodsView.h" #define FitYHShareGoodsWidth(X) (X) #define FitYHShareGoodsHeight(X) (X) //#define FitYHShareGoodsWidth(X) (X) //#define FitYHShareGoodsHeight(X) (X) #define QRWidth (110) @interface KBShareGoodsView(){ NSInteger count; } //主图 @property (strong, nonatomic) UIImageView *mainImageView; //商品标题 @property (strong, nonatomic) UILabel *titleLabel; //商品券后价 @property (strong, nonatomic) UILabel *ticketAfterPrice; //券钱 @property (strong, nonatomic) UILabel *ticketPrice; //券钱 //原始价格 @property (strong, nonatomic) UILabel *originalPrice; //二维码图片 @property (strong, nonatomic) UIImageView *QRcodeImageView; @property (strong, nonatomic) UIImageView *appImageView; @property (nonatomic, strong) UIImageView *userIcon; @property (nonatomic, strong) UILabel *nickName; @property (nonatomic, strong) UILabel *inviteCode; @property (nonatomic, strong) UILabel *commissionLabel; @end @implementation KBShareGoodsView -(instancetype)initWithFrame:(CGRect)frame{ if (self=[super initWithFrame:frame]) { } return self; } -(void)setModel:(KBShareGoodsModel *)model{ _model=model; [self addUI]; [self adjustUI]; [self otherOP]; } -(void)drawRect:(CGRect)rect{ [super drawRect:rect]; } -(void)addUI{ [self addSubview:self.titleLabel]; [self addSubview:self.mainImageView]; [self addSubview:self.ticketAfterPrice]; [self addSubview:self.originalPrice]; [self addSubview:self.ticketPrice]; [self addSubview:self.QRcodeImageView]; [self.QRcodeImageView addSubview:self.appImageView]; [self addSubview:self.commissionLabel]; [self addSubview:self.userIcon]; [self addSubview:self.nickName]; [self addSubview:self.inviteCode]; } -(void)adjustUI{ [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(FitYHShareGoodsWidth(FITSIZE(20))); make.top.mas_equalTo(FitYHShareGoodsHeight(46)); make.right.mas_equalTo(FitYHShareGoodsWidth(FITSIZE(-20))); }]; [self.mainImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(FitYHShareGoodsWidth(FITSIZE(20))); make.top.equalTo(self.titleLabel.mas_bottom).offset(FitYHShareGoodsHeight(42)); make.right.mas_equalTo(FitYHShareGoodsWidth(FITSIZE(-20))); make.height.mas_equalTo(FitYHShareGoodsWidth(self.width-2*FITSIZE(20))); }]; [self.ticketAfterPrice mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(FitYHShareGoodsWidth(FITSIZE(50))); make.top.equalTo(self.mainImageView.mas_bottom).offset(FitYHShareGoodsHeight(40)); }]; [self.originalPrice mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.ticketAfterPrice.mas_right).mas_offset(5); make.centerY.mas_equalTo(self.ticketAfterPrice.mas_centerY); // make.width.mas_equalTo(FITSIZE(74)); }]; [self.QRcodeImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.right.mas_equalTo(FitYHShareGoodsWidth(FITSIZE(-19))); make.top.equalTo(self.mainImageView.mas_bottom).offset(FitYHShareGoodsHeight(38)); make.width.height.mas_equalTo(FitYHShareGoodsWidth(FITSIZE(QRWidth))); }]; [self.appImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.QRcodeImageView.mas_left); make.top.equalTo(self.QRcodeImageView.mas_bottom).offset(FitYHShareGoodsHeight(12)); make.width.height.mas_equalTo(FitYHShareGoodsWidth(FITSIZE(18))); }]; [self.commissionLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.mas_equalTo(self.QRcodeImageView.mas_centerX); make.top.mas_equalTo(self.QRcodeImageView.mas_bottom).mas_offset(5); }]; [self.userIcon mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.ticketAfterPrice.mas_left); make.top.mas_equalTo(self.ticketAfterPrice.mas_bottom).mas_offset(Fitsize(10)); make.width.height.mas_equalTo(Fitsize(30)); }]; [self.nickName mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.userIcon.mas_right).mas_offset(5); make.centerY.mas_equalTo(self.userIcon.mas_centerY); make.width.mas_equalTo(Fitsize(100)); make.height.mas_equalTo(Fitsize(20)); }]; [self.inviteCode mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.userIcon.mas_left); make.top.mas_equalTo(self.userIcon.mas_bottom).mas_equalTo(Fitsize(10)); make.width.mas_equalTo(Fitsize(100)); make.height.mas_equalTo(Fitsize(20)); }]; } - (void)setUserInfo:(NSDictionary *)userInfo { _userInfo = userInfo; [self.userIcon sd_setImageWithURL:[NSURL URLWithString:userInfo[@"headimg"]] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) { count++; if (count > 1) { if (self.imgSuccBlock) { self.imgSuccBlock(); } } }]; self.nickName.text = userInfo[@"nickname"]; self.inviteCode.text= [NSString stringWithFormat:@"邀请码:%@",userInfo[@"invite_code"]]; } -(void)otherOP{ self.backgroundColor=[UIColor yhGrayColor]; [self.mainImageView sd_setImageWithURL:[NSURL URLWithString:self.model.mainImageUrl] placeholderImage:[UIImage imageNamed:@"share_goods_default_img"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) { count++; if (count > 1) { if (self.imgSuccBlock) { self.imgSuccBlock(); } } }]; } #pragma mark 懒加载 -(UILabel *)titleLabel{ if (!_titleLabel) { _titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(FitYHShareGoodsWidth(FITSIZE(20)), FitYHShareGoodsHeight(46), FitYHShareGoodsWidth(self.width-2*FITSIZE(20)), 0)]; NSTextAttachment *textAttach = [[NSTextAttachment alloc]init]; UIImage *img; if (self.model.shareGoodsFromType==YHShareGoodsFromTypeTianMao) { img= [UIImage imageNamed:@"share_title_tm"]; }else if(self.model.shareGoodsFromType==YHShareGoodsFromTypeTaoBao){ img= [UIImage imageNamed:@"share_title_tb"]; } if (img) { textAttach.image = img; } NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@" %@",self.model.title]]; NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:textAttach]; [attri insertAttributedString:string atIndex:0]; _titleLabel.attributedText = attri; // _titleLabel.text=@"虹小型电风扇迷你手持电扇USB学生宿舍 "; _titleLabel.numberOfLines=2; [_titleLabel sizeToFit]; } return _titleLabel; } -(UIImageView *)mainImageView{ if (!_mainImageView) { _mainImageView=[[UIImageView alloc]initWithFrame:CGRectMake(FitYHShareGoodsWidth(FITSIZE(20)), self.titleLabel.bottom+FitYHShareGoodsHeight(42), FitYHShareGoodsWidth(self.width-2*FITSIZE(20)), FitYHShareGoodsWidth((self.width-2*FITSIZE(20))))]; } return _mainImageView; } -(UILabel *)ticketAfterPrice{ if (!_ticketAfterPrice) { _ticketAfterPrice=[[UILabel alloc]initWithFrame:CGRectMake(FitYHShareGoodsWidth(FITSIZE(15)), self.mainImageView.bottom+FitYHShareGoodsHeight(40), 0, 0)]; if (self.model.shareGoodsPurchaseType==YHShareGoodsPurchaseTypeCoupons) { _ticketAfterPrice.text=[NSString stringWithFormat:@"券后价¥%@",self.model.ticketAfterPrice]; }else{ _ticketAfterPrice.text=[NSString stringWithFormat:@"折后价¥%@",self.model.ticketAfterPrice]; } _ticketAfterPrice.font=[UIFont systemFontOfSize:15.0f]; _ticketAfterPrice.textColor=[UIColor YHColorWithHex:0xEE1515]; [_ticketAfterPrice sizeToFit]; } return _ticketAfterPrice; } -(UILabel *)originalPrice{ if (!_originalPrice) { _originalPrice=[[UILabel alloc]initWithFrame:CGRectMake(self.ticketAfterPrice.right+5, self.ticketAfterPrice.y, 0, 0)]; // _originalPrice.centerY = self.ticketAfterPrice.centerY; NSString *price=[NSString stringWithFormat:@"¥%@",self.model.originalPrice]; NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:price]; [attri addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(0, price.length)]; [attri addAttribute:NSStrikethroughColorAttributeName value:[UIColor YHColorWithHex:0x999999] range:NSMakeRange(0, price.length)]; [_originalPrice setAttributedText:attri]; _originalPrice.textColor=[UIColor YHColorWithHex:0x999999]; _originalPrice.font=[UIFont systemFontOfSize:13.0f]; [_originalPrice sizeToFit]; } return _originalPrice; } -(UIImageView *)QRcodeImageView{ if (!_QRcodeImageView) { _QRcodeImageView=[[UIImageView alloc]initWithFrame:CGRectMake(FitYHShareGoodsWidth(self.width-FITSIZE(19)-FITSIZE(QRWidth)), self.mainImageView.bottom+FitYHShareGoodsHeight(38), FitYHShareGoodsWidth(FITSIZE(QRWidth)), FitYHShareGoodsWidth(FITSIZE(QRWidth)))]; //1. 实例化二维码滤镜 CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"]; // 2. 恢复滤镜的默认属性 [filter setDefaults]; // 3. 将字符串转换成NSData NSString *urlStr =self.model.QRcodeImageUrl; NSData *data = [urlStr dataUsingEncoding:NSUTF8StringEncoding]; // 4. 通过KVO设置滤镜inputMessage数据 [filter setValue:data forKey:@"inputMessage"]; // 5. 获得滤镜输出的图像 CIImage *outputImage = [filter outputImage]; UIImage *img=[outputImage createNonInterpolatedUIImagewithSize:FITSIZE(QRWidth)]; [_QRcodeImageView setImage:img]; _QRcodeImageView.backgroundColor=[UIColor clearColor]; } return _QRcodeImageView; } -(UIImageView *)appImageView{ if (!_appImageView) { _appImageView=[[UIImageView alloc]initWithFrame:CGRectMake(self.QRcodeImageView.x+10, self.QRcodeImageView.bottom+FitYHShareGoodsHeight(12), FitYHShareGoodsWidth(FITSIZE(18)), FitYHShareGoodsWidth(FITSIZE(18)))]; _appImageView.center = CGPointMake(self.QRcodeImageView.width/2, self.QRcodeImageView.height/2); [_appImageView setImage:[UIImage imageNamed:@"share_appicon"]]; } return _appImageView; } - (UIImageView *)userIcon { if (!_userIcon) { _userIcon = [[UIImageView alloc] initWithFrame:CGRectMake(self.ticketAfterPrice.left, self.ticketAfterPrice.bottom+15, Fitsize(30), Fitsize(30))]; _userIcon.layer.cornerRadius = FITHeightSIZE(15); _userIcon.backgroundColor = [UIColor yhGrayColor]; _userIcon.layer.masksToBounds = YES; } return _userIcon; } - (UILabel *)nickName { if (!_nickName) { _nickName = [[UILabel alloc] initWithFrame:CGRectMake(self.userIcon.right+5, self.userIcon.y, Fitsize(100), Fitsize(20))]; _nickName.textColor = [UIColor YHColorWithHex:0x333333]; _nickName.font = [UIFont systemFontOfSize:Fitsize(14)]; _nickName.centerY = self.userIcon.centerY; // _nickName.text = self.model.nickname; } return _nickName; } - (UILabel *)inviteCode { if (!_inviteCode) { _inviteCode = [[UILabel alloc] initWithFrame:CGRectMake(self.userIcon.left, self.userIcon.bottom+15, Fitsize(150), Fitsize(20))]; _inviteCode.textColor = [UIColor YHColorWithHex:0x333333]; _inviteCode.font = [UIFont systemFontOfSize:Fitsize(14)]; // _inviteCode.text = [NSString stringWithFormat:@"邀请码:%@",self.model.inviteCode]; } return _inviteCode; } - (UILabel *)commissionLabel { if (!_commissionLabel) { _commissionLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.QRcodeImageView.bottom+8, Fitsize(100), Fitsize(15))]; _commissionLabel.textColor = [UIColor homeRedColor]; _commissionLabel.font = [UIFont boldSystemFontOfSize:Fitsize(12)]; _commissionLabel.text = [NSString stringWithFormat:@"扫我赚¥%@",self.model.commission_price]; _commissionLabel.textAlignment = NSTextAlignmentCenter; _commissionLabel.hidden = YES; _commissionLabel.centerX = self.QRcodeImageView.centerX; } return _commissionLabel; } @end