CNLiveSettingHeaderView.m 13 KB
//
//  CNLiveSettingHeaderView.m
//  CNLiveSettingModule
//
//  Created by 153993236@qq.com on 11/12/2019.
//  Copyright © 2019 153993236@qq.com. All rights reserved.
//

#import "CNLiveSettingHeaderView.h"
#import <Masonry/Masonry.h>
#import <SDWebImage/SDWebImage.h>
#import "QMUIKit.h"

@interface CNLiveSettingHeaderView ()
@property (nonatomic, strong) UIImageView *bgView;
@property (nonatomic, strong) UIButton *witness;

@property (nonatomic, strong) UIImageView *header;
@property (nonatomic, strong) UILabel *name;
@property (nonatomic, strong) UILabel *certification;
@property (nonatomic, strong) UILabel *address;
@property (nonatomic, strong) UIButton *sign;

@end

@implementation CNLiveSettingHeaderView
static const NSInteger margin = 20;

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        self.userInteractionEnabled = YES;
        self.backgroundColor = [UIColor colorWithRed:244.0/255.0 green:244.0/255.0 blue:244.0/255.0 alpha:1.0];
//        [XWUserInfoNotification addUserInfoUpdateObserver:self selector:@selector(userInfoUpdateNotification:)];
        [self createSubViews];
    }
    return self;
}
// 实现方法
- (void)userInfoUpdateNotification:(NSNotification *)noti{
    //使用userInfo处理消息
//    NSDictionary *dic = [noti userInfo];
//    XWUserInfoDataType type = [[dic objectForKey:@"type"] integerValue];
//    if(type == XWUserInfoDataTypeHead){
//        UIImage *info = [dic objectForKey:@"info"];
//        _header.image = info;
//
//    }else if(type == XWUserInfoDataTypeNickname){
//        NSString *info = [dic objectForKey:@"info"];
//        _name.attributedText = [self createAttributedImageName:@"mine_code" string:info isFront:NO];
//
//    }else if(type == XWUserInfoDataTypeAddress){
//        NSString *info = [dic objectForKey:@"info"];
//        _address.attributedText = [self createAttributedImageName:@"mine_location" string:info isFront:YES];
//
//    }
}

- (void)dealloc {
    NSLog(@"CNLiveMineHeaderView -- dealloc");
//    [XWUserInfoNotification removeObserver:self];

}

- (void)createSubViews {
    [self addSubview:self.bgView];
    [self addSubview:self.witness];

    [self addSubview:self.header];
    [self addSubview:self.name];
    [self addSubview:self.certification];
    [self addSubview:self.address];
    [self addSubview:self.sign];

}

- (void)layoutSubviews{
    [super layoutSubviews];
    [_witness mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).with.offset(1.5*margin);
        make.right.equalTo(self.mas_right).with.offset(-1.5*margin);
        make.width.height.offset(60);
        make.bottom.equalTo(self.mas_bottom).with.offset(-0.5*margin);
        
    }];
    
    [_bgView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).with.offset(0);
        make.right.equalTo(self.mas_right).with.offset(-0);
        make.top.equalTo(self.mas_top).with.offset(0);
        make.bottom.equalTo(self.witness.mas_centerY).with.offset(0.5*margin);
        
    }];
    
    [_header mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).with.offset(margin);
        make.centerY.equalTo(self.bgView.mas_centerY).with.offset(0);
        make.width.height.offset(60);

    }];
    
    [_certification mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.header.mas_centerY);
        make.left.equalTo(self.header.mas_right).with.offset(margin/2.0);
        make.height.offset(35);
        
    }];
    
    [_name mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.certification.mas_top);
        make.left.equalTo(self.certification.mas_left);
        make.right.equalTo(self.bgView.mas_right).with.offset(-margin);
        make.height.equalTo(self.certification.mas_height);
        
    }];
    
    [_address mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.certification.mas_bottom);
        make.left.equalTo(self.name.mas_left);
        make.right.equalTo(self.name.mas_right);
        make.height.equalTo(self.name.mas_height);
        
    }];

    [_sign mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right).with.offset(0);
        make.centerY.equalTo(self.header.mas_centerY).with.offset(0);
        make.width.mas_equalTo(105);
        make.height.mas_equalTo(32);
        
    }];
}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */
#pragma mark - Private Methods
- (NSMutableAttributedString *)createAttributedImageName:(NSString *)imageName string:(NSString *)string isFront:(BOOL)isFront{
    if(isFront){
        string = [NSString stringWithFormat:@" %@",string];
    }
    // 1.创建一个富文本
    NSMutableAttributedString *attri =  [[NSMutableAttributedString alloc] initWithString:string];
    // 修改富文本中的不同文字的样式
//    [attri addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, string.length)];
//    [attri addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, string.length)];
    // 2.添加表情图片
    NSTextAttachment *attch = [[NSTextAttachment alloc] init];
    // 表情图片
    UIImage *xw_image = [self xw_getImageName:imageName bundleName:@"CNLiveSettingModule" targetClass:[CNLiveSettingHeaderView class]];
    attch.image = xw_image;
    if(isFront){
        // 设置图片大小
        attch.bounds = CGRectMake(0, -1.5, 10, 12);
        // 创建带有图片的富文本
        NSAttributedString *str = [NSAttributedString attributedStringWithAttachment:attch];
        [attri insertAttributedString:str atIndex:0];// 插入某个位置
    }else{
        // 设置图片大小
        attch.bounds = CGRectMake(10, -2, 15, 15);
        // 创建带有图片的富文本
        NSAttributedString *str = [NSAttributedString attributedStringWithAttachment:attch];
        [attri insertAttributedString:str atIndex:string.length];// 插入某个位置

    }
    return attri;
    
}
- (NSMutableAttributedString *)createAttributedImages:(NSArray *)images {
    // 1.创建一个富文本
    NSMutableAttributedString *attri =  [[NSMutableAttributedString alloc] initWithString:@""];
    for(NSString *image in images){
        // 2.添加表情图片
        NSTextAttachment *attch = [[NSTextAttachment alloc] init];
        // 表情图片
        UIImage *xw_image = [self xw_getImageName:image bundleName:@"CNLiveSettingModule" targetClass:[CNLiveSettingHeaderView class]];
        attch.image = xw_image;
        // 创建带有图片的富文本
        NSAttributedString *str = [NSAttributedString attributedStringWithAttachment:attch];
        NSAttributedString *space = [[NSAttributedString alloc]initWithString:@" "];

        [attri insertAttributedString:space atIndex:0];
        [attri insertAttributedString:str atIndex:0];// 插入某个位置

    }
    return attri;
    
}

#pragma mark - 懒加载
- (UIButton *)witness {
    if (!_witness) {
        _witness = [UIButton buttonWithType:UIButtonTypeCustom];
        UIImage *xw_image = [self xw_getImageName:@"mine_observer" bundleName:@"CNLiveSettingModule" targetClass:[CNLiveSettingHeaderView class]];
        _witness.imageView.contentMode = UIViewContentModeScaleAspectFit;
        [_witness setImage:xw_image forState:UIControlStateNormal];
        _witness.adjustsImageWhenHighlighted = NO;
        [_witness addTarget:self action:@selector(witnessDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _witness;
}
- (UIImageView *)bgView {
    if (!_bgView) {
        _bgView = [[UIImageView alloc] init];
        _bgView.userInteractionEnabled = YES;
        UIImage *xw_image = [self xw_getImageName:@"mine_bg" bundleName:@"CNLiveSettingModule" targetClass:[CNLiveSettingHeaderView class]];
        _bgView.image = xw_image;
    }
    return _bgView;
}
- (UIImageView *)header {
    if (!_header) {
        _header = [[UIImageView alloc] init];
        _header.userInteractionEnabled = YES;
        _header.layer.masksToBounds = YES;
        _header.layer.cornerRadius = 30;
        UIImage *xw_image = [self xw_getImageName:@"default_avatar_y" bundleName:@"CNLiveSettingModule" targetClass:[CNLiveSettingHeaderView class]];
        [_header sd_setImageWithURL:[NSURL URLWithString:@"头像"] placeholderImage:xw_image];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(headerTapDidClicked:)];
        [_header addGestureRecognizer:tap];
    }
    return _header;
}

- (UILabel *)name {
    if (!_name) {
        _name = [[UILabel alloc] init];
        _name.userInteractionEnabled = YES;
        _name.numberOfLines = 1;
        _name.textColor = [UIColor whiteColor];
        _name.font = [UIFont systemFontOfSize:16];
        _name.attributedText = [self createAttributedImageName:@"mine_code" string:@"张晓文" isFront:NO];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(nameTapDidClicked:)];
        [_name addGestureRecognizer:tap];
        
    }
    return _name;
}
- (UILabel *)certification {
    if (!_certification) {
        _certification = [[UILabel alloc] init];
        _certification.numberOfLines = 1;
        _certification.textColor = [UIColor whiteColor];
        _certification.font = [UIFont systemFontOfSize:16];
        _certification.attributedText = [self createAttributedImages:@[@"mine_overbadge",@"mine_common", @"mine_wjj_certification"]];
    }
    return _certification;
}
- (UILabel *)address {
    if (!_address) {
        _address = [[UILabel alloc] init];
        _address.numberOfLines = 1;
        _address.textColor = [UIColor colorWithRed:140/255.0 green:140/255.0 blue:142/255.0 alpha:1.0];
        _address.font = [UIFont systemFontOfSize:13];
        _address.attributedText = [self createAttributedImageName:@"mine_location" string:@"北京海淀区花园桥" isFront:YES];
    }
    return _address;
}
- (UIButton *)sign {
    if (!_sign) {
        _sign = [UIButton buttonWithType:UIButtonTypeCustom];
        _sign.frame = CGRectMake(0, 0, 105, 32);
        _sign.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.2];
        [_sign setTitle:@"签到赚积分" forState:UIControlStateNormal];
        _sign.titleLabel.font = [UIFont systemFontOfSize:13.0];
        [_sign setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        UIImage *xw_image = [self xw_getImageName:@"sign_in_soccer" bundleName:@"CNLiveSettingModule" targetClass:[CNLiveSettingHeaderView class]];
        [_sign setImage:xw_image forState:UIControlStateNormal];
        [_sign addTarget:self action:@selector(signDidClicked:) forControlEvents:UIControlEventTouchUpInside];
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:_sign.layer.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerTopLeft cornerRadii:CGSizeMake(16, 16)];
        CAShapeLayer *maskLayer = [CAShapeLayer layer];
        maskLayer.frame = _sign.bounds;
        maskLayer.path = maskPath.CGPath;
        _sign.layer.mask = maskLayer;
    }
    return _sign;
}

#pragma mark - 响应方法
- (void)headerTapDidClicked:(UITapGestureRecognizer *)tap{
    if (self.delegate && [self.delegate respondsToSelector:@selector(headerView:pushToController:)]) {
        [self.delegate headerView:self pushToController:CNLiveMineHeaderViewTypeUerInfo];
             
    }
    
}
- (void)nameTapDidClicked:(UITapGestureRecognizer *)tap{
    if (self.delegate && [self.delegate respondsToSelector:@selector(headerView:pushToController:)]) {
        [self.delegate headerView:self pushToController:CNLiveMineHeaderViewTypeCode];
             
    }
    
}
- (void)signDidClicked:(UIButton *)btn{
    if (self.delegate && [self.delegate respondsToSelector:@selector(headerView:pushToController:)]) {
        [self.delegate headerView:self pushToController:CNLiveMineHeaderViewTypeSign];
    }
}

- (void)witnessDidClicked:(UIButton *)btn{
    if (self.delegate && [self.delegate respondsToSelector:@selector(headerView:pushToController:)]) {
        [self.delegate headerView:self pushToController:CNLiveMineHeaderViewTypeWintess];
    }
}

/**
 *  创建图片
 *
 *  @param imageName 图片名字
 *  @param bundleName 图片所在的bundle名字
 *  @param targetClass 类和bundle的同级目录
 *  @return 返回UIImage
 */
- (UIImage *)xw_getImageName:(NSString *)imageName bundleName:(NSString *)bundleName targetClass:(Class)targetClass{
    NSBundle *bundle = [NSBundle bundleForClass:targetClass];
    NSURL *url = [bundle URLForResource:bundleName withExtension:@"bundle"];
    NSBundle *targetBundle = [NSBundle bundleWithURL:url];
    UIImage *image = [UIImage imageNamed:imageName inBundle:targetBundle compatibleWithTraitCollection:nil];
    return image?image:[UIImage imageNamed:imageName inBundle:[NSBundle mainBundle] compatibleWithTraitCollection:nil];
    
}

@end