左右文本,右侧可动态
2019-02-14 本文已影响17人
雪_晟
WX20190214-164756@2x.png前言
如上图,经常会有各种需求,比如右侧文本对齐方式(左右对齐),右侧文本的行数限定(可以限制无限行,也可以限制行数),整体的高度根据左右文本来确定,左侧文本限定一个宽度,等等。开工前,为了防止UI的操蛋布局不统一,还是写一个通用的类。
lineView
类
.h
/**
* 左侧间距 leftSpace
* 左右文本间距 centerSpace
* 右侧间距 rightSpace
* 右侧文本 NumberOfLines
* 右侧文本对齐方式 rightLabelTextAlignment
* 右侧文本对齐方式 rightLabelTextAlignment
* 是否限制高度 limitHeight 0,不限制 ,如果限制高度,那么居中对齐。
* 左侧文本限制是否限制宽度
*/
@interface LXBaseLineView : UIView
-(instancetype)initWithLeftSpace:(CGFloat)leftSpace centerSpace:(CGFloat)centerSpace rightSpace:(CGFloat)rightSpace rightLabelLines:(NSInteger)rightNumberOfLines rightLabelTextAlignment:(NSTextAlignment)rightLabelTextAlignment limitLeftWidth:(CGFloat)limitLeftWidth limitHeight:(CGFloat)limitHeight;
@property (nonatomic ,strong)UILabel *leftLabel;
@property (nonatomic ,strong)UILabel *rightLabel;
.m
#import "LXBaseLineView.h"
@interface LXBaseLineView()
@property (nonatomic ,assign)CGFloat leftSpace;
@property (nonatomic ,assign)CGFloat centerSpace;
@property (nonatomic ,assign)CGFloat rightSpace;
@property (nonatomic ,assign)CGFloat limitHeight;
@property (nonatomic ,assign)CGFloat limitLeftWidth;
@end
@implementation LXBaseLineView
//-(void)dealloc{
// NSLog(@"%@",self.class);
//}
-(instancetype)initWithLeftSpace:(CGFloat)leftSpace centerSpace:(CGFloat)centerSpace rightSpace:(CGFloat)rightSpace rightLabelLines:(NSInteger)rightNumberOfLines rightLabelTextAlignment:(NSTextAlignment)rightLabelTextAlignment limitLeftWidth:(CGFloat)limitLeftWidth limitHeight:(CGFloat)limitHeight{
self = [super init];
if (self) {
self.leftSpace = leftSpace;
self.centerSpace = centerSpace;
self.rightSpace = rightSpace;
self.rightLabel.numberOfLines = rightNumberOfLines;
self.rightLabel.textAlignment = rightLabelTextAlignment;
self.limitLeftWidth = limitLeftWidth;
self.limitHeight = limitHeight;
[self initilize];
}
return self;
}
-(instancetype)init{
self = [super init];
if (self) {
self.leftSpace = 15;
self.centerSpace = 15;
self.rightSpace = 15;
self.limitHeight = 0;
self.limitLeftWidth = 0;
[self initilize];
}
return self;
}
-(void)initilize{
[self addSubview:self.leftLabel];
[self addSubview:self.rightLabel];
[self.leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.leftSpace);
//如果有限制高度 那么居中对齐
if (self.limitHeight) {
make.centerY.mas_equalTo(0);
}else{
make.top.mas_equalTo(0);
}
//如果限制了左侧宽度
if (self.limitLeftWidth) {
make.width.mas_equalTo(@(self.limitLeftWidth));
}
}];
[self.rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(-self.rightSpace);
make.left.mas_equalTo(self.leftLabel.mas_right).offset(self.centerSpace);
//如果有限制高度 那么居中对齐
if (self.limitHeight) {
make.centerY.mas_equalTo(0);
make.height.lessThanOrEqualTo(@(self.limitHeight));
}else{
make.top.mas_equalTo(0);
}
}];
//当没有设置左文本最大宽度的时候左侧文本设置抗拉伸
if (!self.limitLeftWidth) {
[self.leftLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
[self.rightLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
}
[self.leftLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
[self.rightLabel setContentHuggingPriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
[self mas_makeConstraints:^(MASConstraintMaker *make) {
//如果有限制高度
if (self.limitHeight) {
make.height.mas_equalTo(self.limitHeight);
}else{
make.bottom.mas_equalTo(self.rightLabel.mas_bottom);
make.height.mas_greaterThanOrEqualTo(self.leftLabel.mas_height);
}
}];
}
//明细名称
-(UILabel *)leftLabel{
if (!_leftLabel) {
_leftLabel = [[UILabel alloc]init];
_leftLabel.textColor = [UIColor blackColor];
_leftLabel.textAlignment = NSTextAlignmentLeft;
_leftLabel.numberOfLines = 1;
_leftLabel.text = @"商品总价";
}
return _leftLabel;
}
//明细名称
-(UILabel *)rightLabel{
if (!_rightLabel) {
_rightLabel = [[UILabel alloc]init];
_rightLabel.textColor = UIColorHexFromRGB(0x666666);
_rightLabel.textAlignment = NSTextAlignmentLeft;
_rightLabel.numberOfLines = 0;
_rightLabel.text = @"¥ 60.00";
}
return _rightLabel;
}
使用
模拟一个json。
lineView.gif
self.lineView = [[LXBaseLineView alloc]initWithLeftSpace:15 centerSpace:15 rightSpace:15 rightLabelLines:0 rightLabelTextAlignment:NSTextAlignmentRight limitLeftWidth:0 limitHeight:60];
[self.view addSubview:self.lineView];
[self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.mas_equalTo(0);
make.top.mas_equalTo(100);
}];
demo地址
demo 地址:lineView