IOS 添加自动布局约束NSLayoutConstraint

发布于:2024-04-27 ⋅ 阅读:(22) ⋅ 点赞:(0)

自定义UICollectionViewCell,并添加自动布局约束。

#import "ViewChooseView.h"

#pragma mark - Cell

@interface ViewChooseViewCell : UICollectionViewCell

@property (nonatomic, strong) UILabel *showTemeLab;
@property (nonatomic, strong) UILabel *promotionTextLab;
@property (nonatomic, strong) UIView *showTimeView;
@property (nonatomic, strong) NSDictionary *data;
//马戏场次控件高度
@property (nonatomic, assign) CGFloat showTimeLabHeight;
//促销标签控件高度
@property (nonatomic, assign) CGFloat promotionLabHeight;
//马戏场次顶部边框间距约束
@property (nonatomic, strong) NSLayoutConstraint *showTimeTopConstraint;


@end

@implementation ViewChooseViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self configUI];
    }
    return self;
}

- (void)configUI {
    //计算场次UILabel的高度
    NSString *text = @"";
    self.showTimeLabHeight =[text boundingRectWithSize:CGSizeMake(CLSCREENWIDTH, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]} context:nil].size.height;
    //计算促销文案UILabel的高度
    self.promotionLabHeight =[text boundingRectWithSize:CGSizeMake(CLSCREENWIDTH, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:9]} context:nil].size.height;
    
    self.showTimeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 35)];
    self.showTimeView.backgroundColor = [UIColor colorWithHexString:@"F9F9F9" withAlpha:1];
    self.showTimeView.cornerRadius = 5;
    self.showTimeView.layer.borderColor = [UIColor colorWithHexString:@"7890FF" withAlpha:0.5].CGColor;
    self.showTimeView.layer.borderWidth = 0;
    self.showTimeView.clipsToBounds = YES;
    
    self.showTemeLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, self.showTimeLabHeight)];
//使用autolayout 设置为NO ,不是有autolayout 设置为YES;
    self.showTemeLab.translatesAutoresizingMaskIntoConstraints = NO;
    self.showTemeLab.textColor = [UIColor colorWithHexString:@"767676"];
    [self.showTemeLab setFont:[UIFont systemFontOfSize:12]];
    self.promotionTextLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, self.promotionLabHeight)];
    //使用autolayout 设置为NO ,不是有autolayout 设置为YES;
    self.promotionTextLab.translatesAutoresizingMaskIntoConstraints = NO;
    self.promotionTextLab.textColor = [UIColor colorWithHexString:@"FF5B03"];
    [self.promotionTextLab setFont:[UIFont systemFontOfSize:9]];
    [self.showTimeView addSubview:self.showTemeLab];
    [self.showTimeView addSubview:self.promotionTextLab];
    //马戏场次顶部约束间距,需要动态改变
    self.showTimeTopConstraint = [self.showTemeLab.topAnchor constraintEqualToAnchor:self.showTimeView.topAnchor constant:(35-self.showTimeLabHeight)/2];
    //添加自动布局约束
    [NSLayoutConstraint activateConstraints:@[
        [self.promotionTextLab.centerXAnchor constraintEqualToAnchor:self.showTimeView.centerXAnchor],
        [self.showTemeLab.centerXAnchor constraintEqualToAnchor:self.showTimeView.centerXAnchor],
        //[self.showTemeLab.centerYAnchor constraintEqualToAnchor:self.showTimeView.centerYAnchor],
        //[self.promotionTextLab.centerYAnchor constraintEqualToAnchor:self.showTimeView.centerYAnchor],
        self.showTimeTopConstraint,
        [self.promotionTextLab.topAnchor constraintEqualToAnchor:self.showTemeLab.bottomAnchor constant:0],
    ]];

    [self.contentView addSubview:self.showTimeView];
    
}
- (void)setData:(NSDictionary *)data{
    _data = data;
    CGFloat showTimeWidth = [[data objectForKey:@"width"] floatValue];
    CGFloat promotionTextWidth = [[data objectForKey:@"promotionTextWidth"] floatValue];
    self.showTimeView.width = showTimeWidth>promotionTextWidth?showTimeWidth:promotionTextWidth;
    self.showTemeLab.width = showTimeWidth;
    self.promotionTextLab.width = promotionTextWidth;
    
    NSString *title = [data safeStringForKey:@"title"];
    NSString *promotionText = [data safeStringForKey:@"promotionText"];
    BOOL isSelect = [[data objectForKey:@"isSelect"] boolValue];
    BOOL isEnable = [[data objectForKey:@"isEnable"] boolValue];
    self.showTemeLab.text = title;
    
    self.promotionTextLab.text = promotionText;
    if([promotionText clIsNotEmpty]){
        self.promotionTextLab.hidden = NO;
        //改变顶部约束间距
        self.showTimeTopConstraint.constant = (35-self.showTimeLabHeight-self.promotionLabHeight)/2;
    }else{
        self.promotionTextLab.hidden = YES;
        //改变顶部约束间距
        self.showTimeTopConstraint.constant = (35-self.showTimeLabHeight)/2;
    }

    self.promotionTextLab.textColor = [UIColor colorWithHexString:@"#FF5B03"];
    
    if(isEnable){
        
        if(isSelect){
            self.showTimeView.layer.borderWidth = 0.5;
            self.showTimeView.backgroundColor = [UIColor colorWithHexString:@"7890FF" withAlpha:0.02];
            self.showTemeLab.textColor = [UIColor colorWithHexString:@"#7809FF"];
        }else{
            self.showTimeView.layer.borderWidth = 0;
            self.showTimeView.backgroundColor = [UIColor colorWithHexString:@"F9F9F9" withAlpha:1];
            self.showTemeLab.textColor = [UIColor colorWithHexString:@"#767676"];
        }
    }else{
        self.showTimeView.layer.borderWidth = 0;
        self.showTimeView.backgroundColor = [UIColor colorWithHexString:@"F9F9F9" withAlpha:1];
        self.showTemeLab.textColor = [UIColor colorWithHexString:@"#767676" withAlpha:0.6];
    }

}

@end


网站公告

今日签到

点亮在社区的每一天
去签到