MusicChooseCell.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // MusicChooseCell.m
  3. // KulexiuForTeacher
  4. //
  5. // Created by 王智 on 2022/6/8.
  6. //
  7. #import "MusicChooseCell.h"
  8. @interface MusicChooseCell ()
  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 UIView *bgView;
  16. @end
  17. @implementation MusicChooseCell
  18. - (void)awakeFromNib {
  19. [super awakeFromNib];
  20. // Initialization code
  21. self.selectionStyle = UITableViewCellSelectionStyleNone;
  22. }
  23. - (void)configWithMessage:(MusicMessageModel *)songMessage {
  24. self.songName.text = [NSString returnNoNullStringWithString:songMessage.musicSheetName];
  25. self.songAuth.text = [NSString returnNoNullStringWithString:songMessage.composer];
  26. NSArray *tagArray = [songMessage.musicTagNames componentsSeparatedByString:@","];
  27. NSString *owner = @"";
  28. if ([NSString isEmptyString:songMessage.addName]) {
  29. owner = [NSString stringWithFormat:@"游客%.0f",songMessage.userId];
  30. }
  31. else {
  32. owner = songMessage.addName;
  33. }
  34. CGFloat maxWidth = [self getTagViewMaxWidth:owner];
  35. [self configTagViewWithTagArray:tagArray maxWidth:maxWidth];
  36. NSString *typeImgName = @"";
  37. if ([songMessage.chargeType isEqualToString:@"VIP"]) {
  38. typeImgName = @"music_vip";
  39. }
  40. else if ([songMessage.chargeType isEqualToString:@"CHARGE"]) {
  41. typeImgName = @"music_order";
  42. }
  43. else {
  44. typeImgName = @"music_free";
  45. }
  46. [self.typeImage setImage:[UIImage imageNamed:typeImgName]];
  47. self.uploaderName.text = [NSString returnNoNullStringWithString:owner];
  48. [self.uploaderLogo sd_setImageWithURL:[NSURL URLWithString:[songMessage.addUserAvatar getUrlEndcodeString]] placeholderImage:[UIImage imageNamed:USERDEFAULT_LOGO]];
  49. if (songMessage.isChoose) {
  50. self.bgView.layer.borderColor = THEMECOLOR.CGColor;
  51. self.bgView.layer.borderWidth = 1.0f;
  52. }
  53. else {
  54. self.bgView.layer.borderColor = [UIColor whiteColor].CGColor;
  55. self.bgView.layer.borderWidth = 1.0f;
  56. }
  57. }
  58. - (CGFloat)getTagViewMaxWidth:(NSString *)teacherName {
  59. CGFloat width = [self getStringWidthInLabel:teacherName font:[UIFont systemFontOfSize:12.0f]];
  60. return kScreenWidth - 45 - 10 - 14 - width - 8;
  61. }
  62. - (void)configTagViewWithTagArray:(NSArray *)tagArray maxWidth:(CGFloat)maxWidth {
  63. [self.tagView removeAllSubViews];
  64. CGFloat width = maxWidth;
  65. CGFloat xSpace = 0.0f;
  66. for (NSInteger i = 0; i < tagArray.count; i++) {
  67. NSString *tagString = tagArray[i];
  68. CGFloat labelWidth = [self getStringWidthInLabel:tagString font:[UIFont systemFontOfSize:11.0f]];
  69. CGFloat viewWidth = labelWidth + 8;
  70. if (xSpace + viewWidth > width) {
  71. return;
  72. }
  73. CGRect frame = CGRectMake(xSpace, 0, viewWidth, 16.0f);
  74. [self createTagLabelViewWithName:tagString frame:frame];
  75. xSpace += (viewWidth + 6);
  76. }
  77. }
  78. - (CGFloat)getStringWidthInLabel:(NSString *)tagString font:(UIFont *)font {
  79. CGFloat width = [tagString boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 16.0f) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:font} context:nil].size.width+1;
  80. return width;
  81. }
  82. - (void)createTagLabelViewWithName:(NSString *)name frame:(CGRect)frame {
  83. UIView *bgView = [[UIView alloc] initWithFrame:frame];
  84. bgView.backgroundColor = HexRGB(0xfff1de);
  85. bgView.layer.cornerRadius = 4.0f;
  86. [self.tagView addSubview:bgView];
  87. UILabel *tagLabel = [[UILabel alloc] init];
  88. tagLabel.text = name;
  89. tagLabel.textColor = HexRGB(0xff8c00);
  90. tagLabel.font = [UIFont systemFontOfSize:11.0f];
  91. tagLabel.textAlignment = NSTextAlignmentCenter;
  92. [bgView addSubview:tagLabel];
  93. [tagLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  94. make.left.mas_equalTo(bgView.mas_left).offset(4);
  95. make.right.mas_equalTo(bgView.mas_right).offset(-4);
  96. make.top.bottom.mas_equalTo(bgView);
  97. }];
  98. }
  99. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  100. [super setSelected:selected animated:animated];
  101. // Configure the view for the selected state
  102. }
  103. @end