123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- //
- // RecentMusicView.m
- // KulexiuForStudent
- //
- // Created by 王智 on 2022/10/18.
- //
- #import "RecentMusicView.h"
- #import "MusicTagView.h"
- @interface RecentMusicView ()
- @property (weak, nonatomic) IBOutlet UILabel *songNameLabel;
- @property (weak, nonatomic) IBOutlet UILabel *songAuth;
- @property (weak, nonatomic) IBOutlet UILabel *uploadName;
- @property (weak, nonatomic) IBOutlet UIView *tagView;
- @property (weak, nonatomic) IBOutlet UIView *lineView;
- @property (nonatomic, copy) RecentMusicDetailBlock callback;
- @property (nonatomic, strong) RecentPracticeModel *model;
- @property (weak, nonatomic) IBOutlet UIImageView *qualityMusicTag;
- @property (weak, nonatomic) IBOutlet NSLayoutConstraint *qualityTagWidth;
- @property (weak, nonatomic) IBOutlet UIImageView *albumTag;
- @property (weak, nonatomic) IBOutlet NSLayoutConstraint *albumTagWidth;
- @property (weak, nonatomic) IBOutlet NSLayoutConstraint *albumTagLeft;
- @end
- @implementation RecentMusicView
- + (instancetype)shareInstance {
- RecentMusicView *view = [[[NSBundle mainBundle] loadNibNamed:@"RecentMusicView" owner:nil options:nil] firstObject];
- return view;
- }
- - (void)configWithMusicModel:(RecentPracticeModel *)sourceModel hiddenLineView:(BOOL)hideLineView callback:(RecentMusicDetailBlock)callback {
- if (callback) {
- self.callback = callback;
- }
- self.model = sourceModel;
- if (sourceModel.exquisiteFlag) {
- self.qualityMusicTag.hidden = NO;
- self.qualityTagWidth.constant = 14.0f;
- }
- else {
- self.qualityMusicTag.hidden = NO;
- self.qualityTagWidth.constant = 0.0f;
- }
-
- if (sourceModel.albumNums > 0) {
- self.albumTag.hidden = NO;
- self.albumTagWidth.constant = 15.0f;
- self.albumTagLeft.constant = 5.0f;
- }
- else {
- self.albumTag.hidden = YES;
- self.albumTagWidth.constant = 0.0f;
- self.albumTagLeft.constant = 0.0f;
- }
-
- if ([NSString isEmptyString:sourceModel.musicSheetName]) {
- self.songNameLabel.text = @"";
- }
- else {
- NSString *musicName = @"";
- if (sourceModel.musicSheetName.length > 10) {
- musicName = [NSString stringWithFormat:@"%@...",[sourceModel.musicSheetName substringToIndex:9]];
- }
- else {
- musicName = sourceModel.musicSheetName;
- }
- self.songNameLabel.text = musicName;
- }
- if ([NSString isEmptyString:sourceModel.composer]) {
- self.songAuth.text = @"";
- }
- else {
- NSString *songAuth = @"";
- if (sourceModel.composer.length > 4) {
- songAuth = [NSString stringWithFormat:@"-%@...",[sourceModel.composer substringToIndex:4]];
- }
- else {
- songAuth = [NSString stringWithFormat:@"-%@",sourceModel.composer];
- }
- self.songAuth.text = songAuth;
- }
- NSArray *tagArray = nil;
- if (![NSString isEmptyString:sourceModel.subjectNames]) {
- tagArray = [sourceModel.subjectNames componentsSeparatedByString:@","];
- }
-
- NSString *owner = @"";
- if ([NSString isEmptyString:sourceModel.addName]) {
- owner = [NSString stringWithFormat:@"上传者:游客%.0f",sourceModel.userId];
- }
- else {
- owner = sourceModel.addName;
- if (sourceModel.addName.length > 4) {
- owner = [NSString stringWithFormat:@"上传者:%@...",[sourceModel.addName substringToIndex:4]];
- }
- else {
- owner = [NSString stringWithFormat:@"上传者:%@",sourceModel.addName];
- }
- }
- self.uploadName.text = owner;
- CGFloat maxWidth = [self getTagViewMaxWidth:owner];
- [self configTagViewWithTagArray:tagArray maxWidth:maxWidth];
- self.lineView.hidden = hideLineView;
- }
- - (CGFloat)getWidthWithTypeCount:(NSInteger)count singleWidth:(CGFloat)singleWidth space:(CGFloat)space {
- if (count == 1) {
- return singleWidth;
- }
- else {
- return singleWidth + (singleWidth + space) * (count - 1);
- }
- }
- - (CGFloat)getTagViewMaxWidth:(NSString *)teacherName {
- CGFloat width = [self getStringWidthInLabel:teacherName font:[UIFont systemFontOfSize:12.0f]];
- return KPortraitWidth - 48 - width - 12;
- }
- - (void)configTagViewWithTagArray:(NSArray *)tagArray maxWidth:(CGFloat)maxWidth {
- [self.tagView removeAllSubViews];
- 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:10.0f]];
- CGFloat viewWidth = labelWidth + 10;
- if (xSpace + viewWidth > width) {
- return;
- }
- CGRect frame = CGRectMake(xSpace, 0, viewWidth, 16.0f);
- [self createTagLabelViewWithName:tagString frame:frame];
- xSpace += (viewWidth + 5);
- }
- }
- - (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 = HexRGB(0xEFFBF9);
- bgView.layer.cornerRadius = 8.0f;
- [self.tagView addSubview:bgView];
-
- UILabel *tagLabel = [[UILabel alloc] init];
- tagLabel.text = name;
- tagLabel.textColor = THEMECOLOR;
- tagLabel.font = [UIFont systemFontOfSize:10.0f];
- tagLabel.textAlignment = NSTextAlignmentCenter;
- [bgView addSubview:tagLabel];
- [tagLabel mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.mas_equalTo(bgView.mas_left).offset(5);
- make.right.mas_equalTo(bgView.mas_right).offset(-5);
- make.top.bottom.mas_equalTo(bgView);
- }];
- }
- - (IBAction)buttonAction:(id)sender {
- if (self.callback) {
- NSString *songId = [NSString stringWithFormat:@"%.0f",self.model.internalBaseClassIdentifier];
- self.callback(songId);
- }
- }
- /*
- // Only override drawRect: if you perform custom drawing.
- // An empty implementation adversely affects performance during animation.
- - (void)drawRect:(CGRect)rect {
- // Drawing code
- }
- */
- @end
|