我正在使用一个使用自定义UITableViewCell子类NoteCell
的表视图。子类有两个文本标签,一个名称标签和一个日期标签,它们在单元格中并排放置。我的目标是让名称标签自行调整大小,以便无论如何都显示完整的日期。
在cellForRowAtIndexPath中,我尝试计算并设置两个文本视图的宽度,以便完全显示日期并截断名称标签。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"NoteCell";
NoteCell *cell = (NoteCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
cell.dateLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin;
cell.nameLabel.text = @"A name that should be truncated";
cell.dateLabel.text = @"A long date to use as an example";
// Set the widths
// First calculate how wide the date label needs to be.
cell.dateLabel.text = @"A really long date";
CGSize dateLabelSize = [cell.dateLabel.text sizeWithFont:cell.dateLabel.font];
CGFloat dateExpansionAmount = fabsf(dateLabelSize.width - cell.dateLabel.frame.size.width) + 10.0f;
cell.dateLabel.frame = CGRectMake(cell.dateLabel.frame.origin.x - dateExpansionAmount,
cell.dateLabel.frame.origin.y,
dateLabelSize.width,
cell.dateLabel.frame.size.height);
CGFloat nameLabelWidth = cell.dateLabel.frame.origin.x - cell.nameLabel.frame.origin.x;
cell.nameLabel.frame = CGRectMake(cell.nameLabel.frame.origin.x,
cell.nameLabel.frame.origin.y,
nameLabelWidth,
cell.nameLabel.frame.size.height);
}
不幸的是,结果不正确,因为我认为我没有正确设置/计算帧边界。见下图。
忽略帧计算的问题是否有更好的方法来解决这个问题,或者手动定位文本标签框架的唯一方法是什么?
答案 0 :(得分:2)
尝试使用它来设置dateLabel。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"NoteCell";
NoteCell *cell = (NoteCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
cell.dateLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin;
cell.nameLabel.text = @"A name that should be truncated";
cell.dateLabel.text = @"A long date to use as an example";
//set frame cell.dateLabel
CGSize maximumSize = CGSizeMake(INT_MAX, 44); // CGSizeMake(width,height).
CGSize dateStringSize = [[cell.dateLabel.text sizeWithFont:[UIFont systemFontOfSize:cell.dateLabel.font]
constrainedToSize:maximumSize
lineBreakMode:UILineBreakModeWordWrap];
CGRect dateFrame = cell.dateLabel.frame;
dateFrame.size.width = dateStringSize. width;
cell.dateLabel.frame = dateFrame;
return cell;
}
答案 1 :(得分:0)
设置文本后尝试调用 方法“sizeToFit” 然后你可以正确地获得yourLabel.frame.size.width,所以如果2个标签超过单元格框架,只需调整较小尺寸的名称标签,并为第二个标签设置正确的位置,从第一个标签开始。
NEW EDIT(见评论)
cell.nameLabel.text = @"A name that should be truncated";
cell.dateLabel.text = @"A long date to use as an example";
[cell.nameLabel sizeToFit];
[cell.dateLabel sizeToFit];
// now check if the 2 width exceed cell size:
if (cell.nameLabel.frame.size.width + cell.dateLabel.frame.size.width > cell.frame.size.width){
//resize nameText:
cell.nameLabel.frame.size.width = cell.frame.size.width - cell.dateLabel.frame.size.width;
// ...and maybe consider to give it a minim size requirement
}
// now set the positions
cell.nameLabel.frame = CGRectMake(0,0,cell.nameLabel.frame.size.width,cell.nameLabel.frame.size.height );
cell.dateLabel.frame = CGRectMake(cell.nameLabel.frame.size.width,0,cell.dateLabel.frame.size.width,cell.dateLabel.frame.size.height );
PS 确保将autoresizingMask设置更正为两个标签的顶部和左侧