如何在UITableViewCell内部动态调整两个文本标签宽度的大小? [英] How do you dynamically resize two text label widths inside of a UITableViewCell?
问题描述
我正在使用一个使用自定义UITableViewCell子类 NoteCell
的表视图.子类具有两个文本标签,即名称标签和日期标签,它们在单元格中并排放置.我的目标是调整名称标签的大小,以便无论如何显示完整的日期.
I'm using a table view that uses a custom UITableViewCell subclass, NoteCell
. The subclass has two text labels, a name label and a date label which are side-by-side in the cell. My goal is to have the name label resize itself so that the full date is shown no matter what.
在cellForRowAtIndexPath中,我尝试计算并设置两个文本视图的宽度,以便完全显示日期,并截断名称标签.
in cellForRowAtIndexPath, I try to calculate and set the widths of the two text views so that the date is fully displayed and the name label is truncated.
- (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);
}
不幸的是,结果不正确,因为我认为我没有正确设置/计算边框.请参见下图.
Unfortunately, the results are not correct because I don't think I'm setting/calculating the frame bounds correctly. See the image below.
不考虑框架计算的问题,是否有更好的方法来解决此问题,或者是手动放置文本标签框架的唯一方法?
Disregarding the problems with the frame calculations is there a better way to approach this problem or is manually positioning the text label frame the only way?
推荐答案
尝试使用它来设置dateLabel.
try use this to set your 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;
}
这篇关于如何在UITableViewCell内部动态调整两个文本标签宽度的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!