2012-03-21 72 views
1

我从服务器获取一些文本,并将它放到UILabel中,它将被添加到UITableViewCell中,并且每次它可以很小或很大或多行时更改此文本。我的问题是我如何使这个UILabel自动适应文本(多行)并调整UITableViewCell的大小以适应UILabel?如何调整UITableViewCell的文本大小?

+0

这是在栈上重复的问题,所以我只UPD ated stack with this solution to this answer.Just check out this solution:** [如何在UITextView里面创建一个UITableViewCell,以UITextView的形式动态调整它的高度](http://stackoverflow.com/a/4242916/434898)** – 2012-03-21 05:26:27

回答

2

你可以在你的tableView的数据源如下:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    // for the sake of illustration, say your model for this table is just an array of strings you wish to present. 
    // it's probably more complex, but the idea is to get the string you want to present for the 
    // cell at indexPath 
    NSString *stringForThisCell = [self.model objectAtIndex:indexPath.row]; 

    // you can get fancier here, and dynamically get the font from the UITextView prototype 
    // but for simplicity, just copy the font size you configured the text view with in your nib 
    CGSize size = [stringForThisCell sizeWithFont:[UIFont systemFontOfSize:14.0]]; 

    // this is a little funky, because for it to be just right, you should format your prototype 
    // cell height to be a good-looking height when your text view has a zero height. 
    // the basic idea here is that the cell will get taller as the text does. 
    return tableView.rowHeight + size.height; 
} 

然后,当您将单元配置,获取字符串和大小相同的方式,并设定UITextView的框架相匹配的大小

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


     NSString *stringForThisCell = [self.model objectAtIndex:indexPath.row]; 
     CGSize size = [stringForThisCell sizeWithFont:[UIFont systemFontOfSize:14.0]]; 
     UITextView *myTextView = (UITextView *)[cell viewWithTag:kMY_TEXT_VIEW_TAG]; // make this tag match a tag you give the text view in the prototype cell 

     myTextView.frame = CGRectMake(myTextView.frame.origin.x, myTextView.frame.origin.y, size.width, size.height); 
     // the rest of your configure cell 
} 
+0

'UILabel'可以是多行......请参阅@ Gypsa的答案。 – rickster 2012-03-21 05:32:12

+0

哦。这是很好的知道,用我的答案替换UITextView与UILabel。但请参阅我对@Gypsa的评论。 – danh 2012-03-21 05:43:43

1

您需要实现这一点: -

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 

{ 
    CGSize labelsize; 
    UILabel * textDesc1 = [[UILabel alloc] init]; 
    [textDesc1 setNumberOfLines:0]; 
    textDesc1.text=[self.blogTextArray objectAtIndex:indexPath.row];//replace with your own text 
    [textDesc1 setFont:[UIFont fontWithName:@"Helvetica" size:14.0]]; 
    labelsize=[textDesc1.text sizeWithFont:textDesc1.font constrainedToSize:CGSizeMake(320, 2000.0) lineBreakMode:UILineBreakModeWordWrap]; 
    [textDesc1 release]; 
    return (CGFloat)labelsize.height; 


} 
- (UITableViewCell *)tableView:(UITableView *)tbleView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    CGSize labelsize; 
    UILabel *commentsTextLabel = [[UILabel alloc] init];; 
    [commentsTextLabel setNumberOfLines:0]; 
    [commentsTextLabel setBackgroundColor:[UIColor clearColor]]; 
    NSString *text=[self.blogTextArray objectAtIndex:indexPath.row];//replace with your own text 
commentsTextLabel.text=text; 
    [commentsTextLabel setFont:[UIFont fontWithName:@"Helvetica"size:14]]; 
    labelsize=[text sizeWithFont:commentsTextLabel.font constrainedToSize:CGSizeMake(320, 2000.0) lineBreakMode:UILineBreakModeWordWrap]; 
    commentsTextLabel.frame=CGRectMake(0, 0, 320, labelsize.height); 
    [cell.contentView addSubview:commentsTextLabel]; 
    [commentsTextLabel release]; 
} 
+0

我不认为你想回答标签高度作为表格单元格的高度。我认为你想增加高度(格式化一行后)到附加行的高度,如果发生自动换行。 – danh 2012-03-21 05:46:37

+0

如果您仅返回标签高度,则会得到一个非常小的表格行,标签上方/下方没有填充。对于类似的情况,我已经为上面/下面的填充设置了自己的常量(基于标准标签如何看上去只有一行),然后在从'heightForRowAtIndexPath'返回之前将它们添加到标签高度。 – rickster 2012-03-21 05:49:56

+0

你可以简单地做return(CGFloat)labelsize.height + 30;您可以添加一个数字以使表格单元高度比标签高度大,这取决于您决定添加的数量。 – Gypsa 2012-03-21 07:46:42

相关问题