CNLiveMineHeaderView.m 13.1 KB
//
//  CNLiveMineHeaderView.m
//  CNLiveBMineModule
//
//  Created by 153993236@qq.com on 11/12/2019.
//  Copyright © 2019 153993236@qq.com. All rights reserved.
//

#import "CNLiveMineHeaderView.h"
#import <Masonry/Masonry.h>
#import <SDWebImage/SDWebImage.h>
#import "QMUIKit.h"
#import "CNLiveCategory.h"
#import "CNLiveBaseKit.h"

#import "CNUserInfoManager.h"
#import "CNLiveUserInfoNotification.h"

@interface CNLiveMineHeaderView ()
@property (nonatomic, strong) UIImageView *bgView;
@property (nonatomic, strong) UIImageView *bgTopView;

@property (nonatomic, strong) UIButton *home;

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

@end

@implementation CNLiveMineHeaderView
static const NSInteger margin = 20;

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        self.userInteractionEnabled = YES;
        [CNLiveUserInfoNotification addUserInfoUpdateObserver:self selector:@selector(userInfoUpdateNotification:)];
        [self createSubViews];
    }
    return self;
}
// 实现方法
- (void)userInfoUpdateNotification:(NSNotification *)noti{
    //使用userInfo处理消息
    NSDictionary *dic = [noti userInfo];
    CNLiveUserInfoDataType type = [[dic objectForKey:@"dataType"] integerValue];
    NSString *info = [dic objectForKey:@"info"];
    if(type == CNLiveUserInfoDataTypeHeader){
        UIImage *xw_image = [self getImageWithImageName:@"CNLiveBMineModule.bundle/default_avatar_y" bundleName:@"CNLiveBMineModule" targetClass:[CNLiveMineHeaderView class]];
        [_header sd_setImageWithURL:[NSURL URLWithString:info] placeholderImage:xw_image];

    }else if(type == CNLiveUserInfoDataTypeNickname){
        _name.text = info;

    }else if(type == CNLiveUserInfoDataTypeAddress){
        _address.attributedText = [self createAttributedImageName:@"CNLiveBMineModule.bundle/mine_location" string:info?info:@"未知" fontSize:13.0 isFront:YES];

    }
}

- (void)dealloc {
    NSLog(@"CNLiveBMineHeaderView -- dealloc");
    [CNLiveUserInfoNotification removeObserver:self];
}

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

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

}

- (void)layoutSubviews{
    [super layoutSubviews];
    [_home mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left);
        make.right.equalTo(self.mas_right);
        make.height.offset(128);
        make.bottom.equalTo(self.mas_bottom);
        
    }];
    
    [_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.home.mas_top).with.offset(40);
    }];
    
    [_bgTopView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).with.offset(margin/1.3);
        make.right.equalTo(self.mas_right).with.offset(-margin/1.3);
        make.top.equalTo(self.mas_top).with.offset(55);
        make.bottom.equalTo(self.mas_bottom);
    }];
    
    [_header mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).with.offset(1.6*margin);
        make.top.equalTo(self.mas_top).with.offset(75);
        make.width.height.offset(66);
    }];

    [_certification mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.header.mas_centerY).with.offset(8);
        make.left.equalTo(self.header.mas_right).with.offset(margin/2.0);
        make.height.offset(24);
    }];
    
    [_name mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.header.mas_top);
        make.left.equalTo(self.certification.mas_left);
        make.right.mas_lessThanOrEqualTo(self.bgView.mas_right).with.offset(-margin);
        make.height.offset(24);
    }];
    
    [_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.offset(16);
    }];

}

- (void)setInteractionEnabled:(BOOL)interactionEnabled{
    _interactionEnabled = interactionEnabled;
    _home.userInteractionEnabled = interactionEnabled;
}

- (void)setIsRefresh:(BOOL)isRefresh{
    _isRefresh = isRefresh;
    if(isRefresh){
        _name.text = CNUserShareModel.nickname;
        _address.attributedText = [self createAttributedImageName:@"CNLiveBMineModule.bundle/mine_location" string:CNUserShareModel.location?CNUserShareModel.location:@"未知" fontSize:13.0 isFront:YES];
    }
}

- (void)setCertificationImages:(NSArray *)certificationImages{
    _certificationImages = certificationImages;
    _certification.attributedText = [self createAttributedImages:certificationImages];
}
/*
 // 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 fontSize:(CGFloat)fontSize 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)];
    UIFont *font = UIFontCNMake(fontSize);
    [attri addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, string.length)];
    // 2.添加表情图片
    NSTextAttachment *attch = [[NSTextAttachment alloc] init];
    // 表情图片
    UIImage *xw_image = [self getImageWithImageName:imageName bundleName:@"CNLiveBMineModule" targetClass:[self class]];
    attch.image = xw_image;
    
    CGFloat paddingTop = font.lineHeight - font.pointSize;
    if(isFront){
        // 设置图片大小
        attch.bounds = CGRectMake(0, -paddingTop, font.pointSize*5/6.0, font.pointSize);
        // 创建带有图片的富文本
        NSAttributedString *str = [NSAttributedString attributedStringWithAttachment:attch];
        [attri insertAttributedString:str atIndex:0];// 插入某个位置
    }else{
        // 设置图片大小
        attch.bounds = CGRectMake(10, -paddingTop, font.pointSize, font.pointSize);
        // 创建带有图片的富文本
        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 getImageWithImageName:image bundleName:@"CNLiveBMineModule" targetClass:[self class]];
        attch.image = xw_image;
        // 创建带有图片的富文本
        CGFloat top = 4;
        if ([image isEqualToString:@"CNLiveBMineModule.bundle/mine_wjj_certification"]) {
            top = 2;
        }
        attch.bounds = CGRectMake(0, top, xw_image.size.width, xw_image.size.height);
        NSAttributedString *str = [NSAttributedString attributedStringWithAttachment:attch];
        NSAttributedString *space = [[NSAttributedString alloc]initWithString:@" "];

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

    }
    return attri;
    
}

#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:CNLiveMineHeaderViewTypeUerInfo];
    }
}
- (void)addressTapDidClicked:(UITapGestureRecognizer *)tap{
    if (self.delegate && [self.delegate respondsToSelector:@selector(headerView:pushToController:)]) {
        [self.delegate headerView:self pushToController:CNLiveMineHeaderViewTypeUerInfo];
    }
}
- (void)homeDidClicked:(UIButton *)btn{
    if (self.delegate && [self.delegate respondsToSelector:@selector(headerView:pushToController:)]) {
        [self.delegate headerView:self pushToController:CNLiveMineHeaderViewTypeHome];
    }
}

#pragma mark - 懒加载
- (UIButton *)home {
    if (!_home) {
        _home = [UIButton buttonWithType:UIButtonTypeCustom];
        UIImage *xw_image = [self getImageWithImageName:@"CNLiveBMineModule.bundle/mine_zy" bundleName:@"CNLiveBMineModule" targetClass:[CNLiveMineHeaderView class]];
        _home.imageView.contentMode = UIViewContentModeScaleAspectFit;
        [_home setBackgroundImage:xw_image forState:UIControlStateNormal];
        _home.adjustsImageWhenHighlighted = NO;
        [_home addTarget:self action:@selector(homeDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _home;
}
- (UIImageView *)bgView {
    if (!_bgView) {
        _bgView = [[UIImageView alloc] init];
        _bgView.userInteractionEnabled = YES;
        UIImage *xw_image = [self getImageWithImageName:@"CNLiveBMineModule.bundle/mine_bg" bundleName:@"CNLiveBMineModule" targetClass:[CNLiveMineHeaderView class]];
        _bgView.image = xw_image;
    }
    return _bgView;
}

- (UIImageView *)bgTopView{
    if (!_bgTopView) {
        _bgTopView = [[UIImageView alloc] init];
        _bgTopView.userInteractionEnabled = YES;
        UIImage *xw_image = [self getImageWithImageName:@"CNLiveBMineModule.bundle/mine_bg_top" bundleName:@"CNLiveBMineModule" targetClass:[CNLiveMineHeaderView class]];
        _bgTopView.image = xw_image;
    }
    return _bgTopView;
}
- (UIImageView *)header {
    if (!_header) {
        _header = [[UIImageView alloc] init];
        _header.userInteractionEnabled = YES;
        _header.layer.masksToBounds = YES;
        _header.layer.cornerRadius = 33;
        UIImage *xw_image = [self getImageWithImageName:@"CNLiveBMineModule.bundle/default_avatar_y" bundleName:@"CNLiveBMineModule" targetClass:[CNLiveMineHeaderView class]];
        [_header sd_setImageWithURL:[NSURL URLWithString:CNUserShareModel.faceUrl] 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 colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0];
        _name.font = [UIFont systemFontOfSize:18];
        _name.text = CNUserShareModel.nickname;
        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];
        NSMutableArray *images = [NSMutableArray arrayWithArray:@[@"CNLiveBMineModule.bundle/mine_wjj_certification", @"CNLiveBMineModule.bundle/mine_common"]];
        // 企业
        if ([CNUserShareModel.wjjStatus.institutionStatus isEqualToString:@"1"]) {
            [images addObject:@"CNLiveBMineModule.bundle/mine_qiye"];
        }
        _certification.attributedText = [self createAttributedImages:images];
    }
    return _certification;
}
- (UILabel *)address {
    if (!_address) {
        _address = [[UILabel alloc] init];
        _address.userInteractionEnabled = YES;
        _address.numberOfLines = 1;
        _address.textColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0];
        _address.font = [UIFont systemFontOfSize:12];
        _address.attributedText = [self createAttributedImageName:@"CNLiveBMineModule.bundle/mine_location" string:CNUserShareModel.location?CNUserShareModel.location:@"未知" fontSize:13.0 isFront:YES];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(addressTapDidClicked:)];
        [_address addGestureRecognizer:tap];
    }
    return _address;
}

@end