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
156 lines
8.2 KiB
//
|
|
// StockInfoCardView.m
|
|
// HC
|
|
//
|
|
// Created by huilinLi on 2025/12/1.
|
|
//
|
|
|
|
#import "StockInfoCardView.h"
|
|
|
|
@interface StockInfoCardView ()
|
|
// 在实现文件中重新定义为 readwrite 方便内部赋值
|
|
@property (nonatomic, strong, readwrite) UILabel *mainPriceLabel;
|
|
@property (nonatomic, strong, readwrite) UILabel *changePriceLabel;
|
|
@property (nonatomic, strong, readwrite) UILabel *changePercentLabel;
|
|
@property (nonatomic, strong, readwrite) UILabel *highValueLabel;
|
|
@property (nonatomic, strong, readwrite) UILabel *openValueLabel;
|
|
@property (nonatomic, strong, readwrite) UILabel *volumeValueLabel;
|
|
@property (nonatomic, strong, readwrite) UILabel *lowValueLabel;
|
|
@property (nonatomic, strong, readwrite) UILabel *turnoverRateLabel;
|
|
@property (nonatomic, strong, readwrite) UILabel *amountLabel;
|
|
@end
|
|
|
|
@implementation StockInfoCardView
|
|
|
|
- (instancetype)initWithFrame:(CGRect)frame {
|
|
self = [super initWithFrame:frame];
|
|
if (self) {
|
|
// 通常在 init 之后立即调用 setupView
|
|
[self setupView];
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (void)setupView {
|
|
self.backgroundColor = [UIColor colorWithRed:0x20/255.0 green:0x20/255.0 blue:0x20/255.0 alpha:1];
|
|
|
|
// 1. 创建左上角价格和变化标签
|
|
_mainPriceLabel = [self createLabelWithFontSize:15 textColor:[UIColor redColor]];
|
|
_changePriceLabel = [self createLabelWithFontSize:8 textColor:[UIColor redColor]];
|
|
_changePercentLabel = [self createLabelWithFontSize:8 textColor:[UIColor redColor]];
|
|
|
|
// 2. 创建右侧信息标签(名称和值)
|
|
|
|
// 标签名称(保持原有的布局比例)
|
|
UILabel *cardLabelHeight = [self createLabelWithText:@"高" fontSize:15 color:[UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1]];
|
|
UILabel *cardLabelStart = [self createLabelWithText:@"开" fontSize:15 color:[UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1]];
|
|
UILabel *cardLabelVolume = [self createLabelWithText:@"量" fontSize:15 color:[UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1]];
|
|
UILabel *cardLabelLow = [self createLabelWithText:@"低" fontSize:15 color:[UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1]];
|
|
UILabel *cardLabelChange = [self createLabelWithText:@"换" fontSize:15 color:[UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1]];
|
|
UILabel *cardLabelQuota = [self createLabelWithText:@"额" fontSize:15 color:[UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1]];
|
|
|
|
// 标签值(初始内容与原代码相同)
|
|
_highValueLabel = [self createLabelWithText:@"1617.260" fontSize:12 color:[UIColor redColor]];
|
|
_openValueLabel = [self createLabelWithText:@"1616.750" fontSize:12 color:[UIColor redColor]];
|
|
_volumeValueLabel = [self createLabelWithText:@"1.511亿" fontSize:12 color:[UIColor whiteColor]];
|
|
_lowValueLabel = [self createLabelWithText:@"1608.850" fontSize:12 color:[UIColor whiteColor]];
|
|
_turnoverRateLabel = [self createLabelWithText:@"--" fontSize:12 color:[UIColor whiteColor]];
|
|
_amountLabel = [self createLabelWithText:@"--" fontSize:12 color:[UIColor whiteColor]];
|
|
|
|
// 将所有子视图添加到 CardView
|
|
NSArray *allSubviews = @[_mainPriceLabel, _changePriceLabel, _changePercentLabel, cardLabelHeight, cardLabelStart, cardLabelVolume, cardLabelLow, cardLabelChange, cardLabelQuota, _highValueLabel, _openValueLabel, _volumeValueLabel, _lowValueLabel, _turnoverRateLabel, _amountLabel];
|
|
|
|
for (UIView *view in allSubviews) {
|
|
[self addSubview:view];
|
|
}
|
|
|
|
// 3. 布局约束 (使用 self 代替原来的 _cardContainer)
|
|
[NSLayoutConstraint activateConstraints:@[
|
|
// 左侧主要信息
|
|
[_mainPriceLabel.topAnchor constraintEqualToAnchor:self.topAnchor constant:25],
|
|
[_mainPriceLabel.leadingAnchor constraintEqualToAnchor:self.leadingAnchor constant:10],
|
|
|
|
[_changePriceLabel.topAnchor constraintEqualToAnchor:self.topAnchor constant:50],
|
|
[_changePriceLabel.leadingAnchor constraintEqualToAnchor:self.leadingAnchor constant: 8],
|
|
|
|
[_changePercentLabel.topAnchor constraintEqualToAnchor:self.topAnchor constant:50],
|
|
[_changePercentLabel.leadingAnchor constraintEqualToAnchor:self.leadingAnchor constant: 45],
|
|
|
|
// 相对布局的 Label(高, 开, 量)
|
|
[cardLabelHeight.bottomAnchor constraintEqualToAnchor:self.bottomAnchor constant:-75],
|
|
|
|
[cardLabelStart.bottomAnchor constraintEqualToAnchor:cardLabelHeight.bottomAnchor],
|
|
[cardLabelVolume.bottomAnchor constraintEqualToAnchor:cardLabelHeight.bottomAnchor],
|
|
|
|
// 相对布局的 Label(低, 换, 额)
|
|
[cardLabelLow.topAnchor constraintEqualToAnchor:self.bottomAnchor constant:-50],
|
|
[cardLabelLow.leadingAnchor constraintEqualToAnchor:cardLabelHeight.leadingAnchor],
|
|
|
|
[cardLabelChange.topAnchor constraintEqualToAnchor:self.bottomAnchor constant:-50],
|
|
[cardLabelChange.leadingAnchor constraintEqualToAnchor:cardLabelStart.leadingAnchor],
|
|
|
|
[cardLabelQuota.topAnchor constraintEqualToAnchor:self.bottomAnchor constant:-50],
|
|
[cardLabelQuota.leadingAnchor constraintEqualToAnchor:cardLabelVolume.leadingAnchor],
|
|
|
|
// 相对布局的 Value(高, 开, 量)
|
|
[_highValueLabel.bottomAnchor constraintEqualToAnchor:self.topAnchor constant:45],
|
|
[_highValueLabel.leadingAnchor constraintEqualToAnchor:cardLabelHeight.leadingAnchor],
|
|
|
|
[_openValueLabel.bottomAnchor constraintEqualToAnchor:_highValueLabel.bottomAnchor],
|
|
[_openValueLabel.leadingAnchor constraintEqualToAnchor:cardLabelStart.leadingAnchor],
|
|
|
|
[_volumeValueLabel.bottomAnchor constraintEqualToAnchor:_highValueLabel.bottomAnchor],
|
|
[_volumeValueLabel.leadingAnchor constraintEqualToAnchor:cardLabelVolume.leadingAnchor],
|
|
|
|
// 相对布局的 Value(低, 换, 额)
|
|
[_lowValueLabel.bottomAnchor constraintEqualToAnchor:self.bottomAnchor constant:-10],
|
|
[_lowValueLabel.leadingAnchor constraintEqualToAnchor:cardLabelLow.leadingAnchor],
|
|
|
|
[_turnoverRateLabel.bottomAnchor constraintEqualToAnchor:_lowValueLabel.bottomAnchor],
|
|
[_turnoverRateLabel.leadingAnchor constraintEqualToAnchor:cardLabelChange.leadingAnchor],
|
|
|
|
[_amountLabel.bottomAnchor constraintEqualToAnchor:_lowValueLabel.bottomAnchor],
|
|
[_amountLabel.leadingAnchor constraintEqualToAnchor:cardLabelVolume.leadingAnchor]
|
|
]];
|
|
|
|
// 关键:基于父视图宽度比例的约束(使用 self 代替 _cardContainer)
|
|
[NSLayoutConstraint constraintWithItem:cardLabelHeight
|
|
attribute:NSLayoutAttributeLeading
|
|
relatedBy:NSLayoutRelationEqual
|
|
toItem:self
|
|
attribute:NSLayoutAttributeTrailing
|
|
multiplier:0.25
|
|
constant:0].active = YES;
|
|
[NSLayoutConstraint constraintWithItem:cardLabelStart
|
|
attribute:NSLayoutAttributeLeading
|
|
relatedBy:NSLayoutRelationEqual
|
|
toItem:self
|
|
attribute:NSLayoutAttributeTrailing
|
|
multiplier:0.5
|
|
constant:0].active = YES;
|
|
[NSLayoutConstraint constraintWithItem:cardLabelVolume
|
|
attribute:NSLayoutAttributeLeading
|
|
relatedBy:NSLayoutRelationEqual
|
|
toItem:self
|
|
attribute:NSLayoutAttributeTrailing
|
|
multiplier:0.75
|
|
constant:0].active = YES;
|
|
}
|
|
|
|
// 辅助方法:创建 UILabel
|
|
- (UILabel *)createLabelWithFontSize:(CGFloat)size textColor:(UIColor *)color {
|
|
UILabel *label = [[UILabel alloc] init];
|
|
label.textColor = color;
|
|
label.font = [UIFont systemFontOfSize:size];
|
|
label.translatesAutoresizingMaskIntoConstraints = NO;
|
|
return label;
|
|
}
|
|
|
|
// 辅助方法:创建带初始文本的 UILabel
|
|
- (UILabel *)createLabelWithText:(NSString *)text fontSize:(CGFloat)size color:(UIColor *)color {
|
|
UILabel *label = [self createLabelWithFontSize:size textColor:color];
|
|
label.text = text;
|
|
return label;
|
|
}
|
|
|
|
@end
|