欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

笔记masonry cell中图片和文字的自适应高

程序员文章站 2022-06-08 16:31:49
...

贴码:

#import "HomeImgWithLabelCell.h"

@interface HomeImgWithLabelCell()
///存放image 和 label 的容器view
@property (nonatomic, strong)   UIView      *it_CView;
@end

@implementation HomeImgWithLabelCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createControls];
        [self layoutControls];
    }
    return self;
}

- (void)createControls
{
    self.lb_title = [[UILabel alloc]init];
    self.lb_title.font = [UIFont systemFontOfSize:16];
    self.lb_title.textColor = [UIColor blackColor];
    self.lb_title.numberOfLines = 0;
    self.imgv_icon = [[UIImageView alloc]init];
    [self.imgv_icon sd_setImageWithURL:[NSURL URLWithString:@"https://pic.36krcnd.com/201803/30021923/e5d6so04q53llwkk!heading"] placeholderImage:nil];
    self.it_CView = [[UIView alloc]init];
    
    [self.contentView addSubview:self.it_CView];
    [self.it_CView addSubview:self.lb_title];
    [self.it_CView addSubview:self.imgv_icon];
}

- (void)layoutControls
{
    [self.it_CView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.equalTo(0);
        make.height.greaterThanOrEqualTo(90); // 10 + image的 70 + 10 主要保证最小情况下能显示出图
        make.bottom.equalTo(0); //撑高使用
    }];
    
    [self.lb_title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(10);
        make.leading.equalTo(16);
        make.trailing.equalTo(self.imgv_icon.mas_leading).offset(-16);
        make.height.greaterThanOrEqualTo(20);//.priorityHigh();  //这两句的效果就不一样了
        make.bottom.equalTo(-10);//.priorityLow();
    }];
    
    [self.imgv_icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(10);
        make.trailing.equalTo(-16);
        make.width.equalTo(108);
        make.height.equalTo(70);
    }];
}

@end

[self.lb_title mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.equalTo(10);

        make.leading.equalTo(16);

        make.trailing.equalTo(self.imgv_icon.mas_leading).offset(-16);

        make.height.greaterThanOrEqualTo(20);

        make.bottom.equalTo(-10);

    }];

这个效果如图:

笔记masonry cell中图片和文字的自适应高


使用优先级后效果:

    [self.lb_title mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.equalTo(10);

        make.leading.equalTo(16);

        make.trailing.equalTo(self.imgv_icon.mas_leading).offset(-16);

        make.height.greaterThanOrEqualTo(20).priorityHigh();

        make.bottom.equalTo(-10).priorityLow();

    }];

笔记masonry cell中图片和文字的自适应高