// // TXShareMusicCellContentView.m // TUIChat // // Created by 王智 on 2023/8/10. // #import "TXShareMusicCellContentView.h" #import "Masonry.h" #import "UIImageView+WebCache.h" #import "TUIDefine.h" #define TXHexRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0] @interface TXShareMusicCellContentView () @property (nonatomic, strong) UILabel *songName; @property (nonatomic, strong) UILabel *songAuth; @property (nonatomic, strong) UIImageView *uploaderLogo; @property (nonatomic, strong) UILabel *uploaderName; @property (nonatomic, strong) UIImageView *typeImage; @property (nonatomic, strong) UIView *tagView; @end @implementation TXShareMusicCellContentView + (instancetype)shareInstance { return [[TXShareMusicCellContentView alloc] init]; } - (instancetype)init { self = [super init]; if (self) { [self configUI]; } return self; } - (void)configUI { self.layer.cornerRadius = 10.0f; self.backgroundColor = [UIColor whiteColor]; UIImageView *musicLogo = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"music_logo"]]; [self addSubview:musicLogo]; [musicLogo mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self.mas_top).offset(10); make.left.mas_equalTo(self.mas_left).offset(11); make.width.mas_equalTo(41); make.height.mas_equalTo(40); }]; [self addSubview:self.songName]; [self.songName mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(musicLogo.mas_top); make.height.mas_equalTo(20); make.left.mas_equalTo(musicLogo.mas_right).offset(6); }]; [self addSubview:self.typeImage]; [self.typeImage mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.songName.mas_right).offset(4); make.width.mas_equalTo(55); make.height.mas_equalTo(22); make.centerY.mas_equalTo(self.songName.mas_centerY); make.right.mas_lessThanOrEqualTo(self.mas_right).offset(-12); }]; UIView *lineView = [[UIView alloc] init]; lineView.backgroundColor = TXHexRGB(0xF2F2F2); [self addSubview:lineView]; [lineView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(musicLogo.mas_bottom).offset(10); make.left.mas_equalTo(self.mas_left).offset(10); make.right.mas_equalTo(self.mas_right).offset(-10); make.height.mas_equalTo(1); }]; [self addSubview:self.uploaderLogo]; [self.uploaderLogo mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.mas_left).offset(18); make.top.mas_equalTo(lineView.mas_top).offset(8); make.width.height.mas_equalTo(20); }]; [self addSubview:self.songAuth]; [self.songAuth mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.songName.mas_left); make.top.mas_equalTo(self.typeImage.mas_bottom).offset(2); make.height.mas_equalTo(17); make.right.mas_equalTo(self.mas_right).offset(-10); }]; [self addSubview:self.uploaderName]; [self.uploaderName mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.uploaderLogo.mas_right).offset(7); make.centerY.mas_equalTo(self.uploaderLogo.mas_centerY); make.height.mas_equalTo(15); }]; [self addSubview:self.tagView]; [self.tagView mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.mas_equalTo(self.uploaderName.mas_centerY); make.left.mas_equalTo(self.uploaderName.mas_right).offset(8); make.right.mas_equalTo(self.mas_right).offset(-10); make.height.mas_equalTo(16); }]; } - (void)configWithSongName:(NSString *)songName type:(NSString *)type authName:(NSString *)authName sendAvatar:(NSString *)sendAvatar sendName:(NSString *)sendName userId:(NSString *)userId tags:(NSString *)tags { self.songName.text = [self returnNoNullStringWithString:songName]; self.songAuth.text = [self returnNoNullStringWithString:authName]; NSArray *tagArray = [tags componentsSeparatedByString:@","]; NSString *owner = @""; if ([self isEmptyString:sendName]) { owner = [NSString stringWithFormat:@"游客%@",userId]; } else { owner = sendName; } CGFloat maxWidth = [self getTagViewMaxWidth:owner]; [self configTagViewWithTagArray:tagArray maxWidth:maxWidth]; NSString *typeImgName = @""; if ([type isEqualToString:@"VIP"]) { typeImgName = @"music_vip"; } else if ([type isEqualToString:@"CHARGE"]) { typeImgName = @"music_order"; } else { typeImgName = @"music_free"; } [self.typeImage setImage:[UIImage imageNamed:typeImgName]]; self.uploaderName.text = [self returnNoNullStringWithString:owner]; [self.uploaderLogo sd_setImageWithURL:[NSURL URLWithString:[self getUrlEndcodeString:sendAvatar]] placeholderImage:[UIImage imageNamed:@"user_default_avatal"]]; } - (CGFloat)getTagViewMaxWidth:(NSString *)teacherName { CGFloat width = [self getStringWidthInLabel:teacherName font:[UIFont systemFontOfSize:12.0f]]; return 260 - 45 - 10 - 14 - width - 8; } - (void)configTagViewWithTagArray:(NSArray *)tagArray maxWidth:(CGFloat)maxWidth { [self removeAllSubViews:self.tagView]; CGFloat width = maxWidth; CGFloat xSpace = 0.0f; for (NSInteger i = 0; i < tagArray.count; i++) { NSString *tagString = tagArray[i]; CGFloat labelWidth = [self getStringWidthInLabel:tagString font:[UIFont systemFontOfSize:11.0f]]; CGFloat viewWidth = labelWidth + 8; if (xSpace + viewWidth > width) { return; } CGRect frame = CGRectMake(xSpace, 0, viewWidth, 16.0f); [self createTagLabelViewWithName:tagString frame:frame]; xSpace += (viewWidth + 6); } } - (CGFloat)getStringWidthInLabel:(NSString *)tagString font:(UIFont *)font { CGFloat width = [tagString boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 16.0f) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:font} context:nil].size.width+1; return width; } - (void)createTagLabelViewWithName:(NSString *)name frame:(CGRect)frame { UIView *bgView = [[UIView alloc] initWithFrame:frame]; bgView.backgroundColor = TXHexRGB(0xfff1de); bgView.layer.cornerRadius = 4.0f; [self.tagView addSubview:bgView]; UILabel *tagLabel = [[UILabel alloc] init]; tagLabel.text = name; tagLabel.textColor = TXHexRGB(0xff8c00); tagLabel.font = [UIFont systemFontOfSize:11.0f]; tagLabel.textAlignment = NSTextAlignmentCenter; [bgView addSubview:tagLabel]; [tagLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(bgView.mas_left).offset(4); make.right.mas_equalTo(bgView.mas_right).offset(-4); make.top.bottom.mas_equalTo(bgView); }]; } - (NSString *)returnNoNullStringWithString:(NSString *)string { if (string == nil || string.length == 0) { return @""; } return string; } - (NSString *)getUrlEndcodeString:(NSString *)string { return [string stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];; } - (void)removeAllSubViews:(UIView *)view { while (view.subviews.count) { [view.subviews.lastObject removeFromSuperview]; } } - (BOOL)isEmptyString:(NSString *)str { if (str == nil || str.length == 0) { return YES; } return NO; } #pragma mark ----- lazying - (UILabel *)songName { if (!_songName) { _songName = [[UILabel alloc] init]; _songName.textColor = TXHexRGB(0x333333); _songName.font = [UIFont systemFontOfSize:14.0f weight:UIFontWeightSemibold]; } return _songName; } - (UILabel *)songAuth { if (!_songAuth) { _songAuth = [[UILabel alloc] init]; _songAuth.textColor = TXHexRGB(0x6A6A6A); _songAuth.font = [UIFont systemFontOfSize:12.0f]; } return _songAuth; } - (UIImageView *)uploaderLogo { if (!_uploaderLogo) { _uploaderLogo = [[UIImageView alloc] init]; } return _uploaderLogo; } - (UILabel *)uploaderName { if (!_uploaderName) { _uploaderName = [[UILabel alloc] init]; _uploaderName.textColor = TXHexRGB(0x333333); _uploaderName.font = [UIFont systemFontOfSize:12.0f]; } return _uploaderName; } - (UIImageView *)typeImage { if (!_typeImage) { _typeImage = [[UIImageView alloc] init]; } return _typeImage; } - (UIView *)tagView { if (!_tagView) { _tagView = [[UIView alloc] init]; } return _tagView; } /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ @end