// // TFMineHeaderView.m // TFReader // // Created by 谢腾飞 on 2020/12/11. // Copyright © 2020 xtfei_2011@126.com. All rights reserved. // #import "TFMineHeaderView.h" #import "TFUserCenterModel.h" #import "AppDelegate.h" #define MenuAvatar_H 70 #define MenuNumLabel_H 30 #define MenuLabel_H 20 @interface TFMineHeaderView () @property (nonatomic ,strong) UIButton *userAvatar; @property (nonatomic ,strong) UILabel *userNickname; @property (nonatomic ,strong) UIImageView *vipView; @property (nonatomic ,strong) UILabel *userIDLabel; // 书币 @property (nonatomic ,strong) UILabel *goldNumLabel; @property (nonatomic ,strong) UILabel *goldLabel; // 书券 #if TF_Task_Mode @property (nonatomic ,strong) UILabel *taskNumLabel; @property (nonatomic ,strong) UILabel *taskLabel; #endif // 月票 @property (nonatomic ,strong) UILabel *voucherNumLabel; @property (nonatomic ,strong) UILabel *voucherLabel; @property (nonatomic ,strong) NSString *avatarString; @end @implementation TFMineHeaderView - (instancetype)init { if (self = [super init]) { self.backgroundColor = kWhiteColor; #if TF_Recharge_Mode self.frame = CGRectMake(0, 0, SCREEN_WIDTH, 3 * kMargin + PUB_NAVBAR_OFFSET + MenuAvatar_H + MenuNumLabel_H + MenuLabel_H + kHalfMargin); #else self.frame = CGRectMake(0, 0, SCREEN_WIDTH, 3 * kMargin + PUB_NAVBAR_OFFSET + MenuAvatar_H); #endif [self createSubViews]; } return self; } - (void)createSubViews { self.userAvatar = [UIButton buttonWithType:UIButtonTypeCustom]; [self.userAvatar.layer setCornerRadius:35]; [self.userAvatar setClipsToBounds:YES]; [self.userAvatar setBackgroundImage:HoldUserAvatar forState:UIControlStateNormal]; [self.userAvatar addTarget:self action:@selector(avatarSelected) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:self.userAvatar]; [self.userAvatar mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.mas_left).with.offset(kMargin); make.top.mas_equalTo(2 * kMargin + PUB_NAVBAR_OFFSET); make.width.mas_equalTo(MenuAvatar_H); make.height.mas_equalTo(MenuAvatar_H); }]; self.userNickname = [[UILabel alloc] init]; self.userNickname.font = kBoldFont20; self.userNickname.backgroundColor = kWhiteColor; self.userNickname.textColor = kBlackColor; self.userNickname.textAlignment = NSTextAlignmentLeft; self.userNickname.userInteractionEnabled = YES; if (TFUserInfoManager.isLogin) { self.userNickname.text = [TFUserInfoManager shareInstance].nickname; } else { self.userNickname.text = TFLocalizedString(@"点我登录"); self.userNickname.textColor = kBlackColor; } UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(avatarSelected)]; [self addSubview:self.userNickname]; [self.userNickname addGestureRecognizer:tap]; [self.userNickname mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.userAvatar.mas_right).with.offset(kHalfMargin); make.width.mas_equalTo(SCREEN_WIDTH - 100); make.top.mas_equalTo(self.userAvatar.mas_top); make.height.mas_equalTo(MenuAvatar_H); }]; self.userIDLabel = [[UILabel alloc] init]; self.userIDLabel.backgroundColor = kWhiteColor; self.userIDLabel.textColor = kGrayTextColor; self.userIDLabel.textAlignment = NSTextAlignmentLeft; self.userIDLabel.font = kFont12; [self addSubview:self.userIDLabel]; [self.userIDLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.userAvatar.mas_right).with.offset(kHalfMargin); make.top.mas_equalTo(self.userAvatar.mas_centerY).with.offset(kQuarterMargin + 2); make.width.mas_equalTo(CGFLOAT_MIN); make.height.mas_equalTo(15); }]; #if TF_Super_Member_Mode self.vipView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"public_vip_normal"]]; self.vipView.hidden = YES; [self addSubview:self.vipView]; [self.vipView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.userIDLabel.mas_right).with.offset(kQuarterMargin); make.centerY.mas_equalTo(self.userIDLabel.mas_centerY); make.height.mas_equalTo(10); make.width.mas_equalTo(kGeometricWidth(10, 138, 48)); }]; #endif #if TF_Recharge_Mode self.goldNumLabel = [[UILabel alloc] init]; self.goldNumLabel.text = @"--"; self.goldNumLabel.textColor = kMainColor; self.goldNumLabel.textAlignment = NSTextAlignmentCenter; self.goldNumLabel.backgroundColor = kWhiteColor; self.goldNumLabel.font = kMainFont; self.goldNumLabel.userInteractionEnabled = YES; [self addSubview:self.goldNumLabel]; [self.goldNumLabel addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(goldSelected)]]; { UIView *line = [[UIView alloc] init]; line.backgroundColor = kGrayLineColor; [self addSubview:line]; [line mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.goldNumLabel.mas_right); make.top.mas_equalTo(self.goldNumLabel.mas_top).with.offset(5); make.width.mas_equalTo(kCellLineHeight); make.height.mas_equalTo(40); }]; } #if TF_Task_Mode self.taskNumLabel = [[UILabel alloc] init]; self.taskNumLabel.text = @"--"; self.taskNumLabel.textColor = kMainColor; self.taskNumLabel.textAlignment = NSTextAlignmentCenter; self.taskNumLabel.backgroundColor = kWhiteColor; self.taskNumLabel.font = kMainFont; self.taskNumLabel.userInteractionEnabled = YES; [self addSubview:self.taskNumLabel]; [self.taskNumLabel addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(taskSelected)]]; { UIView *line = [[UIView alloc] init]; line.backgroundColor = kGrayLineColor; [self addSubview:line]; [line mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.taskNumLabel.mas_right); make.top.mas_equalTo(self.taskNumLabel.mas_top).with.offset(5); make.width.mas_equalTo(kCellLineHeight); make.height.mas_equalTo(40); }]; } #endif self.voucherNumLabel = [[UILabel alloc] init]; self.voucherNumLabel.text = @"--"; self.voucherNumLabel.textColor = kMainColor; self.voucherNumLabel.textAlignment = NSTextAlignmentCenter; self.voucherNumLabel.backgroundColor = kWhiteColor; self.voucherNumLabel.font = kMainFont; self.voucherNumLabel.userInteractionEnabled = YES; [self addSubview:self.voucherNumLabel]; [self.voucherNumLabel addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(voucherSelected)]]; AppDelegate *delegate = (AppDelegate *)kRCodeSync([UIApplication sharedApplication].delegate); NSMutableArray *labelNumConstraints = [NSMutableArray arrayWithObjects:self.goldNumLabel, #if TF_Task_Mode self.taskNumLabel, #endif nil]; if (delegate.checkSettingModel.system_setting.monthly_ticket_switch == 1) { [labelNumConstraints addObject:self.voucherNumLabel]; } [labelNumConstraints mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:0 leadSpacing:0 tailSpacing:0]; [labelNumConstraints mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self.userAvatar.mas_bottom).with.offset(kMargin); make.height.mas_equalTo(MenuNumLabel_H); }]; self.goldLabel = [[UILabel alloc] init]; self.goldLabel.text = [NSString stringWithFormat:@"%@", TFSystemInfoManager.masterUnit]; self.goldLabel.textColor = kGrayTextColor; self.goldLabel.textAlignment = NSTextAlignmentCenter; self.goldLabel.backgroundColor = kWhiteColor; self.goldLabel.font = kFont12; self.goldLabel.userInteractionEnabled = YES; [self addSubview:self.goldLabel]; [self.goldLabel addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(goldSelected)]]; #if TF_Task_Mode self.taskLabel = [[UILabel alloc] init]; self.taskLabel.text = TFLocalizedString(@"书券"); self.taskLabel.textColor = kGrayTextColor; self.taskLabel.textAlignment = NSTextAlignmentCenter; self.taskLabel.backgroundColor = kWhiteColor; self.taskLabel.font = kFont12; self.taskLabel.userInteractionEnabled = YES; [self addSubview:self.taskLabel]; [self.taskLabel addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(taskSelected)]]; #endif self.voucherLabel = [[UILabel alloc] init]; self.voucherLabel.text = TFLocalizedString(@"月票"); self.voucherLabel.textColor = kGrayTextColor; self.voucherLabel.textAlignment = NSTextAlignmentCenter; self.voucherLabel.backgroundColor = kWhiteColor; self.voucherLabel.font = kFont12; self.voucherLabel.userInteractionEnabled = YES; [self addSubview:self.voucherLabel]; [self.voucherLabel addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(voucherSelected)]]; NSMutableArray *labelConstraints = [NSMutableArray arrayWithObjects:self.goldLabel, #if TF_Task_Mode self.taskLabel, #endif nil]; if (delegate.checkSettingModel.system_setting.monthly_ticket_switch == 1) { [labelConstraints addObject:self.voucherLabel]; } [labelConstraints mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:0 leadSpacing:0 tailSpacing:0]; [labelConstraints mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self.goldNumLabel.mas_bottom); make.height.mas_equalTo(MenuLabel_H); }]; #endif } #pragma mark - 点击事件 - (void)avatarSelected { if (self.portraitHandleBlock) { self.portraitHandleBlock(); } } - (void)goldSelected { if (self.goldSelectedBlock) { self.goldSelectedBlock(); } } - (void)taskSelected { if (self.taskSelectedBlock) { self.taskSelectedBlock(); } } - (void)voucherSelected { if (self.voucherSelectedBlock) { self.voucherSelectedBlock(); } } #pragma mark - 设置变量 - (void)setAvatarImage:(UIImage *)avatarImage { _avatarImage = avatarImage; [self.userAvatar setBackgroundImage:avatarImage forState:UIControlStateNormal]; } - (void)setNickName:(NSString *)nickName { _nickName = nickName; self.userNickname.text = nickName; } - (void)setUserModel:(TFUserCenterModel *)userModel { _userModel = userModel; #if TF_Super_Member_Mode // VIP if (userModel.isVip) { self.vipView.image = [UIImage imageNamed:@"public_vip_select"]; } else { self.vipView.image = [UIImage imageNamed:@"public_vip_normal"]; } #endif // 昵称 if (userModel.nickname.length > 0) { self.userNickname.text = userModel.nickname?:@""; self.userNickname.textColor = kBlackColor; self.userIDLabel.hidden = NO; self.vipView.hidden = NO; [self.userNickname mas_updateConstraints:^(MASConstraintMaker *make) { make.height.mas_equalTo(40); }]; } else { self.userNickname.text = TFLocalizedString(@"点我登录"); self.userNickname.textColor = kBlackColor; self.userIDLabel.hidden = YES; self.vipView.hidden = YES; [self.userNickname mas_updateConstraints:^(MASConstraintMaker *make) { make.height.mas_equalTo(MenuAvatar_H); }]; } // 用户id if (!userModel.uid) { self.userIDLabel.hidden = YES; self.userIDLabel.text = @""; } else { self.userIDLabel.hidden = NO; self.userIDLabel.text = [NSString stringWithFormat:@"ID:%@", [TFUtilsHelper formatStringWithInteger:userModel.uid]]; } [self.userIDLabel mas_updateConstraints:^(MASConstraintMaker *make) { make.width.mas_equalTo([TFViewHelper getDynamicWidthWithLabel:self.userIDLabel]); }]; // 主货币单位 if (userModel.masterUnit.length > 0) { self.goldLabel.text = [NSString stringWithFormat:@"%@", userModel.masterUnit]; } else { self.goldLabel.text = [NSString stringWithFormat:@"%@", TFSystemInfoManager.masterUnit]; } #if TF_Task_Mode // 子货币单位 if (userModel.subUnit.length > 0) { self.taskLabel.text = [NSString stringWithFormat:@"%@", userModel.subUnit]; } else { self.taskLabel.text = [NSString stringWithFormat:@"%@", TFSystemInfoManager.subUnit]; } #endif if (userModel.masterRemain <= 0 && !TFUserInfoManager.isLogin) { self.goldNumLabel.text = @"--"; } else { self.goldNumLabel.text = [TFUtilsHelper formatStringWithInteger:userModel.masterRemain]; } // 月票数量 if (userModel.ticketRemain <= 0 && !TFUserInfoManager.isLogin) { self.voucherNumLabel.text = @"--"; } else { self.voucherNumLabel.text = [TFUtilsHelper formatStringWithInteger:userModel.ticketRemain]; } #if TF_Task_Mode // 子货币数量 if (userModel.subRemain <= 0 && !TFUserInfoManager.isLogin) { self.taskNumLabel.text = @"--"; } else { self.taskNumLabel.text = [TFUtilsHelper formatStringWithInteger:userModel.subRemain]; } #endif if (![self.avatarString isEqualToString:userModel.avatar]) { self.avatarString = userModel.avatar; [self.userAvatar setBackgroundImageWithURL:[NSURL URLWithString:userModel.avatar?:@""] forState:UIControlStateNormal placeholder:HoldUserAvatar]; } } @end