// // XPMineHeadView.m // YuMi // // Created by YuMi on 2021/9/16. // #import "XPMineHeadView.h" ///Third #import ///Tool #import "UIImage+Utils.h" #import "DJDKMIMOMColor.h" ///View #import "NetImageView.h" #import "XPNobleCenterEntranceView.h" #import "UIImage+Utils.h" #import "XPMineFriendNumberView.h" #import "XPMineAccountView.h" ///Model #import "UserInfoModel.h" #import "WalletInfoModel.h" @interface XPMineHeadView () ///头像 @property (nonatomic,strong) NetImageView * avatarImageView; ///name的容器 @property (nonatomic,strong) UIStackView *nameStackView; ///贵族等级icon @property (nonatomic,strong) NetImageView *nobleImageView; ///名字 @property (nonatomic,strong) UILabel *nameLabel; ///id 的容器 @property (nonatomic,strong) UIStackView *idStackView; ///id @property (nonatomic,strong) UILabel *idLabel; ///性别 @property (nonatomic,strong) UIButton *sexImageView; ///星座 @property(nonatomic,strong) UIImageView *starVeiw; ///显示等级 @property (nonatomic,strong) YYLabel *levelLabel; ///关注、粉丝的容器 @property (nonatomic,strong) UIStackView *attentionStackView; ///关注 @property (nonatomic,strong) XPMineFriendNumberView *attentionView; ///粉丝 @property (nonatomic,strong) XPMineFriendNumberView *fansView; ///账户中心 @property (nonatomic, strong) XPMineAccountView *accountView; ///贵族中心 @property (nonatomic, strong) XPNobleCenterEntranceView *nobleEntranceView; //审核View @property (nonatomic,strong) UIImageView *reviewView; @end @implementation XPMineHeadView - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self initSubViews]; [self initSubViewConstraints]; } return self; } #pragma mark - Private Method - (void)initSubViews { self.backgroundColor = [UIColor clearColor]; [self addSubview:self.avatarImageView]; [self addSubview:self.nobleImageView]; [self addSubview:self.nameStackView]; [self addSubview:self.idStackView]; [self.nameStackView addArrangedSubview:self.nameLabel]; [self.idStackView addArrangedSubview:self.idLabel]; [self.idStackView addArrangedSubview:self.sexImageView]; [self.idStackView addArrangedSubview:self.starVeiw]; [self addSubview:self.levelLabel]; [self addSubview:self.attentionStackView]; [self.attentionStackView addArrangedSubview:self.attentionView]; [self.attentionStackView addArrangedSubview:self.fansView]; [self addSubview:self.accountView]; [self addSubview:self.nobleEntranceView]; [self.avatarImageView addSubview:self.reviewView]; UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAvatarImageView)]; [self.avatarImageView addGestureRecognizer:tap]; } - (void)initSubViewConstraints { [self.avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.size.mas_equalTo(CGSizeMake(74, 74)); make.leading.mas_equalTo(self).offset(15); make.top.mas_equalTo(self).offset(49 + kStatusBarHeight); }]; [self.nobleImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.avatarImageView.mas_right).offset(12); make.top.mas_equalTo(self.avatarImageView).mas_offset(4); make.width.height.mas_equalTo(20); }]; [self.nameStackView mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.mas_equalTo(self.nobleImageView); make.left.mas_equalTo(self.nobleImageView.mas_right).mas_offset(5); make.height.mas_equalTo(16); make.right.mas_equalTo(-15); }]; [self.idStackView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.nobleImageView); make.top.mas_equalTo(self.nameStackView.mas_bottom).offset(13); make.height.mas_equalTo(18); }]; CGFloat starWidth = 14 * 46 / 18; [self.starVeiw mas_makeConstraints:^(MASConstraintMaker *make) { make.width.mas_equalTo(starWidth); make.height.mas_equalTo(14); }]; [self.sexImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.width.mas_equalTo(28); make.height.mas_equalTo(14); }]; [self.levelLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.nobleImageView); make.top.mas_equalTo(self.idStackView.mas_bottom).offset(5); }]; [self.attentionStackView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self.avatarImageView).mas_offset(37); make.height.mas_equalTo(40); make.width.mas_equalTo(108); make.right.mas_equalTo(0); }]; CGFloat width = (KScreenWidth - 30) / 4; CGFloat height = 45; [self.attentionView mas_makeConstraints:^(MASConstraintMaker *make) { make.size.mas_equalTo(CGSizeMake(54, 34)); }]; [self.fansView mas_makeConstraints:^(MASConstraintMaker *make) { make.size.mas_equalTo(CGSizeMake(54, 34)); }]; width = (KScreenWidth - 30 - 13) * 0.5; height = width * 60 / 166; [self.accountView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.avatarImageView.mas_bottom).mas_offset(23); make.size.mas_equalTo(CGSizeMake(width, height)); make.left.mas_equalTo(15); }]; [self.nobleEntranceView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self.accountView); make.size.mas_equalTo(CGSizeMake(width, height)); make.trailing.mas_equalTo(-15); }]; [self.reviewView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.bottom.equalTo(self.avatarImageView); make.height.mas_equalTo(20); }]; } #pragma mark - Action - (void)tapAvatarImageView { if (self.delegate && [self.delegate respondsToSelector:@selector(xPMineHeadView:didClickAvatar:)]) { [self.delegate xPMineHeadView:self didClickAvatar:self.userInfo]; } } ///粉丝 - (void)tapFansRecognizer { if (self.delegate && [self.delegate respondsToSelector:@selector(xpMineHeadViewClickFans)]) { [self.delegate xpMineHeadViewClickFans]; } } ///关注 - (void)tapAttentionRecognizer { if (self.delegate && [self.delegate respondsToSelector:@selector(xpMineHeadViewClickAttention)]) { [self.delegate xpMineHeadViewClickAttention]; } } ///账号中心 - (void)tapAccountrecognizer { if (self.delegate && [self.delegate respondsToSelector:@selector(xpMineHeadViewClickAcount)]) { [self.delegate xpMineHeadViewClickAcount]; } } ///贵族中心 - (void)tapNobleCenterrecognizer { if (self.delegate && [self.delegate respondsToSelector:@selector(xPMineHeadViewCliekNobleCenter)]) { [self.delegate xPMineHeadViewCliekNobleCenter]; } } #pragma mark - NSMutableAttributedString -(NSInteger) getMonth:(long )time { NSDate *date = [NSDate dateWithTimeIntervalSince1970:time/1000]; NSCalendar* calendar = [NSCalendar currentCalendar]; NSDateComponents* components = [calendar components:NSCalendarUnitMonth fromDate:date]; NSInteger month = components.month; return month; } - (NSInteger) getDay:(long) time { NSDate *date = [NSDate dateWithTimeIntervalSince1970:time/1000]; NSCalendar* calendar = [NSCalendar currentCalendar]; NSDateComponents* components = [calendar components:NSCalendarUnitDay fromDate:date]; NSInteger day = components.day; return day; } /// 生成一个图片的富文本 /// @param imageUrl 网络图片的地址 - (NSMutableAttributedString *)createUrlImageAttribute:(NSString *)imageUrl { NetImageConfig *config = [[NetImageConfig alloc]init]; ///先这样吧 config.autoLoad = YES; NetImageView *imageView = [[NetImageView alloc]initWithUrl:imageUrl config:config]; UIImage* image = imageView.image; if (image) { CGFloat height = image.size.height > 0 ? image.size.height : 1; CGFloat scale = image.size.width / height ; imageView.bounds = CGRectMake(0, 0, 20 * scale, 20); } else { NSURL *imgUrl = [NSURL URLWithString:imageUrl]; UIImage *myImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:imgUrl]]; CGFloat height = myImage.size.height > 0 ? myImage.size.height : 1; CGFloat scale = myImage.size.width / height; imageView.bounds = CGRectMake(0, 0, 20 * scale, 20); } imageView.layer.masksToBounds = YES; imageView.contentMode = UIViewContentModeScaleAspectFit; NSMutableAttributedString * attrString = [NSMutableAttributedString yy_attachmentStringWithContent:imageView contentMode:UIViewContentModeScaleAspectFit attachmentSize:CGSizeMake(imageView.bounds.size.width, imageView.bounds.size.height) alignToFont:[UIFont systemFontOfSize:15.0] alignment:YYTextVerticalAlignmentCenter]; return attrString; } /// 占位的富文本 /// @param width 需要的间隙 - (NSMutableAttributedString *)createSapceAttribute:(CGFloat)width { UIView *spaceView = [[UIView alloc]init]; spaceView.backgroundColor = [UIColor clearColor]; spaceView.bounds = CGRectMake(0, 0, width, 10); NSMutableAttributedString * attribute = [NSMutableAttributedString yy_attachmentStringWithContent:spaceView contentMode:UIViewContentModeScaleAspectFit attachmentSize:CGSizeMake(spaceView.frame.size.width, spaceView.frame.size.height) alignToFont:[UIFont systemFontOfSize:15.0] alignment:YYTextVerticalAlignmentCenter]; return attribute; } ///铭牌 - (NSMutableAttributedString *)createNameplateAttibute:(NSString *)tagName image:(NSString *)imageName textFont:(UIFont *)textFont { NetImageConfig *config = [[NetImageConfig alloc]init]; ///先这样吧 config.autoLoad = YES; NetImageView *imageView = [[NetImageView alloc]initWithUrl:imageName config:config]; UIImage* image = imageView.image; if (image) { CGFloat scale = image.size.width / (image.size.height > 0 ? image.size.height : 1); imageView.bounds = CGRectMake(0, 0, 20 * scale, 20); } else { NSURL *imgUrl = [NSURL URLWithString:imageName]; UIImage *myImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:imgUrl]]; CGFloat scale = myImage.size.width / (myImage.size.height > 0 ? myImage.size.height : 1); if (scale == 0) { imageView.bounds = CGRectMake(0, 0, 60, 20); }else { imageView.bounds = CGRectMake(0, 0, 20 * scale, 20); } } imageView.contentMode = UIViewContentModeScaleAspectFit; // 铭牌文字 UILabel *label = [[UILabel alloc] init]; label.textAlignment = NSTextAlignmentCenter; label.font = [UIFont boldSystemFontOfSize:12]; label.adjustsFontSizeToFitWidth = YES; label.textColor = UIColor.whiteColor; label.text = tagName; [imageView addSubview:label]; [label mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(27); make.right.mas_equalTo(-8); make.centerY.mas_equalTo(0); }]; NSMutableAttributedString *string = [NSMutableAttributedString yy_attachmentStringWithContent:imageView contentMode:UIViewContentModeScaleAspectFit attachmentSize:imageView.bounds.size alignToFont:[UIFont boldSystemFontOfSize:12] alignment:YYTextVerticalAlignmentCenter]; return string; } //复制id -(void)copyNameAction{ [XNDJTDDLoadingTool showSuccessWithMessage:YMLocalizedString(@"XPMineHeadView8")]; UIPasteboard *pasteboard = [UIPasteboard generalPasteboard]; pasteboard. string = [NSString stringWithFormat:@"%ld", (long)_userInfo.erbanNo]; } - (NSMutableAttributedString *)creatNameplateIdLabelAttribute:(UserInfoModel *)userInfo{ UIFont *font = [UIFont systemFontOfSize:14]; NSString *text = [NSString stringWithFormat:@"ID:%ld", (long)userInfo.erbanNo]; NSMutableAttributedString *textAtt = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@ ",text] attributes:@{NSForegroundColorAttributeName:UIColorFromRGB(0x6D6B89)}]; NSTextAttachment * attachment = [[NSTextAttachment alloc] init]; UIImage *iconImage = [UIImage imageNamed:@"user_card_copy_id1"];; attachment.bounds = CGRectMake(0, roundf(font.capHeight - iconImage.size.height)/2.f, iconImage.size.width, iconImage.size.height); attachment.image = iconImage; [textAtt insertAttributedString:[NSMutableAttributedString attributedStringWithAttachment:attachment] atIndex:textAtt.length]; return textAtt; } ///当装扮铭牌时,我的页面中不需要显示铭牌标识 - (void)creatNameplateLevleAttribute:(UserInfoModel *)userInfo complete:(nonnull CompletionHandler)complete{ NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] init]; if (!userInfo) { if(complete){ complete(attributedString); } return; } //主播认证 if (userInfo.nameplateWord.length > 0 && userInfo.nameplatePic.length>0) { [self createNameplateAttibute:userInfo.nameplateWord image:userInfo.nameplatePic textFont:[UIFont systemFontOfSize:9]]; [attributedString appendAttributedString:[self createSapceAttribute:2]]; } // userRank 用户级别 if (userInfo.userLevelVo.experUrl) { [attributedString appendAttributedString:[self createUrlImageAttribute:userInfo.userLevelVo.experUrl]]; [attributedString appendAttributedString:[self createSapceAttribute:2]]; } //charmRank 魅力等级 if (userInfo.userLevelVo.charmUrl) { [attributedString appendAttributedString:[self createUrlImageAttribute:userInfo.userLevelVo.charmUrl]]; [attributedString appendAttributedString:[self createSapceAttribute:4]]; } if(complete){ complete(attributedString); } } #pragma mark - Getters And Setters - (void)setUserInfo:(UserInfoModel *)userInfo { _userInfo = userInfo; if (_userInfo) { self.idLabel.attributedText = userInfo.idAtt; self.nameLabel.text = _userInfo.nick.length > 0 ? _userInfo.nick : @""; self.avatarImageView.imageUrl = userInfo.avatar; if (userInfo.isReview) { self.reviewView.hidden = NO; } else { self.reviewView.hidden = YES; self.avatarImageView.imageUrl = userInfo.avatar; } self.nobleImageView.imageUrl = userInfo.userVipInfoVO.vipIcon; UIImage *nobleImage = self.nobleImageView.image; if (nobleImage) { CGFloat scale = nobleImage.size.width / nobleImage.size.height; [self.nobleImageView mas_updateConstraints:^(MASConstraintMaker *make) { make.size.mas_equalTo(CGSizeMake(20* scale, 20)); }]; [self.nameStackView mas_updateConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.nobleImageView.mas_right).mas_offset(5); }]; } else { [self.nobleImageView mas_updateConstraints:^(MASConstraintMaker *make) { make.size.mas_equalTo(CGSizeMake(0, 20)); }]; [self.nameStackView mas_updateConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.nobleImageView.mas_right); }]; } NSString *starImage = [NSString getCalculateConstellationImageWithMonth:_userInfo.birth]; if(starImage.length > 0){ self.starVeiw.image = kImage(starImage); } [self.sexImageView setTitle:[NSString getAgeWithBirth:userInfo.birth] forState:UIControlStateNormal]; self.sexImageView.hidden = NO; self.sexImageView.backgroundColor = userInfo.gender == GenderType_Male ? UIColorFromRGB(0x6BB3FF) :UIColorFromRGB(0xFF80CC); self.sexImageView.selected = _userInfo.gender != GenderType_Male; self.sexImageView.titleEdgeInsets = _userInfo.gender != GenderType_Male ? UIEdgeInsetsMake(0, 2, 0, 0):UIEdgeInsetsMake(0, -1, 0, 0); self.levelLabel.attributedText = _userInfo.levelAtt; self.fansView.number = [NSString stringWithFormat:@"%ld",_userInfo.fansNum]; self.attentionView.number = [NSString stringWithFormat:@"%ld",_userInfo.followNum]; self.nobleEntranceView.vipInfo = userInfo.userVipInfoVO; } } - (void)setWalletInfo:(WalletInfoModel *)walletInfo { self.accountView.diamonds = walletInfo.diamonds; } - (void)setNobleInfo:(NobleCenterModel *)nobleInfo { self.nobleEntranceView.nobleInfo = nobleInfo; } - (NetImageView *)avatarImageView { if (!_avatarImageView) { NetImageConfig * config = [[NetImageConfig alloc]init]; config.imageType = ImageTypeUserIcon; config.placeHolder = [UIImageConstant defaultAvatarPlaceholder]; _avatarImageView = [[NetImageView alloc] initWithConfig:config]; _avatarImageView.userInteractionEnabled = YES; _avatarImageView.layer.masksToBounds = YES; _avatarImageView.layer.cornerRadius = 37; } return _avatarImageView; } - (UIStackView *)nameStackView { if (!_nameStackView) { _nameStackView = [[UIStackView alloc] init]; _nameStackView.axis = UILayoutConstraintAxisHorizontal; _nameStackView.distribution = UIStackViewDistributionFill; _nameStackView.alignment = UIStackViewAlignmentCenter; _nameStackView.spacing = 5; } return _nameStackView; } - (NetImageView *)nobleImageView { if (!_nobleImageView) { _nobleImageView = [[NetImageView alloc] init]; } return _nobleImageView; } - (UILabel *)nameLabel { if (!_nameLabel) { _nameLabel = [[UILabel alloc] init]; _nameLabel.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:16]; _nameLabel.textColor = [DJDKMIMOMColor colorWithHexString:@"#1F1A4E"]; } return _nameLabel; } - (UIStackView *)idStackView { if (!_idStackView) { _idStackView = [[UIStackView alloc] init]; _idStackView.axis = UILayoutConstraintAxisHorizontal; _idStackView.distribution = UIStackViewDistributionFill; _idStackView.alignment = UIStackViewAlignmentCenter; _idStackView.spacing = 5; } return _idStackView; } - (UILabel *)idLabel { if (!_idLabel) { _idLabel = [[UILabel alloc] init]; _idLabel.font = [UIFont systemFontOfSize:14]; _idLabel.textColor = UIColorFromRGB(0x6D6B89); _idLabel.userInteractionEnabled = YES; UITapGestureRecognizer *longPress = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(copyNameAction)]; [_idLabel addGestureRecognizer:longPress]; } return _idLabel; } - (UIButton *)sexImageView { if (!_sexImageView) { _sexImageView = [[UIButton alloc] init]; [_sexImageView setImage:kImage(@"home_age_boy_icon") forState:UIControlStateNormal]; [_sexImageView setImage:kImage(@"home_age_girl_icon") forState:UIControlStateSelected]; _sexImageView.titleLabel.font = [UIFont systemFontOfSize:10 weight:UIFontWeightMedium]; [_sexImageView setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; _sexImageView.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0); _sexImageView.layer.cornerRadius = 14/2; _sexImageView.layer.masksToBounds = YES; _sexImageView.hidden = YES; } return _sexImageView; } - (YYLabel *)levelLabel { if (!_levelLabel) { _levelLabel = [[YYLabel alloc] init]; _levelLabel.preferredMaxLayoutWidth = KScreenWidth - 88; } return _levelLabel; } - (UIStackView *)attentionStackView { if (!_attentionStackView) { _attentionStackView = [[UIStackView alloc] init]; _attentionStackView.axis = UILayoutConstraintAxisHorizontal; _attentionStackView.distribution = UIStackViewDistributionFill; _attentionStackView.alignment = UIStackViewAlignmentFill; } return _attentionStackView; } - (XPMineFriendNumberView *)attentionView { if (!_attentionView) { _attentionView = [[XPMineFriendNumberView alloc] init]; _attentionView.title = YMLocalizedString(@"XPMineHeadView4"); _attentionView.number = @"0"; UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAttentionRecognizer)]; [_attentionView addGestureRecognizer:tap]; } return _attentionView; } - (XPMineFriendNumberView *)fansView { if (!_fansView) { _fansView = [[XPMineFriendNumberView alloc] init]; _fansView.title = YMLocalizedString(@"XPMineHeadView5"); UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapFansRecognizer)]; [_fansView addGestureRecognizer:tap]; } return _fansView; } - (XPMineAccountView *)accountView { if (!_accountView) { _accountView = [[XPMineAccountView alloc] init]; UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAccountrecognizer)]; [_accountView addGestureRecognizer:tap]; } return _accountView; } - (XPNobleCenterEntranceView *)nobleEntranceView { if (!_nobleEntranceView) { _nobleEntranceView = [[XPNobleCenterEntranceView alloc] init]; UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapNobleCenterrecognizer)]; [_nobleEntranceView addGestureRecognizer:tap]; } return _nobleEntranceView; } - (UIImageView *)reviewView{ if (!_reviewView){ _reviewView = [UIImageView new]; _reviewView.hidden = YES; _reviewView.image = [UIImage imageNamed:@"mine_avatar_reviewing"]; UILabel *titleView = [[UILabel alloc] init]; titleView.text = YMLocalizedString(@"XPMineHeadView7"); titleView.font = [UIFont systemFontOfSize:11 weight:UIFontWeightRegular]; titleView.textColor = [UIColor whiteColor]; titleView.textAlignment = NSTextAlignmentCenter; [_reviewView addSubview:titleView]; [titleView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(_reviewView); }]; } return _reviewView; } - (UIImageView *)starVeiw{ if(!_starVeiw){ _starVeiw = [UIImageView new]; } return _starVeiw; } @end