You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
8.2 KiB

1 month ago
  1. //
  2. // StockInfoCardView.m
  3. // HC
  4. //
  5. // Created by huilinLi on 2025/12/1.
  6. //
  7. #import "StockInfoCardView.h"
  8. @interface StockInfoCardView ()
  9. // 在实现文件中重新定义为 readwrite 方便内部赋值
  10. @property (nonatomic, strong, readwrite) UILabel *mainPriceLabel;
  11. @property (nonatomic, strong, readwrite) UILabel *changePriceLabel;
  12. @property (nonatomic, strong, readwrite) UILabel *changePercentLabel;
  13. @property (nonatomic, strong, readwrite) UILabel *highValueLabel;
  14. @property (nonatomic, strong, readwrite) UILabel *openValueLabel;
  15. @property (nonatomic, strong, readwrite) UILabel *volumeValueLabel;
  16. @property (nonatomic, strong, readwrite) UILabel *lowValueLabel;
  17. @property (nonatomic, strong, readwrite) UILabel *turnoverRateLabel;
  18. @property (nonatomic, strong, readwrite) UILabel *amountLabel;
  19. @end
  20. @implementation StockInfoCardView
  21. - (instancetype)initWithFrame:(CGRect)frame {
  22. self = [super initWithFrame:frame];
  23. if (self) {
  24. // 通常在 init 之后立即调用 setupView
  25. [self setupView];
  26. }
  27. return self;
  28. }
  29. - (void)setupView {
  30. self.backgroundColor = [UIColor colorWithRed:0x20/255.0 green:0x20/255.0 blue:0x20/255.0 alpha:1];
  31. // 1. 创建左上角价格和变化标签
  32. _mainPriceLabel = [self createLabelWithFontSize:15 textColor:[UIColor redColor]];
  33. _changePriceLabel = [self createLabelWithFontSize:8 textColor:[UIColor redColor]];
  34. _changePercentLabel = [self createLabelWithFontSize:8 textColor:[UIColor redColor]];
  35. // 2. 创建右侧信息标签(名称和值)
  36. // 标签名称(保持原有的布局比例)
  37. UILabel *cardLabelHeight = [self createLabelWithText:@"高" fontSize:15 color:[UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1]];
  38. UILabel *cardLabelStart = [self createLabelWithText:@"开" fontSize:15 color:[UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1]];
  39. UILabel *cardLabelVolume = [self createLabelWithText:@"量" fontSize:15 color:[UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1]];
  40. UILabel *cardLabelLow = [self createLabelWithText:@"低" fontSize:15 color:[UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1]];
  41. UILabel *cardLabelChange = [self createLabelWithText:@"换" fontSize:15 color:[UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1]];
  42. UILabel *cardLabelQuota = [self createLabelWithText:@"额" fontSize:15 color:[UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1]];
  43. // 标签值(初始内容与原代码相同)
  44. _highValueLabel = [self createLabelWithText:@"1617.260" fontSize:12 color:[UIColor redColor]];
  45. _openValueLabel = [self createLabelWithText:@"1616.750" fontSize:12 color:[UIColor redColor]];
  46. _volumeValueLabel = [self createLabelWithText:@"1.511亿" fontSize:12 color:[UIColor whiteColor]];
  47. _lowValueLabel = [self createLabelWithText:@"1608.850" fontSize:12 color:[UIColor whiteColor]];
  48. _turnoverRateLabel = [self createLabelWithText:@"--" fontSize:12 color:[UIColor whiteColor]];
  49. _amountLabel = [self createLabelWithText:@"--" fontSize:12 color:[UIColor whiteColor]];
  50. // 将所有子视图添加到 CardView
  51. NSArray *allSubviews = @[_mainPriceLabel, _changePriceLabel, _changePercentLabel, cardLabelHeight, cardLabelStart, cardLabelVolume, cardLabelLow, cardLabelChange, cardLabelQuota, _highValueLabel, _openValueLabel, _volumeValueLabel, _lowValueLabel, _turnoverRateLabel, _amountLabel];
  52. for (UIView *view in allSubviews) {
  53. [self addSubview:view];
  54. }
  55. // 3. 布局约束 (使用 self 代替原来的 _cardContainer)
  56. [NSLayoutConstraint activateConstraints:@[
  57. // 左侧主要信息
  58. [_mainPriceLabel.topAnchor constraintEqualToAnchor:self.topAnchor constant:25],
  59. [_mainPriceLabel.leadingAnchor constraintEqualToAnchor:self.leadingAnchor constant:10],
  60. [_changePriceLabel.topAnchor constraintEqualToAnchor:self.topAnchor constant:50],
  61. [_changePriceLabel.leadingAnchor constraintEqualToAnchor:self.leadingAnchor constant: 8],
  62. [_changePercentLabel.topAnchor constraintEqualToAnchor:self.topAnchor constant:50],
  63. [_changePercentLabel.leadingAnchor constraintEqualToAnchor:self.leadingAnchor constant: 45],
  64. // 相对布局的 Label(高,, 量)
  65. [cardLabelHeight.bottomAnchor constraintEqualToAnchor:self.bottomAnchor constant:-75],
  66. [cardLabelStart.bottomAnchor constraintEqualToAnchor:cardLabelHeight.bottomAnchor],
  67. [cardLabelVolume.bottomAnchor constraintEqualToAnchor:cardLabelHeight.bottomAnchor],
  68. // 相对布局的 Label(低,, 额)
  69. [cardLabelLow.topAnchor constraintEqualToAnchor:self.bottomAnchor constant:-50],
  70. [cardLabelLow.leadingAnchor constraintEqualToAnchor:cardLabelHeight.leadingAnchor],
  71. [cardLabelChange.topAnchor constraintEqualToAnchor:self.bottomAnchor constant:-50],
  72. [cardLabelChange.leadingAnchor constraintEqualToAnchor:cardLabelStart.leadingAnchor],
  73. [cardLabelQuota.topAnchor constraintEqualToAnchor:self.bottomAnchor constant:-50],
  74. [cardLabelQuota.leadingAnchor constraintEqualToAnchor:cardLabelVolume.leadingAnchor],
  75. // 相对布局的 Value(高,, 量)
  76. [_highValueLabel.bottomAnchor constraintEqualToAnchor:self.topAnchor constant:45],
  77. [_highValueLabel.leadingAnchor constraintEqualToAnchor:cardLabelHeight.leadingAnchor],
  78. [_openValueLabel.bottomAnchor constraintEqualToAnchor:_highValueLabel.bottomAnchor],
  79. [_openValueLabel.leadingAnchor constraintEqualToAnchor:cardLabelStart.leadingAnchor],
  80. [_volumeValueLabel.bottomAnchor constraintEqualToAnchor:_highValueLabel.bottomAnchor],
  81. [_volumeValueLabel.leadingAnchor constraintEqualToAnchor:cardLabelVolume.leadingAnchor],
  82. // 相对布局的 Value(低,, 额)
  83. [_lowValueLabel.bottomAnchor constraintEqualToAnchor:self.bottomAnchor constant:-10],
  84. [_lowValueLabel.leadingAnchor constraintEqualToAnchor:cardLabelLow.leadingAnchor],
  85. [_turnoverRateLabel.bottomAnchor constraintEqualToAnchor:_lowValueLabel.bottomAnchor],
  86. [_turnoverRateLabel.leadingAnchor constraintEqualToAnchor:cardLabelChange.leadingAnchor],
  87. [_amountLabel.bottomAnchor constraintEqualToAnchor:_lowValueLabel.bottomAnchor],
  88. [_amountLabel.leadingAnchor constraintEqualToAnchor:cardLabelVolume.leadingAnchor]
  89. ]];
  90. // 关键:基于父视图宽度比例的约束(使用 self 代替 _cardContainer
  91. [NSLayoutConstraint constraintWithItem:cardLabelHeight
  92. attribute:NSLayoutAttributeLeading
  93. relatedBy:NSLayoutRelationEqual
  94. toItem:self
  95. attribute:NSLayoutAttributeTrailing
  96. multiplier:0.25
  97. constant:0].active = YES;
  98. [NSLayoutConstraint constraintWithItem:cardLabelStart
  99. attribute:NSLayoutAttributeLeading
  100. relatedBy:NSLayoutRelationEqual
  101. toItem:self
  102. attribute:NSLayoutAttributeTrailing
  103. multiplier:0.5
  104. constant:0].active = YES;
  105. [NSLayoutConstraint constraintWithItem:cardLabelVolume
  106. attribute:NSLayoutAttributeLeading
  107. relatedBy:NSLayoutRelationEqual
  108. toItem:self
  109. attribute:NSLayoutAttributeTrailing
  110. multiplier:0.75
  111. constant:0].active = YES;
  112. }
  113. // 辅助方法:创建 UILabel
  114. - (UILabel *)createLabelWithFontSize:(CGFloat)size textColor:(UIColor *)color {
  115. UILabel *label = [[UILabel alloc] init];
  116. label.textColor = color;
  117. label.font = [UIFont systemFontOfSize:size];
  118. label.translatesAutoresizingMaskIntoConstraints = NO;
  119. return label;
  120. }
  121. // 辅助方法:创建带初始文本的 UILabel
  122. - (UILabel *)createLabelWithText:(NSString *)text fontSize:(CGFloat)size color:(UIColor *)color {
  123. UILabel *label = [self createLabelWithFontSize:size textColor:color];
  124. label.text = text;
  125. return label;
  126. }
  127. @end