objectivec
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface OrderAfterPeriodSelectNumView : UIView
//左边标题
@property (nonatomic,strong) UILabel *titleLab;
//数量
@property (nonatomic,strong) UILabel *numLab;
@end
objectivec
#import "OrderAfterPeriodSelectNumView.h"
@interface OrderAfterPeriodSelectNumView()
@property (nonatomic, assign) CGFloat titleLabWidth;
@end
@implementation OrderAfterPeriodSelectNumView
#pragma mark - 原始初始化方法
// 1、首先调用init方法
- (instancetype)init{
if (self = [super init]) {
[self initUI];
}
return self;
}
// 2、然后调用initWithFrame方法
- (instancetype)initWithFrame:(CGRect)frame{
if (self =[super initWithFrame:frame]) {
[self initUI];
}
return self;
}
// 初始化UI控件
- (void) initUI{
UILabel *titleLab = [[UILabel alloc] init];
titleLab.textColor = [UIColor colorWithHexString:@"333333"];
titleLab.font = [UIFont systemFontOfSize:14];
titleLab.textAlignment = NSTextAlignmentLeft;
titleLab.text = @"退款数量";
[self addSubview:titleLab];
_titleLab = titleLab;
UILabel *numLab = [[UILabel alloc] init];
titleLab.textColor = [UIColor colorWithHexString:@"333333"];
numLab.font = [UIFont systemFontOfSize:14];
numLab.textAlignment = NSTextAlignmentRight;
numLab.text = @"0";
[self addSubview:numLab];
_numLab = numLab;
//获取字符串的宽高
CGRect rect = [titleLab.text boundingRectWithSize:CGSizeMake(SCREENWIDTH, MAXFLOAT) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14]} context:nil];
_titleLabWidth = rect.size.width;
}
// 设置UI控件的frame
- (void)layoutSubviews{
[super layoutSubviews];
// 1.获取当前控件的尺寸
CGFloat width = self.frame.size.width;
CGFloat height = self.frame.size.height;
// 2.设置子控件的frame
self.titleLab.frame = CGRectMake(16, 0, self.titleLabWidth, height);
self.numLab.frame = CGRectMake(16+self.titleLabWidth, 0, width - (16+self.titleLabWidth+16), height);
}
@end
添加到UIStackView
objectivec
OrderAfterPeriodSelectNumView *numView = [[OrderAfterPeriodSelectNumView alloc] init];
[numView mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.equalTo(@(50));
}];
self.numView = numView;
[self.stackView addArrangedSubview:numView];