Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cell label高度计算过高 #384

Open
ZhaoEnCena opened this issue Oct 26, 2018 · 2 comments
Open

cell label高度计算过高 #384

ZhaoEnCena opened this issue Oct 26, 2018 · 2 comments

Comments

@ZhaoEnCena
Copy link

return [tableView fd_heightForCellWithIdentifier:@"TheCell" configuration:^(id cell) {
TheCell *payCell = (TheCell *)cell;
payCell.saveMoney.text = @"节省三百块现大洋,又节省了六百块American dollars,it's perfect,so, line feed now";
}];
cellForRowAtIndexPath 里
cell.saveMoney.text = @"节省三百块现大洋,又节省了六百块American dollars,it's perfect,so, line feed now";

通过分层看,cell中的label高度远超过使用 return UITableViewAutomaticDimension 计算的高度。实际占用的高度也是后者正确。 xib画的cell。
default
default

@ShouBinCheng
Copy link

- (CGFloat)numberOfText{
    // 获取单行时候的内容的size
    CGSize singleSize = [self.text sizeWithAttributes:@{NSFontAttributeName:self.font}];
    // 获取多行时候,文字的size
    CGSize textSize = [self.text boundingRectWithSize:CGSizeMake(self.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.font} context:nil].size;
    // 返回计算的行数
    return ceil( textSize.height / singleSize.height);
}

你可以试试不将numberOfLines=0; 将numberOfLines = label.numberOfText

@ShouBinCheng
Copy link

你还可以创建约束是top和height为最高优先级,bottom为最低优先级。如下可完美解决你的问题

[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.inset(20);
        make.top.inset(30).priorityHigh();
        make.height.mas_greaterThanOrEqualTo(self.titleLabel.font.lineHeight).priorityHigh();
        make.bottom.inset(30).priorityLow();
    }];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants