2012-03-21 108 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

相關問題