RecentMusicView.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //
  2. // RecentMusicView.m
  3. // KulexiuForStudent
  4. //
  5. // Created by 王智 on 2022/10/18.
  6. //
  7. #import "RecentMusicView.h"
  8. #import "MusicTagView.h"
  9. @interface RecentMusicView ()
  10. @property (weak, nonatomic) IBOutlet UILabel *songNameLabel;
  11. @property (weak, nonatomic) IBOutlet UILabel *songAuth;
  12. @property (weak, nonatomic) IBOutlet UILabel *uploadName;
  13. @property (weak, nonatomic) IBOutlet UIView *tagView;
  14. @property (weak, nonatomic) IBOutlet UIView *lineView;
  15. @property (nonatomic, copy) RecentMusicDetailBlock callback;
  16. @property (nonatomic, strong) RecentPracticeModel *model;
  17. @property (weak, nonatomic) IBOutlet UIImageView *qualityMusicTag;
  18. @property (weak, nonatomic) IBOutlet NSLayoutConstraint *qualityTagWidth;
  19. @property (weak, nonatomic) IBOutlet UIImageView *albumTag;
  20. @property (weak, nonatomic) IBOutlet NSLayoutConstraint *albumTagWidth;
  21. @property (weak, nonatomic) IBOutlet NSLayoutConstraint *albumTagLeft;
  22. @end
  23. @implementation RecentMusicView
  24. + (instancetype)shareInstance {
  25. RecentMusicView *view = [[[NSBundle mainBundle] loadNibNamed:@"RecentMusicView" owner:nil options:nil] firstObject];
  26. return view;
  27. }
  28. - (void)configWithMusicModel:(RecentPracticeModel *)sourceModel hiddenLineView:(BOOL)hideLineView callback:(RecentMusicDetailBlock)callback {
  29. if (callback) {
  30. self.callback = callback;
  31. }
  32. self.model = sourceModel;
  33. if (sourceModel.exquisiteFlag) {
  34. self.qualityMusicTag.hidden = NO;
  35. self.qualityTagWidth.constant = 14.0f;
  36. }
  37. else {
  38. self.qualityMusicTag.hidden = NO;
  39. self.qualityTagWidth.constant = 0.0f;
  40. }
  41. if (sourceModel.albumNums > 0) {
  42. self.albumTag.hidden = NO;
  43. self.albumTagWidth.constant = 15.0f;
  44. self.albumTagLeft.constant = 5.0f;
  45. }
  46. else {
  47. self.albumTag.hidden = YES;
  48. self.albumTagWidth.constant = 0.0f;
  49. self.albumTagLeft.constant = 0.0f;
  50. }
  51. if ([NSString isEmptyString:sourceModel.musicSheetName]) {
  52. self.songNameLabel.text = @"";
  53. }
  54. else {
  55. NSString *musicName = @"";
  56. if (sourceModel.musicSheetName.length > 10) {
  57. musicName = [NSString stringWithFormat:@"%@...",[sourceModel.musicSheetName substringToIndex:9]];
  58. }
  59. else {
  60. musicName = sourceModel.musicSheetName;
  61. }
  62. self.songNameLabel.text = musicName;
  63. }
  64. if ([NSString isEmptyString:sourceModel.composer]) {
  65. self.songAuth.text = @"";
  66. }
  67. else {
  68. NSString *songAuth = @"";
  69. if (sourceModel.composer.length > 4) {
  70. songAuth = [NSString stringWithFormat:@"-%@...",[sourceModel.composer substringToIndex:4]];
  71. }
  72. else {
  73. songAuth = [NSString stringWithFormat:@"-%@",sourceModel.composer];
  74. }
  75. self.songAuth.text = songAuth;
  76. }
  77. NSArray *tagArray = nil;
  78. if (![NSString isEmptyString:sourceModel.subjectNames]) {
  79. tagArray = [sourceModel.subjectNames componentsSeparatedByString:@","];
  80. }
  81. NSString *owner = @"";
  82. if ([NSString isEmptyString:sourceModel.addName]) {
  83. owner = [NSString stringWithFormat:@"上传者:游客%.0f",sourceModel.userId];
  84. }
  85. else {
  86. owner = sourceModel.addName;
  87. if (sourceModel.addName.length > 4) {
  88. owner = [NSString stringWithFormat:@"上传者:%@...",[sourceModel.addName substringToIndex:4]];
  89. }
  90. else {
  91. owner = [NSString stringWithFormat:@"上传者:%@",sourceModel.addName];
  92. }
  93. }
  94. self.uploadName.text = owner;
  95. CGFloat maxWidth = [self getTagViewMaxWidth:owner];
  96. [self configTagViewWithTagArray:tagArray maxWidth:maxWidth];
  97. self.lineView.hidden = hideLineView;
  98. }
  99. - (CGFloat)getWidthWithTypeCount:(NSInteger)count singleWidth:(CGFloat)singleWidth space:(CGFloat)space {
  100. if (count == 1) {
  101. return singleWidth;
  102. }
  103. else {
  104. return singleWidth + (singleWidth + space) * (count - 1);
  105. }
  106. }
  107. - (CGFloat)getTagViewMaxWidth:(NSString *)teacherName {
  108. CGFloat width = [self getStringWidthInLabel:teacherName font:[UIFont systemFontOfSize:12.0f]];
  109. return KPortraitWidth - 48 - width - 12;
  110. }
  111. - (void)configTagViewWithTagArray:(NSArray *)tagArray maxWidth:(CGFloat)maxWidth {
  112. [self.tagView removeAllSubViews];
  113. CGFloat width = maxWidth;
  114. CGFloat xSpace = 0.0f;
  115. for (NSInteger i = 0; i < tagArray.count; i++) {
  116. NSString *tagString = tagArray[i];
  117. CGFloat labelWidth = [self getStringWidthInLabel:tagString font:[UIFont systemFontOfSize:10.0f]];
  118. CGFloat viewWidth = labelWidth + 10;
  119. if (xSpace + viewWidth > width) {
  120. return;
  121. }
  122. CGRect frame = CGRectMake(xSpace, 0, viewWidth, 16.0f);
  123. [self createTagLabelViewWithName:tagString frame:frame];
  124. xSpace += (viewWidth + 5);
  125. }
  126. }
  127. - (CGFloat)getStringWidthInLabel:(NSString *)tagString font:(UIFont *)font {
  128. CGFloat width = [tagString boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 16.0f) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:font} context:nil].size.width+1;
  129. return width;
  130. }
  131. - (void)createTagLabelViewWithName:(NSString *)name frame:(CGRect)frame {
  132. UIView *bgView = [[UIView alloc] initWithFrame:frame];
  133. bgView.backgroundColor = HexRGB(0xEFFBF9);
  134. bgView.layer.cornerRadius = 8.0f;
  135. [self.tagView addSubview:bgView];
  136. UILabel *tagLabel = [[UILabel alloc] init];
  137. tagLabel.text = name;
  138. tagLabel.textColor = THEMECOLOR;
  139. tagLabel.font = [UIFont systemFontOfSize:10.0f];
  140. tagLabel.textAlignment = NSTextAlignmentCenter;
  141. [bgView addSubview:tagLabel];
  142. [tagLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  143. make.left.mas_equalTo(bgView.mas_left).offset(5);
  144. make.right.mas_equalTo(bgView.mas_right).offset(-5);
  145. make.top.bottom.mas_equalTo(bgView);
  146. }];
  147. }
  148. - (IBAction)buttonAction:(id)sender {
  149. if (self.callback) {
  150. NSString *songId = [NSString stringWithFormat:@"%.0f",self.model.internalBaseClassIdentifier];
  151. self.callback(songId);
  152. }
  153. }
  154. /*
  155. // Only override drawRect: if you perform custom drawing.
  156. // An empty implementation adversely affects performance during animation.
  157. - (void)drawRect:(CGRect)rect {
  158. // Drawing code
  159. }
  160. */
  161. @end