ShareMusicCellContentView.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //
  2. // ShareMusicCellContentView.m
  3. // KulexiuForTeacher
  4. //
  5. // Created by 王智 on 2022/6/8.
  6. //
  7. #import "ShareMusicCellContentView.h"
  8. @interface ShareMusicCellContentView ()
  9. @property (weak, nonatomic) IBOutlet UILabel *songName;
  10. @property (weak, nonatomic) IBOutlet UILabel *songAuth;
  11. @property (weak, nonatomic) IBOutlet UIImageView *uploaderLogo;
  12. @property (weak, nonatomic) IBOutlet UILabel *uploaderName;
  13. @property (weak, nonatomic) IBOutlet UIImageView *typeImage;
  14. @property (weak, nonatomic) IBOutlet UIView *tagView;
  15. @property (weak, nonatomic) IBOutlet UIImageView *teacher_info;
  16. @end
  17. @implementation ShareMusicCellContentView
  18. + (instancetype)shareInstance {
  19. ShareMusicCellContentView *view = [[[NSBundle mainBundle] loadNibNamed:@"ShareMusicCellContentView" owner:nil options:nil] firstObject];
  20. return view;
  21. }
  22. - (void)configWithSongName:(NSString *)songName type:(NSString *)type authName:(NSString *)authName sendAvatar:(NSString *)sendAvatar sendName:(NSString *)sendName userId:(NSString *)userId tags:(NSString *)tags {
  23. self.songName.text = [NSString returnNoNullStringWithString:songName];
  24. self.songAuth.text = [NSString returnNoNullStringWithString:authName];
  25. NSArray *tagArray = [tags componentsSeparatedByString:@","];
  26. NSString *owner = @"";
  27. if ([NSString isEmptyString:sendName]) {
  28. owner = [NSString stringWithFormat:@"游客%@",userId];
  29. }
  30. else {
  31. owner = sendName;
  32. }
  33. CGFloat maxWidth = [self getTagViewMaxWidth:owner];
  34. [self configTagViewWithTagArray:tagArray maxWidth:maxWidth];
  35. NSString *typeImgName = @"";
  36. if ([type isEqualToString:@"VIP"]) {
  37. typeImgName = @"music_vip";
  38. }
  39. else if ([type isEqualToString:@"CHARGE"]) {
  40. typeImgName = @"music_order";
  41. }
  42. else {
  43. typeImgName = @"music_free";
  44. }
  45. [self.typeImage setImage:[UIImage imageNamed:typeImgName]];
  46. self.uploaderName.text = [NSString returnNoNullStringWithString:owner];
  47. [self.uploaderLogo sd_setImageWithURL:[NSURL URLWithString:[sendAvatar getUrlEndcodeString]] placeholderImage:[UIImage imageNamed:USERDEFAULT_LOGO]];
  48. }
  49. - (CGFloat)getTagViewMaxWidth:(NSString *)teacherName {
  50. CGFloat width = [self getStringWidthInLabel:teacherName font:[UIFont systemFontOfSize:12.0f]];
  51. return 260 - 45 - 10 - 14 - width - 8;
  52. }
  53. - (void)configTagViewWithTagArray:(NSArray *)tagArray maxWidth:(CGFloat)maxWidth {
  54. [self.tagView removeAllSubViews];
  55. CGFloat width = maxWidth;
  56. CGFloat xSpace = 0.0f;
  57. for (NSInteger i = 0; i < tagArray.count; i++) {
  58. NSString *tagString = tagArray[i];
  59. CGFloat labelWidth = [self getStringWidthInLabel:tagString font:[UIFont systemFontOfSize:11.0f]];
  60. CGFloat viewWidth = labelWidth + 8;
  61. if (xSpace + viewWidth > width) {
  62. return;
  63. }
  64. CGRect frame = CGRectMake(xSpace, 0, viewWidth, 16.0f);
  65. [self createTagLabelViewWithName:tagString frame:frame];
  66. xSpace += (viewWidth + 6);
  67. }
  68. }
  69. - (CGFloat)getStringWidthInLabel:(NSString *)tagString font:(UIFont *)font {
  70. CGFloat width = [tagString boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 16.0f) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:font} context:nil].size.width+1;
  71. return width;
  72. }
  73. - (void)createTagLabelViewWithName:(NSString *)name frame:(CGRect)frame {
  74. UIView *bgView = [[UIView alloc] initWithFrame:frame];
  75. bgView.backgroundColor = HexRGB(0xfff1de);
  76. bgView.layer.cornerRadius = 4.0f;
  77. [self.tagView addSubview:bgView];
  78. UILabel *tagLabel = [[UILabel alloc] init];
  79. tagLabel.text = name;
  80. tagLabel.textColor = HexRGB(0xff8c00);
  81. tagLabel.font = [UIFont systemFontOfSize:11.0f];
  82. tagLabel.textAlignment = NSTextAlignmentCenter;
  83. [bgView addSubview:tagLabel];
  84. [tagLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  85. make.left.mas_equalTo(bgView.mas_left).offset(4);
  86. make.right.mas_equalTo(bgView.mas_right).offset(-4);
  87. make.top.bottom.mas_equalTo(bgView);
  88. }];
  89. }
  90. /*
  91. // Only override drawRect: if you perform custom drawing.
  92. // An empty implementation adversely affects performance during animation.
  93. - (void)drawRect:(CGRect)rect {
  94. // Drawing code
  95. }
  96. */
  97. @end