我有一个很长的标签,这是我的第一个标签,我想把它放在我的牢房里。这就是我所拥有的,但它不起作用。
我有一个自定义的UITabelviewCell,里面有几个标签。
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
switch (indexPath.row) {
case 0:{
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [diningHallTimes[indexPath.row][@"description"] sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
CGFloat height = MAX(size.height, 44.0f);
return height + (CELL_CONTENT_MARGIN * 2) + 40;
// return myStringSize.height;
break;
}
default:
return 40;
break;
}
}
这是行的单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
DiningInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [diningHallTimes[indexPath.row][@"description"] sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
CGFloat height = MAX(size.height, 44.0f);
CGFloat the = height + (CELL_CONTENT_MARGIN * 2);
cell.descriptionLabel.numberOfLines = 0;
[cell.descriptionLabel setFrame:CGRectMake(20, 0, 280, the)];
NSLog(@"%f", the);
cell.descriptionLabel.text = diningHallTimes[indexPath.row][@"description"];
cell.daysLabel.text = diningHallTimes[indexPath.row][@"days"];
cell.timeLabel.text = diningHallTimes[indexPath.row][@"time"];
return cell;
}
但是这就是我的细胞看起来像
不确定为什么会发生这种情况,我正在运行iOS8,但是我需要它同时适用于7和。
感谢您的帮助。
答案 0 :(得分:2)
您需要为标签文字计算boundingRectWithSize:
。还需要计算更新内容所需的行数。请尝试以下方法来计算标签的新框架。
- (UILabel *) updateLabelFrame:(UILabel *)label {
CGRect lblRect = label.frame;
CGSize maxSize = CGSizeMake(label.frame.size.width, MAXFLOAT);
CGRect labelRect = [label.text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:label.font} context:nil];
CGFloat labelHeight = labelRect.size.height;
// For below line 16 is default height (The height before content is set). Change this value
// as per your requirement
int lines = labelHeight / 16; // Here 16 is a fix height (default height) for label.
[label setNumberOfLines:lines];
lblRect.size.height = labelHeight;
[label setFrame:lblRect];
return label;
}
修改强>
您可以将此方法放在任何您想要的位置。像一些基类或相同的视图控制器。这里我修改了上面的方法,它将返回带有动态帧的标签。
对于您的情况,您需要将此方法放在视图控制器类中。然后在为标签更新内容后,在-cellForRowAtIndexPath
中调用此方法。见下面的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
DiningInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [diningHallTimes[indexPath.row][@"description"] sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
CGFloat height = MAX(size.height, 44.0f);
CGFloat the = height + (CELL_CONTENT_MARGIN * 2);
cell.descriptionLabel.numberOfLines = 0;
[cell.descriptionLabel setFrame:CGRectMake(20, 0, 280, the)];
NSLog(@"%f", the);
cell.descriptionLabel.text = diningHallTimes[indexPath.row][@"description"];
cell.daysLabel.text = diningHallTimes[indexPath.row][@"days"];
cell.timeLabel.text = diningHallTimes[indexPath.row][@"time"];
// Update descriptionLabel height.
cell.descriptionLabel = [self updateLabelFrame:cell.descriptionLabel];
return cell;
}
答案 1 :(得分:1)
您必须为标签设置Autolayout约束,并使用内容创建原型单元格。然后你必须返回,然后你必须得到牢房的高度。
供参考,您可以看到以下视频 https://www.youtube.com/watch?v=6KImie4ZMwk
答案 2 :(得分:1)
尝试[yourLabel sizeToFit]
,或者您可以使用[yourLabel sizeThatFit:CGFrame];