TXShareMusicCellContentView.m 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. //
  2. // TXShareMusicCellContentView.m
  3. // TUIChat
  4. //
  5. // Created by 王智 on 2023/8/10.
  6. //
  7. #import "TXShareMusicCellContentView.h"
  8. #import "Masonry.h"
  9. #import "UIImageView+WebCache.h"
  10. #import "TUIDefine.h"
  11. #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]
  12. @interface TXShareMusicCellContentView ()
  13. @property (nonatomic, strong) UILabel *songName;
  14. @property (nonatomic, strong) UILabel *songAuth;
  15. @property (nonatomic, strong) UIImageView *uploaderLogo;
  16. @property (nonatomic, strong) UILabel *uploaderName;
  17. @property (nonatomic, strong) UIImageView *typeImage;
  18. @property (nonatomic, strong) UIView *tagView;
  19. @end
  20. @implementation TXShareMusicCellContentView
  21. + (instancetype)shareInstance {
  22. return [[TXShareMusicCellContentView alloc] init];
  23. }
  24. - (instancetype)init {
  25. self = [super init];
  26. if (self) {
  27. [self configUI];
  28. }
  29. return self;
  30. }
  31. - (void)configUI {
  32. self.layer.cornerRadius = 10.0f;
  33. self.backgroundColor = [UIColor whiteColor];
  34. UIImageView *musicLogo = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"music_logo"]];
  35. [self addSubview:musicLogo];
  36. [musicLogo mas_makeConstraints:^(MASConstraintMaker *make) {
  37. make.top.mas_equalTo(self.mas_top).offset(10);
  38. make.left.mas_equalTo(self.mas_left).offset(11);
  39. make.width.mas_equalTo(41);
  40. make.height.mas_equalTo(40);
  41. }];
  42. [self addSubview:self.songName];
  43. [self.songName mas_makeConstraints:^(MASConstraintMaker *make) {
  44. make.top.mas_equalTo(musicLogo.mas_top);
  45. make.height.mas_equalTo(20);
  46. make.left.mas_equalTo(musicLogo.mas_right).offset(6);
  47. }];
  48. [self addSubview:self.typeImage];
  49. [self.typeImage mas_makeConstraints:^(MASConstraintMaker *make) {
  50. make.left.mas_equalTo(self.songName.mas_right).offset(4);
  51. make.width.mas_equalTo(55);
  52. make.height.mas_equalTo(22);
  53. make.centerY.mas_equalTo(self.songName.mas_centerY);
  54. make.right.mas_lessThanOrEqualTo(self.mas_right).offset(-12);
  55. }];
  56. UIView *lineView = [[UIView alloc] init];
  57. lineView.backgroundColor = TXHexRGB(0xF2F2F2);
  58. [self addSubview:lineView];
  59. [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
  60. make.top.mas_equalTo(musicLogo.mas_bottom).offset(10);
  61. make.left.mas_equalTo(self.mas_left).offset(10);
  62. make.right.mas_equalTo(self.mas_right).offset(-10);
  63. make.height.mas_equalTo(1);
  64. }];
  65. [self addSubview:self.uploaderLogo];
  66. [self.uploaderLogo mas_makeConstraints:^(MASConstraintMaker *make) {
  67. make.left.mas_equalTo(self.mas_left).offset(18);
  68. make.top.mas_equalTo(lineView.mas_top).offset(8);
  69. make.width.height.mas_equalTo(20);
  70. }];
  71. [self addSubview:self.songAuth];
  72. [self.songAuth mas_makeConstraints:^(MASConstraintMaker *make) {
  73. make.left.mas_equalTo(self.songName.mas_left);
  74. make.top.mas_equalTo(self.typeImage.mas_bottom).offset(2);
  75. make.height.mas_equalTo(17);
  76. make.right.mas_equalTo(self.mas_right).offset(-10);
  77. }];
  78. [self addSubview:self.uploaderName];
  79. [self.uploaderName mas_makeConstraints:^(MASConstraintMaker *make) {
  80. make.left.mas_equalTo(self.uploaderLogo.mas_right).offset(7);
  81. make.centerY.mas_equalTo(self.uploaderLogo.mas_centerY);
  82. make.height.mas_equalTo(15);
  83. }];
  84. [self addSubview:self.tagView];
  85. [self.tagView mas_makeConstraints:^(MASConstraintMaker *make) {
  86. make.centerY.mas_equalTo(self.uploaderName.mas_centerY);
  87. make.left.mas_equalTo(self.uploaderName.mas_right).offset(8);
  88. make.right.mas_equalTo(self.mas_right).offset(-10);
  89. make.height.mas_equalTo(16);
  90. }];
  91. }
  92. - (void)configWithSongName:(NSString *)songName type:(NSString *)type authName:(NSString *)authName sendAvatar:(NSString *)sendAvatar sendName:(NSString *)sendName userId:(NSString *)userId tags:(NSString *)tags {
  93. self.songName.text = [self returnNoNullStringWithString:songName];
  94. self.songAuth.text = [self returnNoNullStringWithString:authName];
  95. NSArray *tagArray = [tags componentsSeparatedByString:@","];
  96. NSString *owner = @"";
  97. if ([self isEmptyString:sendName]) {
  98. owner = [NSString stringWithFormat:@"游客%@",userId];
  99. }
  100. else {
  101. owner = sendName;
  102. }
  103. CGFloat maxWidth = [self getTagViewMaxWidth:owner];
  104. [self configTagViewWithTagArray:tagArray maxWidth:maxWidth];
  105. NSString *typeImgName = @"";
  106. if ([type isEqualToString:@"VIP"]) {
  107. typeImgName = @"music_vip";
  108. }
  109. else if ([type isEqualToString:@"CHARGE"]) {
  110. typeImgName = @"music_order";
  111. }
  112. else {
  113. typeImgName = @"music_free";
  114. }
  115. [self.typeImage setImage:[UIImage imageNamed:typeImgName]];
  116. self.uploaderName.text = [self returnNoNullStringWithString:owner];
  117. [self.uploaderLogo sd_setImageWithURL:[NSURL URLWithString:[self getUrlEndcodeString:sendAvatar]] placeholderImage:[UIImage imageNamed:@"user_default_avatal"]];
  118. }
  119. - (CGFloat)getTagViewMaxWidth:(NSString *)teacherName {
  120. CGFloat width = [self getStringWidthInLabel:teacherName font:[UIFont systemFontOfSize:12.0f]];
  121. return 260 - 45 - 10 - 14 - width - 8;
  122. }
  123. - (void)configTagViewWithTagArray:(NSArray *)tagArray maxWidth:(CGFloat)maxWidth {
  124. [self removeAllSubViews:self.tagView];
  125. CGFloat width = maxWidth;
  126. CGFloat xSpace = 0.0f;
  127. for (NSInteger i = 0; i < tagArray.count; i++) {
  128. NSString *tagString = tagArray[i];
  129. CGFloat labelWidth = [self getStringWidthInLabel:tagString font:[UIFont systemFontOfSize:11.0f]];
  130. CGFloat viewWidth = labelWidth + 8;
  131. if (xSpace + viewWidth > width) {
  132. return;
  133. }
  134. CGRect frame = CGRectMake(xSpace, 0, viewWidth, 16.0f);
  135. [self createTagLabelViewWithName:tagString frame:frame];
  136. xSpace += (viewWidth + 6);
  137. }
  138. }
  139. - (CGFloat)getStringWidthInLabel:(NSString *)tagString font:(UIFont *)font {
  140. CGFloat width = [tagString boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 16.0f) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:font} context:nil].size.width+1;
  141. return width;
  142. }
  143. - (void)createTagLabelViewWithName:(NSString *)name frame:(CGRect)frame {
  144. UIView *bgView = [[UIView alloc] initWithFrame:frame];
  145. bgView.backgroundColor = TXHexRGB(0xfff1de);
  146. bgView.layer.cornerRadius = 4.0f;
  147. [self.tagView addSubview:bgView];
  148. UILabel *tagLabel = [[UILabel alloc] init];
  149. tagLabel.text = name;
  150. tagLabel.textColor = TXHexRGB(0xff8c00);
  151. tagLabel.font = [UIFont systemFontOfSize:11.0f];
  152. tagLabel.textAlignment = NSTextAlignmentCenter;
  153. [bgView addSubview:tagLabel];
  154. [tagLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  155. make.left.mas_equalTo(bgView.mas_left).offset(4);
  156. make.right.mas_equalTo(bgView.mas_right).offset(-4);
  157. make.top.bottom.mas_equalTo(bgView);
  158. }];
  159. }
  160. - (NSString *)returnNoNullStringWithString:(NSString *)string {
  161. if (string == nil || string.length == 0) {
  162. return @"";
  163. }
  164. return string;
  165. }
  166. - (NSString *)getUrlEndcodeString:(NSString *)string {
  167. return [string stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];;
  168. }
  169. - (void)removeAllSubViews:(UIView *)view {
  170. while (view.subviews.count) {
  171. [view.subviews.lastObject removeFromSuperview];
  172. }
  173. }
  174. - (BOOL)isEmptyString:(NSString *)str {
  175. if (str == nil || str.length == 0) {
  176. return YES;
  177. }
  178. return NO;
  179. }
  180. #pragma mark ----- lazying
  181. - (UILabel *)songName {
  182. if (!_songName) {
  183. _songName = [[UILabel alloc] init];
  184. _songName.textColor = TXHexRGB(0x333333);
  185. _songName.font = [UIFont systemFontOfSize:14.0f weight:UIFontWeightSemibold];
  186. }
  187. return _songName;
  188. }
  189. - (UILabel *)songAuth {
  190. if (!_songAuth) {
  191. _songAuth = [[UILabel alloc] init];
  192. _songAuth.textColor = TXHexRGB(0x6A6A6A);
  193. _songAuth.font = [UIFont systemFontOfSize:12.0f];
  194. }
  195. return _songAuth;
  196. }
  197. - (UIImageView *)uploaderLogo {
  198. if (!_uploaderLogo) {
  199. _uploaderLogo = [[UIImageView alloc] init];
  200. }
  201. return _uploaderLogo;
  202. }
  203. - (UILabel *)uploaderName {
  204. if (!_uploaderName) {
  205. _uploaderName = [[UILabel alloc] init];
  206. _uploaderName.textColor = TXHexRGB(0x333333);
  207. _uploaderName.font = [UIFont systemFontOfSize:12.0f];
  208. }
  209. return _uploaderName;
  210. }
  211. - (UIImageView *)typeImage {
  212. if (!_typeImage) {
  213. _typeImage = [[UIImageView alloc] init];
  214. }
  215. return _typeImage;
  216. }
  217. - (UIView *)tagView {
  218. if (!_tagView) {
  219. _tagView = [[UIView alloc] init];
  220. }
  221. return _tagView;
  222. }
  223. /*
  224. // Only override drawRect: if you perform custom drawing.
  225. // An empty implementation adversely affects performance during animation.
  226. - (void)drawRect:(CGRect)rect {
  227. // Drawing code
  228. }
  229. */
  230. @end