自动计算UITableView的tableHeaderView高

2018-01-19  本文已影响0人  Desert_Eagle

UITableView的tableHeaderView在使用约束写的时候最后需要计算高度,使用masonry时最后可以用

[_tableHeaderView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height

计算高度

demo如下:

- (UIView *)tableHeaderView{
    if (!_tableHeaderView) {
        _tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 0)];
        
        UILabel *titleLab = [[UILabel alloc] init];
        titleLab.numberOfLines = 0;
        titleLab.text = [NSString stringWithFormat:@"%@",_headerDataDic[@"title"]];
        titleLab.font = [UIFont systemFontOfSize:18];
        titleLab.textColor = [UIColor hexStringToColor:@"#333333"];
        [_tableHeaderView addSubview:titleLab];
        
        UILabel *timeLab = [[UILabel alloc] init];
        timeLab.font = [UIFont systemFontOfSize:12];
        timeLab.textColor = [UIColor hexStringToColor:@"#999999"];
        timeLab.text = [NSString stringWithFormat:@"%@",_headerDataDic[@"online_time"]];
        [_tableHeaderView addSubview:timeLab];
        
        UILabel *editorLab = [[UILabel alloc] init];
        editorLab.textColor = timeLab.textColor;
        editorLab.font = timeLab.font;
        editorLab.text = [NSString stringWithFormat:@"%@",_headerDataDic[@"art_author"]];
        [_tableHeaderView addSubview:editorLab];
        
        UILabel *clickCountLab = [[UILabel alloc] init];
        clickCountLab.font = timeLab.font;
        clickCountLab.textColor = timeLab.textColor;
        clickCountLab.text = [NSString stringWithFormat:@"%@",_headerDataDic[@"art_click"]];
        [_tableHeaderView addSubview:clickCountLab];
        
        UIImageView *clickImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"discover_see"]];
        [_tableHeaderView addSubview:clickImageView];
        
        UILabel *thumbUpLab = [[UILabel alloc] init];
        thumbUpLab.textColor = timeLab.textColor;
        thumbUpLab.font = timeLab.font;
        thumbUpLab.text = [NSString stringWithFormat:@"%@",_headerDataDic[@"laud_count"]];
        [_tableHeaderView addSubview:thumbUpLab];
        _thumbUpCountLab = thumbUpLab;
        
        UIButton *thumbUpBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [thumbUpBtn setImage:[UIImage imageNamed:@"discover_thumbUp"] forState:UIControlStateNormal];
        thumbUpBtn.userInteractionEnabled = NO;
        [_tableHeaderView addSubview:thumbUpBtn];
        
        UILabel *detailLab = [[UILabel alloc] init];
        detailLab.numberOfLines = 0;
        [_tableHeaderView addSubview:detailLab];
        NSString *detailTextString = [NSString stringWithFormat:@"%@",_headerDataDic[@"art_content"]];
        NSString *str = [NSString stringWithFormat:@"<head><style>img{width:%f !important;height:auto}</style></head>%@",self.view.width,detailTextString];
        NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithData:[str dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} documentAttributes:nil error:nil];
        detailLab.attributedText = attributeString;
        
        UIView *seperatorView = [[UIView alloc] init];
        seperatorView.backgroundColor = [UIColor hexStringToColor:@"#f1f2f6"];
        [_tableHeaderView addSubview:seperatorView];
        
        UILabel *commentLab = [[UILabel alloc] init];
        commentLab.textColor = [UIColor hexStringToColor:@"#333333"];
        commentLab.font = [UIFont systemFontOfSize:15];
        commentLab.text = @"评论";
        [_tableHeaderView addSubview:commentLab];
        
        UILabel *commentCountLab = [[UILabel alloc] init];
        commentCountLab.textColor = [UIColor hexStringToColor:@"#999999"];
        commentCountLab.text = [NSString stringWithFormat:@"%@条",[NSString stringWithFormat:@"%@",_headerDataDic[@"comm_count"]]];
        commentCountLab.font = [UIFont systemFontOfSize:12];
        [_tableHeaderView addSubview:commentCountLab];
        
        UIView *bottomSeperatorView = [[UIView alloc] init];
        bottomSeperatorView.backgroundColor = [UIColor hexStringToColor:@"#e2e2e2"];
        [_tableHeaderView addSubview:bottomSeperatorView];
        
        [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_tableHeaderView.mas_top).offset(24);
            make.left.equalTo(_tableHeaderView.mas_left).offset(15);
            make.right.equalTo(_tableHeaderView.mas_right).offset(-15);
        }];
        
        [timeLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(titleLab.mas_bottom).offset(15);
            make.left.equalTo(titleLab.mas_left);
        }];
        
        [clickCountLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(timeLab.mas_centerY);
            make.right.equalTo(titleLab.mas_right);
        }];
        
        [clickImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(clickCountLab.mas_centerY); 
            make.right.equalTo(clickCountLab.mas_left).offset(-5);
            make.width.height.mas_equalTo(17);
        }];
        
        [thumbUpLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(clickCountLab.mas_centerY);
            make.right.equalTo(clickImageView.mas_left).offset(-20);
        }];
        
        [thumbUpBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(thumbUpLab.mas_centerY);
            make.right.equalTo(thumbUpLab.mas_left);
            make.width.mas_equalTo(29);
            make.height.mas_equalTo(34);
        }];
        
        [editorLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(timeLab.mas_centerY);
            make.left.equalTo(timeLab.mas_right).offset(20);
        }];
        
        [detailLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(titleLab);
            make.top.equalTo(timeLab.mas_bottom).offset(24);
        }];
        
        titleLab.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width - 30;
        detailLab.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width - 30;
        
        [seperatorView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(_tableHeaderView);
            make.top.equalTo(detailLab.mas_bottom).offset(35);
            make.height.mas_equalTo(8);
        }];
        
        [commentLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(seperatorView.mas_bottom);
            make.left.equalTo(titleLab.mas_left);
            make.height.mas_equalTo(44);
        }];
        
        [commentCountLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(commentLab);
            make.left.equalTo(commentLab.mas_right).offset(10);
        }];
        
        [bottomSeperatorView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(_tableHeaderView);
            make.top.equalTo(commentLab.mas_bottom);
            make.height.mas_equalTo(1);
            make.bottom.equalTo(_tableHeaderView.mas_bottom);
        }];
        
        CGFloat height = [_tableHeaderView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
        _tableHeaderView.height = height;
        
    }
    return _tableHeaderView;
}

注意点:在有label的情况下需要确定label的最大宽度,如下所示:

titleLab.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width - 30;
detailLab.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width - 30;
上一篇下一篇

猜你喜欢

热点阅读