143 lines
5.4 KiB
Objective-C
143 lines
5.4 KiB
Objective-C
//
|
|
// KBMyHeaderView.m
|
|
// keyBoard
|
|
//
|
|
|
|
#import "KBMyHeaderView.h"
|
|
#import <Masonry/Masonry.h>
|
|
#import "UIColor+Extension.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 *cardLeft;
|
|
@property (nonatomic, strong) UIImageView *cardRight;
|
|
@end
|
|
|
|
@implementation KBMyHeaderView
|
|
|
|
- (instancetype)initWithFrame:(CGRect)frame {
|
|
if (self = [super initWithFrame:frame]) {
|
|
self.backgroundColor = [UIColor colorWithHex:0xF2F6FA]; // 淡色背景
|
|
|
|
[self addSubview:self.titleLabel];
|
|
[self addSubview:self.keyboardBtn];
|
|
[self addSubview:self.avatarView];
|
|
[self addSubview:self.nameLabel];
|
|
[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(126);
|
|
}];
|
|
[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.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.centerY.equalTo(self.avatarView);
|
|
make.left.equalTo(self.avatarView.mas_right).offset(12);
|
|
make.right.lessThanOrEqualTo(self).offset(-20);
|
|
}];
|
|
// 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)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];
|
|
}
|
|
|
|
#pragma mark - Lazy UI
|
|
- (UILabel *)titleLabel {
|
|
if (!_titleLabel) {
|
|
_titleLabel = [UILabel new];
|
|
_titleLabel.text = @"Settings"; // 大标题
|
|
_titleLabel.font = [UIFont systemFontOfSize:30 weight:UIFontWeightBold];
|
|
_titleLabel.textColor = [UIColor colorWithHex:0x1B1F1A];
|
|
}
|
|
return _titleLabel;
|
|
}
|
|
|
|
- (UIButton *)keyboardBtn {
|
|
if (!_keyboardBtn) {
|
|
_keyboardBtn = [UIButton buttonWithType:UIButtonTypeCustom];
|
|
[_keyboardBtn setTitle:@" My Keyboard " forState:UIControlStateNormal];
|
|
_keyboardBtn.titleLabel.font = [UIFont systemFontOfSize:10 weight:UIFontWeightSemibold];
|
|
[_keyboardBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
|
|
_keyboardBtn.backgroundColor = [UIColor colorWithHex:KBColor];
|
|
_keyboardBtn.layer.cornerRadius = 17;
|
|
_keyboardBtn.layer.masksToBounds = YES;
|
|
}
|
|
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];
|
|
}
|
|
return _avatarView;
|
|
}
|
|
|
|
- (UILabel *)nameLabel {
|
|
if (!_nameLabel) {
|
|
_nameLabel = [UILabel new];
|
|
_nameLabel.text = @"Notice";
|
|
_nameLabel.font = [UIFont systemFontOfSize:20 weight:UIFontWeightSemibold];
|
|
_nameLabel.textColor = [UIColor colorWithHex:0x1B1F1A];
|
|
}
|
|
return _nameLabel;
|
|
}
|
|
|
|
- (UIImageView *)cardLeft { if (!_cardLeft) { _cardLeft = [UIImageView new]; _cardLeft.contentMode = UIViewContentModeScaleAspectFit; _cardLeft.image = [UIImage imageNamed:@"my_member_icon"];} return _cardLeft; }
|
|
- (UIImageView *)cardRight { if (!_cardRight) { _cardRight = [UIImageView new];_cardRight.contentMode = UIViewContentModeScaleAspectFit; _cardRight.image = [UIImage imageNamed:@"my_recharge_icon"];} return _cardRight; }
|
|
|
|
|
|
@end
|