2013-05-13 104 views
2

我试图创建一个UITableViewCell与许多项目里面,但我有UILabels问题。调整UILabel在自定义UITableViewCell内部的问题

当使用UITableViewCellTextLabelDetailTextLabel默认样式只得到适当的高度,以适应内容,我只有用heightForRowAtIndexPath计算row height

但随着我的定制cellUILabelsresize他们的高度,始终保持我在界面生成器设置size,我想这

CGRect frame;  
    cell.title.text = item.title; 
    frame.origin = cell.title.frame.origin; 
    frame.size = [item.title sizeWithFont:[UIFont boldSystemFontOfSize:17] 
         constrainedToSize:CGSizeMake(FB_Cell_width, 4000) lineBreakMode:cell.title.lineBreakMode]; 

    cell.title.frame = frame; 

UILabel大小不会改变。

我尝试:

cell.title.text = item.title; 
[cell.title sizeToFit]; 

但规模依然不改。

如何以及在哪里需要更改UILabel框架以适合其内容?

编辑:我只是解开使用autolayout选项,现在它似乎工作。

+0

也许在自动口罩可以帮助。或者,也许你可以使用'sizeWithFont'。 – dasdom 2013-05-13 11:43:00

回答

0

使用

cell.title.numberOfLines = 0; //makes your label of unlimited numberoflines 
cell.title.text = item.title; 
[cell.title sizeToFit]; 
+0

在界面生成器中将numberOfLines属性设置为0 – Nanoc 2013-05-13 11:48:08

+0

'[[cell title] setText:item.title];' '[[cell title] sizeToFit];' – 2013-05-13 11:49:30

1
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

UILabel *title = [[UILabel alloc]init]; 
title.numberOfLines = 0; 
title.text = item.title; 
float height = [self heightForText:title.text]; 
title.frame = CGRectMake(10, 10, 40, height); 
[cell addSubView:title]; 
} 

- (CGFloat)heightForText:(NSString *)bodyText 
{ 
    UIFont *cellFont = [UIFont systemFontOfSize:30]; 
    CGSize constraintSize = CGSizeMake(500, MAXFLOAT); 
    CGSize labelSize = [bodyText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping]; 
    CGFloat height = labelSize.height + 10; 
    return height; 
} 

//try it like this may be it will helps u 
相关问题