344 lines
13 KiB
Objective-C
344 lines
13 KiB
Objective-C
//
|
||
// KBMyHeaderView.m
|
||
// keyBoard
|
||
//
|
||
|
||
#import "KBMyHeaderView.h"
|
||
#import <Masonry/Masonry.h>
|
||
#import "UIColor+Extension.h"
|
||
#import "KBPersonInfoVC.h"
|
||
#import "KBMyKeyBoardVC.h"
|
||
#import "KBJfPay.h"
|
||
#import "KBVipPay.h"
|
||
#import "UIImageView+KBWebImage.h"
|
||
#import "KBUser.h"
|
||
|
||
@interface KBMyHeaderView ()
|
||
@property (nonatomic, strong) UILabel *titleLabel;
|
||
@property (nonatomic, strong) UIButton *keyboardBtn;
|
||
@property (nonatomic, strong) UIImageView *avatarView;
|
||
@property (nonatomic, strong) UILabel *nameLabel;
|
||
@property (nonatomic, strong) UIImageView *vipIconView;
|
||
@property (nonatomic, strong) UILabel *vipExpiryLabel;
|
||
@property (nonatomic, strong) UIImageView *cardLeft;
|
||
@property (nonatomic, strong) UIImageView *cardRight;
|
||
@property (nonatomic, strong) UIImageView *avatarEditIcon; // 头像右下角的编辑图标
|
||
@property (nonatomic, strong) KBUser *userModel;
|
||
//@property (nonatomic, strong) MASConstraint *vipIconWidthConstraint;
|
||
//@property (nonatomic, strong) MASConstraint *nameRightToVipConstraint;
|
||
//@property (nonatomic, strong) MASConstraint *nameRightToSuperviewConstraint;
|
||
|
||
@end
|
||
|
||
@implementation KBMyHeaderView
|
||
|
||
- (instancetype)initWithFrame:(CGRect)frame {
|
||
if (self = [super initWithFrame:frame]) {
|
||
self.backgroundColor = [UIColor clearColor]; // 透明,露出底部背景图
|
||
|
||
[self addSubview:self.titleLabel];
|
||
[self addSubview:self.keyboardBtn];
|
||
[self addSubview:self.avatarView];
|
||
[self addSubview:self.avatarEditIcon]; // 头像右下角编辑图标
|
||
[self addSubview:self.nameLabel];
|
||
[self addSubview:self.vipIconView];
|
||
[self addSubview:self.vipExpiryLabel];
|
||
[self addSubview:self.cardLeft];
|
||
[self addSubview:self.cardRight];
|
||
|
||
// 布局
|
||
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
make.left.equalTo(self).offset(26);
|
||
make.bottom.equalTo(self.avatarView.mas_top).offset(-35); // 顶部安全区下方
|
||
}];
|
||
[self.keyboardBtn mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
make.centerY.equalTo(self.titleLabel);
|
||
make.right.equalTo(self).offset(-20);
|
||
make.height.mas_equalTo(34);
|
||
make.width.mas_greaterThanOrEqualTo(90);
|
||
}];
|
||
[self.avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
make.left.equalTo(self).offset(26);
|
||
make.bottom.equalTo(self.cardLeft.mas_top).offset(-23);
|
||
make.width.height.mas_equalTo(70);
|
||
}];
|
||
[self.avatarEditIcon mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
make.width.height.mas_equalTo(18);
|
||
make.right.equalTo(self.avatarView.mas_right);
|
||
make.bottom.equalTo(self.avatarView.mas_bottom);
|
||
}];
|
||
[self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
make.left.equalTo(self.avatarView.mas_right).offset(10);
|
||
make.bottom.equalTo(self.avatarView.mas_centerY).offset(-4);
|
||
}];
|
||
|
||
[self.vipIconView mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
make.centerY.equalTo(self.nameLabel);
|
||
make.left.equalTo(self.nameLabel.mas_right).offset(10);
|
||
make.right.lessThanOrEqualTo(self).offset(-16);
|
||
make.width.mas_equalTo(51);
|
||
make.height.mas_equalTo(20);
|
||
}];
|
||
|
||
[self.vipExpiryLabel mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
make.top.equalTo(self.avatarView.mas_centerY).offset(4);
|
||
make.left.equalTo(self.nameLabel);
|
||
make.right.lessThanOrEqualTo(self).offset(-16);
|
||
}];
|
||
|
||
[self.nameLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultLow
|
||
forAxis:UILayoutConstraintAxisHorizontal];
|
||
[self.vipIconView setContentCompressionResistancePriority:UILayoutPriorityRequired
|
||
forAxis:UILayoutConstraintAxisHorizontal];
|
||
|
||
|
||
// 166 99
|
||
// w ?
|
||
CGFloat height = 99 * (KB_SCREEN_WIDTH - 2 * 16 - 10) * 0.5 / 166;
|
||
[self.cardLeft mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
make.left.equalTo(self).offset(16);
|
||
make.bottom.equalTo(self).offset(-30);
|
||
make.right.equalTo(self.mas_centerX).offset(-5);
|
||
make.height.mas_equalTo(height);
|
||
}];
|
||
[self.cardRight mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
make.right.equalTo(self).offset(-16);
|
||
make.bottom.equalTo(self).offset(-30);
|
||
make.left.equalTo(self.mas_centerX).offset(5);
|
||
make.height.equalTo(self.cardLeft);
|
||
}];
|
||
}
|
||
return self;
|
||
}
|
||
|
||
- (void)layoutSubviews {
|
||
[super layoutSubviews];
|
||
// 圆角
|
||
self.avatarView.layer.cornerRadius = self.avatarView.bounds.size.height * 0.5;
|
||
self.avatarView.layer.masksToBounds = YES;
|
||
}
|
||
|
||
- (void)configureWithUser:(KBUser *)user {
|
||
// 名称:优先昵称,兜底展示本地化的“Settings”或固定提示
|
||
self.userModel = user;
|
||
NSString *name = user.nickName;
|
||
if (name.length == 0) {
|
||
name = KBLocalized(@"Settings");
|
||
}
|
||
self.nameLabel.text = name;
|
||
|
||
// 头像:使用通用占位图
|
||
[self.avatarView kb_setAvatarURL:user.avatarUrl placeholder:KBPlaceholderImage];
|
||
|
||
BOOL isVip = user.isVip;
|
||
self.vipIconView.hidden = !isVip;
|
||
self.vipExpiryLabel.hidden = !isVip;
|
||
self.vipExpiryLabel.text = isVip ? [self vipExpiryDisplayTextFrom:user.vipExpiry] : nil;
|
||
}
|
||
|
||
- (NSString *)vipExpiryDisplayTextFrom:(NSString *)rawDate {
|
||
NSString *prefix = KBLocalized(@"Due On");
|
||
if (rawDate.length == 0) {
|
||
return prefix;
|
||
}
|
||
|
||
NSDateFormatter *input = [self.class vipExpiryInputFormatter];
|
||
NSDate *date = [input dateFromString:rawDate];
|
||
if (!date) {
|
||
return [NSString stringWithFormat:@"%@ %@", prefix, rawDate];
|
||
}
|
||
|
||
NSDateFormatter *output = [self.class vipExpiryOutputFormatter];
|
||
NSString *formatted = [output stringFromDate:date];
|
||
if (formatted.length == 0) {
|
||
formatted = rawDate;
|
||
}
|
||
return [NSString stringWithFormat:@"%@ %@", prefix, formatted];
|
||
}
|
||
|
||
+ (void)kb_applyGradientTo:(UIView *)view colors:(NSArray *)colors {
|
||
// 删除旧的渐变层,重复 setNeedsLayout 时避免叠加
|
||
NSMutableArray<CALayer *> *remove = [NSMutableArray array];
|
||
for (CALayer *l in view.layer.sublayers) {
|
||
if ([l isKindOfClass:[CAGradientLayer class]]) { [remove addObject:l]; }
|
||
}
|
||
for (CALayer *l in remove) { [l removeFromSuperlayer]; }
|
||
|
||
CAGradientLayer *g = [CAGradientLayer layer];
|
||
g.colors = colors;
|
||
g.startPoint = CGPointMake(0, 0.5);
|
||
g.endPoint = CGPointMake(1, 0.5);
|
||
g.frame = view.bounds;
|
||
[view.layer insertSublayer:g atIndex:0];
|
||
}
|
||
|
||
+ (NSDateFormatter *)vipExpiryInputFormatter {
|
||
static NSDateFormatter *formatter;
|
||
static dispatch_once_t onceToken;
|
||
dispatch_once(&onceToken, ^{
|
||
formatter = [NSDateFormatter new];
|
||
formatter.dateFormat = @"yyyy-MM-dd";
|
||
formatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
|
||
});
|
||
return formatter;
|
||
}
|
||
|
||
+ (NSDateFormatter *)vipExpiryOutputFormatter {
|
||
static NSDateFormatter *formatter;
|
||
static dispatch_once_t onceToken;
|
||
dispatch_once(&onceToken, ^{
|
||
formatter = [NSDateFormatter new];
|
||
formatter.dateStyle = NSDateFormatterLongStyle;
|
||
formatter.timeStyle = NSDateFormatterNoStyle;
|
||
});
|
||
return formatter;
|
||
}
|
||
|
||
#pragma mark - Lazy UI
|
||
- (UILabel *)titleLabel {
|
||
if (!_titleLabel) {
|
||
_titleLabel = [UILabel new];
|
||
_titleLabel.text = KBLocalized(@"Personal"); // 大标题
|
||
_titleLabel.font = [KBFont bold:30];
|
||
_titleLabel.textColor = [UIColor colorWithHex:0x1B1F1A];
|
||
}
|
||
return _titleLabel;
|
||
}
|
||
|
||
- (UIButton *)keyboardBtn {
|
||
if (!_keyboardBtn) {
|
||
_keyboardBtn = [UIButton buttonWithType:UIButtonTypeCustom];
|
||
[_keyboardBtn setTitle:KBLocalized(@"My Keyboard") forState:UIControlStateNormal];
|
||
_keyboardBtn.titleLabel.font = [KBFont medium:10];
|
||
[_keyboardBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
|
||
_keyboardBtn.backgroundColor = [UIColor colorWithHex:KBColorValue];
|
||
_keyboardBtn.layer.cornerRadius = 17;
|
||
_keyboardBtn.layer.masksToBounds = YES;
|
||
|
||
// 图标 + 文本,保持不同语言下左右内边距一致
|
||
UIImage *kbImg = [UIImage imageNamed:@"my_kb_icon"];
|
||
if (kbImg) {
|
||
[_keyboardBtn setImage:kbImg forState:UIControlStateNormal];
|
||
_keyboardBtn.imageView.contentMode = UIViewContentModeScaleAspectFit;
|
||
// 按钮左右内边距(与文字无关,始终生效)
|
||
CGFloat horizontalPadding = 10.0;
|
||
_keyboardBtn.contentEdgeInsets = UIEdgeInsetsMake(0, horizontalPadding, 0, horizontalPadding);
|
||
// 图片与文字之间的间距
|
||
CGFloat spacing = 6.0;
|
||
_keyboardBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -spacing / 2.0, 0, spacing / 2.0);
|
||
_keyboardBtn.titleEdgeInsets = UIEdgeInsetsMake(0, spacing / 2.0, 0, -spacing / 2.0);
|
||
|
||
// 保证宽度由内容决定,避免中英文长度不同导致被压扁
|
||
[_keyboardBtn setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
|
||
[_keyboardBtn setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
|
||
}
|
||
|
||
// 点击事件
|
||
[_keyboardBtn addTarget:self action:@selector(onKeyboardTap) forControlEvents:UIControlEventTouchUpInside];
|
||
}
|
||
return _keyboardBtn;
|
||
}
|
||
|
||
- (UIImageView *)avatarView {
|
||
if (!_avatarView) {
|
||
_avatarView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"lufei.jpg"]];
|
||
_avatarView.contentMode = UIViewContentModeScaleAspectFill;
|
||
_avatarView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0];
|
||
_avatarView.userInteractionEnabled = YES; // 头像可点击
|
||
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onAvatarTap)];
|
||
[_avatarView addGestureRecognizer:tap];
|
||
}
|
||
return _avatarView;
|
||
}
|
||
|
||
- (UILabel *)nameLabel {
|
||
if (!_nameLabel) {
|
||
_nameLabel = [UILabel new];
|
||
_nameLabel.text = @"Notice";
|
||
_nameLabel.font = [KBFont medium:20];
|
||
_nameLabel.textColor = [UIColor colorWithHex:KBBlackValue];
|
||
_nameLabel.lineBreakMode = NSLineBreakByTruncatingTail;
|
||
[_nameLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
|
||
[_nameLabel setContentHuggingPriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
|
||
}
|
||
return _nameLabel;
|
||
}
|
||
|
||
- (UIImageView *)vipIconView {
|
||
if (!_vipIconView) {
|
||
_vipIconView = [UIImageView new];
|
||
_vipIconView.contentMode = UIViewContentModeScaleAspectFit;
|
||
_vipIconView.image = [UIImage imageNamed:@"my_vip_icon"];
|
||
_vipIconView.hidden = YES;
|
||
[_vipIconView setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
|
||
[_vipIconView setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
|
||
}
|
||
return _vipIconView;
|
||
}
|
||
|
||
- (UILabel *)vipExpiryLabel {
|
||
if (!_vipExpiryLabel) {
|
||
_vipExpiryLabel = [UILabel new];
|
||
_vipExpiryLabel.font = [KBFont regular:12];
|
||
_vipExpiryLabel.textColor = [UIColor colorWithHex:0x9FA5B5];
|
||
_vipExpiryLabel.numberOfLines = 1;
|
||
_vipExpiryLabel.lineBreakMode = NSLineBreakByTruncatingTail;
|
||
_vipExpiryLabel.hidden = YES;
|
||
}
|
||
return _vipExpiryLabel;
|
||
}
|
||
|
||
- (UIImageView *)cardLeft {
|
||
if (!_cardLeft) {
|
||
_cardLeft = [UIImageView new];
|
||
_cardLeft.contentMode = UIViewContentModeScaleAspectFit;
|
||
_cardLeft.image = [UIImage imageNamed:@"my_member_icon"];
|
||
_cardLeft.userInteractionEnabled = YES;
|
||
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onLeftCardTap)];
|
||
[_cardLeft addGestureRecognizer:tap];
|
||
}
|
||
return _cardLeft;
|
||
}
|
||
|
||
- (UIImageView *)cardRight {
|
||
if (!_cardRight) {
|
||
_cardRight = [UIImageView new];
|
||
_cardRight.contentMode = UIViewContentModeScaleAspectFit;
|
||
_cardRight.image = [UIImage imageNamed:@"my_recharge_icon"];
|
||
_cardRight.userInteractionEnabled = YES;
|
||
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onRightCardTap)];
|
||
[_cardRight addGestureRecognizer:tap];
|
||
}
|
||
return _cardRight;
|
||
}
|
||
|
||
- (UIImageView *)avatarEditIcon {
|
||
if (!_avatarEditIcon) {
|
||
_avatarEditIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"my_head_edite"]];
|
||
_avatarEditIcon.contentMode = UIViewContentModeScaleAspectFit;
|
||
_avatarEditIcon.userInteractionEnabled = NO; // 仅展示
|
||
}
|
||
return _avatarEditIcon;
|
||
}
|
||
|
||
#pragma mark - Actions
|
||
- (void)onKeyboardTap {
|
||
KBMyKeyBoardVC *vc = [[KBMyKeyBoardVC alloc] init];
|
||
[KB_CURRENT_NAV pushViewController:vc animated:true];
|
||
}
|
||
- (void)onAvatarTap {
|
||
KBPersonInfoVC *vc = [[KBPersonInfoVC alloc] init];
|
||
// vc.userModel = self.userModel;
|
||
[KB_CURRENT_NAV pushViewController:vc animated:true];
|
||
}
|
||
- (void)onLeftCardTap {
|
||
KBVipPay *vc = [[KBVipPay alloc] init];
|
||
[KB_CURRENT_NAV pushViewController:vc animated:true];
|
||
}
|
||
- (void)onRightCardTap {
|
||
KBJfPay *vc = [[KBJfPay alloc] init];
|
||
[KB_CURRENT_NAV pushViewController:vc animated:true];
|
||
}
|
||
|
||
@end
|