Files
keyboard/keyBoard/Class/Me/V/KBMyHeaderView.m

344 lines
13 KiB
Mathematica
Raw Normal View History

2025-11-10 19:22:31 +08:00
//
// KBMyHeaderView.m
// keyBoard
//
#import "KBMyHeaderView.h"
#import <Masonry/Masonry.h>
#import "UIColor+Extension.h"
2025-11-11 14:56:57 +08:00
#import "KBPersonInfoVC.h"
2025-11-11 20:24:13 +08:00
#import "KBMyKeyBoardVC.h"
2025-11-17 18:51:06 +08:00
#import "KBJfPay.h"
#import "KBVipPay.h"
2025-12-03 20:14:14 +08:00
#import "UIImageView+KBWebImage.h"
#import "KBUser.h"
2025-11-10 19:22:31 +08:00
@interface KBMyHeaderView ()
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *keyboardBtn;
@property (nonatomic, strong) UIImageView *avatarView;
@property (nonatomic, strong) UILabel *nameLabel;
2025-12-11 22:02:18 +08:00
@property (nonatomic, strong) UIImageView *vipIconView;
@property (nonatomic, strong) UILabel *vipExpiryLabel;
2025-11-10 19:22:31 +08:00
@property (nonatomic, strong) UIImageView *cardLeft;
@property (nonatomic, strong) UIImageView *cardRight;
2025-11-10 19:51:23 +08:00
@property (nonatomic, strong) UIImageView *avatarEditIcon; //
2025-12-03 20:31:33 +08:00
@property (nonatomic, strong) KBUser *userModel;
2025-12-11 22:02:18 +08:00
//@property (nonatomic, strong) MASConstraint *vipIconWidthConstraint;
//@property (nonatomic, strong) MASConstraint *nameRightToVipConstraint;
//@property (nonatomic, strong) MASConstraint *nameRightToSuperviewConstraint;
2025-12-03 20:31:33 +08:00
2025-11-10 19:22:31 +08:00
@end
@implementation KBMyHeaderView
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
2025-11-10 19:55:50 +08:00
self.backgroundColor = [UIColor clearColor]; //
2025-11-10 19:22:31 +08:00
[self addSubview:self.titleLabel];
[self addSubview:self.keyboardBtn];
[self addSubview:self.avatarView];
2025-11-10 19:51:23 +08:00
[self addSubview:self.avatarEditIcon]; //
2025-11-10 19:22:31 +08:00
[self addSubview:self.nameLabel];
2025-12-11 22:02:18 +08:00
[self addSubview:self.vipIconView];
[self addSubview:self.vipExpiryLabel];
2025-11-10 19:22:31 +08:00
[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);
2025-11-25 16:10:08 +08:00
make.width.mas_greaterThanOrEqualTo(90);
2025-11-10 19:22:31 +08:00
}];
[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);
}];
2025-11-10 19:51:23 +08:00
[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);
}];
2025-11-10 19:22:31 +08:00
[self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
2025-12-12 14:16:48 +08:00
make.left.equalTo(self.avatarView.mas_right).offset(10);
make.bottom.equalTo(self.avatarView.mas_centerY).offset(-4);
}];
2025-12-11 22:02:18 +08:00
[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) {
2025-12-12 14:16:48 +08:00
make.top.equalTo(self.avatarView.mas_centerY).offset(4);
2025-12-11 22:02:18 +08:00
make.left.equalTo(self.nameLabel);
make.right.lessThanOrEqualTo(self).offset(-16);
}];
[self.nameLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultLow
forAxis:UILayoutConstraintAxisHorizontal];
[self.vipIconView setContentCompressionResistancePriority:UILayoutPriorityRequired
forAxis:UILayoutConstraintAxisHorizontal];
2025-11-10 19:22:31 +08:00
// 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;
}
2025-12-03 20:14:14 +08:00
- (void)configureWithUser:(KBUser *)user {
// Settings
2025-12-03 20:31:33 +08:00
self.userModel = user;
2025-12-03 20:14:14 +08:00
NSString *name = user.nickName;
if (name.length == 0) {
name = KBLocalized(@"Settings");
}
self.nameLabel.text = name;
// 使
[self.avatarView kb_setAvatarURL:user.avatarUrl placeholder:KBAvatarPlaceholderImage];
2025-12-11 22:02:18 +08:00
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];
2025-12-03 20:14:14 +08:00
}
2025-11-10 19:22:31 +08:00
+ (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];
}
2025-12-11 22:02:18 +08:00
+ (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;
}
2025-11-10 19:22:31 +08:00
#pragma mark - Lazy UI
- (UILabel *)titleLabel {
if (!_titleLabel) {
_titleLabel = [UILabel new];
2025-11-25 15:36:16 +08:00
_titleLabel.text = KBLocalized(@"Personal"); //
_titleLabel.font = [KBFont bold:30];
2025-11-10 19:22:31 +08:00
_titleLabel.textColor = [UIColor colorWithHex:0x1B1F1A];
}
return _titleLabel;
}
- (UIButton *)keyboardBtn {
if (!_keyboardBtn) {
_keyboardBtn = [UIButton buttonWithType:UIButtonTypeCustom];
2025-11-17 21:35:25 +08:00
[_keyboardBtn setTitle:KBLocalized(@"My Keyboard") forState:UIControlStateNormal];
2025-11-25 15:36:16 +08:00
_keyboardBtn.titleLabel.font = [KBFont medium:10];
2025-11-10 19:22:31 +08:00
[_keyboardBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
2025-11-10 19:51:23 +08:00
_keyboardBtn.backgroundColor = [UIColor colorWithHex:KBColorValue];
2025-11-10 19:22:31 +08:00
_keyboardBtn.layer.cornerRadius = 17;
_keyboardBtn.layer.masksToBounds = YES;
2025-11-10 19:51:23 +08:00
2025-11-25 16:10:08 +08:00
// +
2025-11-10 19:51:23 +08:00
UIImage *kbImg = [UIImage imageNamed:@"my_kb_icon"];
if (kbImg) {
[_keyboardBtn setImage:kbImg forState:UIControlStateNormal];
_keyboardBtn.imageView.contentMode = UIViewContentModeScaleAspectFit;
2025-11-25 16:10:08 +08:00
//
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];
2025-11-10 19:51:23 +08:00
}
//
[_keyboardBtn addTarget:self action:@selector(onKeyboardTap) forControlEvents:UIControlEventTouchUpInside];
2025-11-10 19:22:31 +08:00
}
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];
2025-11-10 19:51:23 +08:00
_avatarView.userInteractionEnabled = YES; //
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onAvatarTap)];
[_avatarView addGestureRecognizer:tap];
2025-11-10 19:22:31 +08:00
}
return _avatarView;
}
- (UILabel *)nameLabel {
if (!_nameLabel) {
_nameLabel = [UILabel new];
_nameLabel.text = @"Notice";
2025-11-25 15:36:16 +08:00
_nameLabel.font = [KBFont medium:20];
_nameLabel.textColor = [UIColor colorWithHex:KBBlackValue];
2025-12-11 22:02:18 +08:00
_nameLabel.lineBreakMode = NSLineBreakByTruncatingTail;
[_nameLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
[_nameLabel setContentHuggingPriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
2025-11-10 19:22:31 +08:00
}
return _nameLabel;
}
2025-12-11 22:02:18 +08:00
- (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;
}
2025-11-10 19:51:23 +08:00
- (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;
}
2025-11-10 19:22:31 +08:00
2025-11-10 19:51:23 +08:00
#pragma mark - Actions
2025-11-11 20:24:13 +08:00
- (void)onKeyboardTap {
KBMyKeyBoardVC *vc = [[KBMyKeyBoardVC alloc] init];
[KB_CURRENT_NAV pushViewController:vc animated:true];
}
2025-11-11 14:56:57 +08:00
- (void)onAvatarTap {
KBPersonInfoVC *vc = [[KBPersonInfoVC alloc] init];
2025-12-04 14:44:56 +08:00
// vc.userModel = self.userModel;
2025-11-11 14:56:57 +08:00
[KB_CURRENT_NAV pushViewController:vc animated:true];
}
2025-11-17 18:51:06 +08:00
- (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];
}
2025-11-10 19:22:31 +08:00
@end