如何动态调整UITableViewCell内的两个文本标签宽度?

我正在使用一个使用自定义UITableViewCell子类NoteCell的表视图。 子类有两个文本标签,一个名称标签和一个日期标签,它们在单元格中并排放置。 我的目标是让名称标签自行resize,以便无论如何显示完整日期。

在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); } 

不幸的是,结果不正确,因为我认为我没有正确设置/计算帧边界。 见下图。

图1图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; } 

设置文本后尝试调用方法“sizeToFit”然后你可以正确获取yourLabel.frame.size.width,所以如果2个标签超过单元格框架,只需调整较小尺寸的名称标签并设置正确第二个位置,从第一个结束开始……

新编辑(见评论)

 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设置都正确地校正到顶部和左侧