2012-02-24 76 views
1

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

中的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); 
} 

不幸的是,因为我不认为我设置/正确计算帧边界做的结果是不正确的。看到下面的图片。

Image 1 Image 2

忽视的问题与框架计算是否有更好的方式来处理这个问题,或者是手动定位文本标签框架的唯一途径?

+0

您是否已尝试在被问到的标签上设置autoResize属性? – 2012-02-24 23:33:55

+0

你的意思是说,设置autoresize属性,即支柱和弹簧? - 如果是这样,那么当设备旋转时,将调整标签的大小以扩大或缩小。 – 2012-02-24 23:40:33

+0

您正确设置了日期标签的调整大小掩码,但未设置名称标签。它应该具有灵活的宽度和固定的右边距。 – sosborn 2012-02-25 00:37:31

回答

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; 
} 
+0

谢谢回复! - 我已经尝试过这种方法,但是会发生什么情况是日期标签从屏幕上偏离了披露指示符。基本上,因为您正在增加宽度,而不是扩大标签并将其向左扩展,则会将宽度增加到右侧。 – 2012-02-26 01:11:54

+0

我也尝试过使用支柱和弹簧。我将原点锚点设置在右上角,而不是默认的左上角。我也设置了标签自动展开。仍然没有运气,因为我遇到了与上述相同的问题。 – 2012-02-26 01:13:17

0

后您设置文本尝试调用 方法“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设置修正为顶部和左侧正确地为两个标签

+0

我现在给它一个尝试,但我注意到的第一件事是,'sizeToFit'方法调整大小和移动标签出它创建它自己的一套问题的位置。你认为有没有更好的方法来实现这个目标,而不需要手动计算宽度? – 2012-02-25 00:09:07

+0

sizeToFit调整您的uilabel的框架以消除未使用的空间。这样,您可以确切地知道有多大你的文字,和现在的“手动”计算其宽度也不是那么难......看到新的编辑我的回答 – meronix 2012-02-25 10:18:16

+0

好,我给它一个尝试,我很接近它来获得工作 - 你的建议是有关sizeToFit。有一个徘徊的问题。标签的起源是固定的,然后sizeToFit将扩大标签的框架然而以适合文本(这是哪里出了问题),它扩展了标签没有离开的权利,这意味着它跑出镜头过去披露的指标。标签需要将宽度向左扩展,而不是右... – 2012-02-26 01:53:41

相关问题