MORedPacketInfoView.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //
  2. // MORedPacketInfoView.m
  3. // MiMoLive
  4. //
  5. // Created by MiMo on 2025/5/17.
  6. //
  7. #import "MORedPacketInfoView.h"
  8. @interface MORedPacketInfoView ()
  9. @property (nonatomic, strong) UIView *backgroundView;
  10. @property (nonatomic, strong) UILabel *diamondLabel;
  11. @property (nonatomic, strong) UIImageView *diamondIcon;
  12. @property (nonatomic, strong) UILabel *itemLabel;
  13. @property (nonatomic, strong) UIImageView *itemIcon;
  14. @property (nonatomic, strong) UIView *separatorView;
  15. @end
  16. @implementation MORedPacketInfoView
  17. - (instancetype)init {
  18. self = [super init];
  19. if (self) {
  20. [self setupViews];
  21. }
  22. return self;
  23. }
  24. - (void)layoutSubviews {
  25. [super layoutSubviews];
  26. [self setupGradientLayerWithView:self.backgroundView
  27. startColor:[MOTools colorWithHexString:@"#FFA289"]
  28. endColor:[MOTools colorWithHexString:@"#FDC9AD"]
  29. layerName:@"gradientLayer"
  30. startPoint:(CGPoint) { 0.5, 0 }
  31. endPoint:(CGPoint){0.5, 1}];
  32. self.backgroundView.layer.cornerRadius = 15;
  33. self.backgroundView.layer.masksToBounds = YES;
  34. }
  35. - (UILabel *)createLabelWithFontSize:(CGFloat)fontSize {
  36. UILabel *label = [[UILabel alloc] init];
  37. label.textColor = [MOTools colorWithHexString:@"#A13A00"];
  38. label.font = [MOTextTools getTheFontWithSize:fontSize AndFontName:kNormalContentBlodFontStr];
  39. return label;
  40. }
  41. - (UIImageView *)createIconWithName:(NSString *)name {
  42. UIImage *image = [UIImage imageNamed:name];
  43. UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
  44. imageView.contentMode = UIViewContentModeScaleAspectFit;
  45. return imageView;
  46. }
  47. - (void)setupViews {
  48. // 背景视图
  49. self.backgroundView = [[UIView alloc] init];
  50. self.backgroundView.backgroundColor = [UIColor colorWithRed:1.0 green:0.8 blue:0.8 alpha:1.0];
  51. self.backgroundView.layer.cornerRadius = 15;
  52. [self addSubview:self.backgroundView];
  53. // 子控件
  54. self.diamondLabel = [self createLabelWithFontSize:16.0];
  55. self.diamondIcon = [self createIconWithName:@"icon_red_list_diamond"];
  56. self.separatorView = [[UIView alloc] init];
  57. self.separatorView.backgroundColor = [MOTools colorWithHexString:@"#B93900"];
  58. self.itemLabel = [self createLabelWithFontSize:14.0];
  59. self.itemIcon = [self createIconWithName:@"icon_red_packet_small"];
  60. // 加入背景视图中
  61. [self.backgroundView addSubview:self.diamondLabel];
  62. [self.backgroundView addSubview:self.diamondIcon];
  63. [self.backgroundView addSubview:self.separatorView];
  64. [self.backgroundView addSubview:self.itemLabel];
  65. [self.backgroundView addSubview:self.itemIcon];
  66. // 使用 Masonry 布局
  67. [self.backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
  68. make.top.left.right.mas_equalTo(0);
  69. make.height.mas_equalTo(30);
  70. }];
  71. [self.diamondLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  72. make.left.equalTo(self.backgroundView.mas_left).offset(10);
  73. make.centerY.equalTo(self.backgroundView);
  74. }];
  75. [self.diamondIcon mas_makeConstraints:^(MASConstraintMaker *make) {
  76. make.left.equalTo(self.diamondLabel.mas_right).offset(6);
  77. make.centerY.equalTo(self.backgroundView);
  78. make.width.mas_equalTo(14);
  79. make.height.mas_equalTo(11);
  80. }];
  81. [self.separatorView mas_makeConstraints:^(MASConstraintMaker *make) {
  82. make.left.equalTo(self.diamondIcon.mas_right).offset(6);
  83. make.centerY.equalTo(self.backgroundView);
  84. make.size.mas_equalTo(CGSizeMake(1, 9));
  85. }];
  86. [self.itemLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  87. make.left.equalTo(self.separatorView.mas_right).offset(6);
  88. make.centerY.equalTo(self.backgroundView);
  89. }];
  90. [self.itemIcon mas_makeConstraints:^(MASConstraintMaker *make) {
  91. make.left.equalTo(self.itemLabel.mas_right).offset(3);
  92. make.right.equalTo(self.backgroundView.mas_right).offset(-10);
  93. make.centerY.equalTo(self.backgroundView);
  94. make.size.mas_equalTo(CGSizeMake(13, 14));
  95. }];
  96. }
  97. - (void)setDiamondCount:(NSInteger)diamondCount itemCount:(NSInteger)itemCount {
  98. self.diamondLabel.text = [NSString stringWithFormat:@"%ld", (long)diamondCount];
  99. self.itemLabel.text = [NSString stringWithFormat:@"%ld", (long)itemCount];
  100. }
  101. @end