Masonry自动计算行高:
@interface ChatTableViewCell()
@property (weak,nonatomic) IBOutletUILabel *chatLabel;
@property (weak,nonatomic) IBOutletUIActivityIndicatorView *activityView;
@property (weak,nonatomic) IBOutletUIButton *sendButton;
@end
@implementation ChatTableViewCell
-(void)layoutSubviews
{
[superlayoutSubviews];
//---------------------------------设置最大约束尺寸
self.chatLabel.preferredMaxLayoutWidth = kScreenWidth -30;
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [superinitWithStyle:style reuseIdentifier:reuseIdentifier]) {
[selfconfigSubviews];
self.selectionStyle =UITableViewCellSelectionStyleNone;
}
returnself;
}
-(void)configSubviews
{
//展示文本
UILabel * label = [[UILabelalloc]init];
self.chatLabel = label;
self.chatLabel.numberOfLines = 0;
[self.contentViewaddSubview:label];
WS(ws);
//对文本添加约束
[self.chatLabelmas_makeConstraints:^(MASConstraintMaker *make) {
//左边 上边各留5各间距,右边由上面设置的最大约束进行设置约束
make.left.top.equalTo(ws.contentView).offset(5);
//-----------------------------------重点:将最下面的控件与contentView进行约束,否则行高不准确
make.bottom.equalTo(ws.contentView).offset(-5);
}];
//小菊花
UIActivityIndicatorView * indicationView = [[UIActivityIndicatorViewalloc]init];
indicationView.color = [UIColorgrayColor];
self.activityView = indicationView;
[self.contentViewaddSubview:indicationView];
//对小菊花添加约束
[self.activityViewmas_makeConstraints:^(MASConstraintMaker *make) {
//水平居中
make.centerY.equalTo(ws.contentView);
//大小
make.size.mas_equalTo(CGSizeMake(15,15));
//在文本的右侧 留5个间距
make.left.equalTo(ws.chatLabel.mas_right).offset(5);
}];
//buttion与菊花的位置全都相同
UIButton * button = [[UIButtonalloc]init];
button.backgroundColor = [UIColorredColor];
[button setImage:[UIImageimageNamed:@"blueArrow"]forState:UIControlStateNormal];
[self.contentViewaddSubview:button];
self.sendButton = button;
[self.sendButtonmas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(ws.contentView);
make.size.mas_equalTo(CGSizeMake(15,15));
make.left.equalTo(ws.chatLabel.mas_right).offset(5);
}];
//----------------------------------重点:设置cell的contentView 与 cell四条边对齐
[self.contentViewmas_makeConstraints:^(MASConstraintMaker *make) {
// 重点代码:contentView的4条边和self对齐
make.top.left.right.bottom.mas_equalTo(self);
}];
}