gpt4 book ai didi

ios - 根据文本行自动计算标签的高度

转载 作者:行者123 更新时间:2023-12-01 16:28:26 24 4
gpt4 key购买 nike

到目前为止,我一直在进行自定义像元高度的计算。我需要在获取数据后自动调整单元格的大小。
self.countLabelself.descriptionLabel仅占用一行文本,另一方面,self.detailLabel可能包含多行文本。因此,以下代码是我设置约束的方式。

enter image description here

CustomCell.m

- (void)initView
{
self.countLabel = [UILabel new];
self.countLabel.text = @"AAAAAA";
self.countLabel.font = [UIFont fontWithPixel:22];
self.countLabel.numberOfLines = 1;
[self.contentView addSubview:self.countLabel];

self.detailLabel = [UILabel new];
self.detailLabel.text = @"Number of people: ";
self.detailLabel.font = [UIFont fontWithPixel:22];
self.detailLabel.textColor = [UIColor qu_grayColor];
[self.contentView addSubview:self.detailLabel];

self.descriptionLabel = [UILabel new];
self.descriptionLabel.numberOfLines = 1;
self.descriptionLabel.text = @"ABCDEFGHIJKLM";
self.descriptionLabel.font = [UIFont fontWithPixel:26];
[self.contentView addSubview:self.descriptionLabel];

[self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentView).offset(20);
make.top.equalTo(self.contentView).offset(5);
make.right.equalTo(self.contentView).offset(-10);
}];

[self.detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentView).offset(20);
make.top.equalTo(self.countLabel.mas_bottom);
make.bottom.equalTo(self.descriptionLabel.mas_top);
make.right.equalTo(self.contentView).offset(-10);
}];

[self.descriptionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentView).offset(10);
make.right.equalTo(self.contentView).offset(-10);
make.bottom.equalTo(self.contentView).offset(-15);

}];
}

最佳答案

self.detailLabel.text = @"Number of peoples: ";
self.detailLabel.font = [UIFont fontWithPixel:22];
...
[self setLabelHeight:self.detailLabel];


- (void)setLabelHeight:(UILabel *)label {
CGFloat fixedWidth = label.frame.size.width;
CGSize newSize = [label sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
CGRect newFrame = label.frame;
newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
label.frame = newFrame;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//if indexPath.row == detailsLabelRow
//return self.detailLabel.height
//else
//return 1 line height
}

关于ios - 根据文本行自动计算标签的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33771569/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com