I get some text from the server and i put it to the UILabel that it will be add to the UITableViewCell and this text change every time it can be tiny or large or with multiple line. my question is how can i make this UILabel to fit the text automatically (multiple line) and resize the UITableViewCell to fit the UILabel?
Asked
Active
Viewed 8,028 times
1
-
This is a repeating question on stack , so I just updated stack with the solution to this answer.Just check out this solution : **[How to make a UITableViewCell with a UITextView inside, that dynamically adjust its height, on the basis of the UITextView?](http://stackoverflow.com/a/4242916/434898)** – Ajay Sharma Mar 21 '12 at 05:26
2 Answers
2
You can do the following in your tableView's datasource:
- (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;
}
Then, when you configure the cell, get the string and the size the same way, and set the UITextView frame to match the size
- (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
}

danh
- 62,181
- 10
- 95
- 136
-
-
oh. that's good to know, replace UITextView in my answer with UILabel. But see my comment on @Gypsa. – danh Mar 21 '12 at 05:43
1
You need to implement this:-
- (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];
}

Gypsa
- 11,230
- 6
- 44
- 82
-
I don't think you want to answer the label height as the height of the table cell. I think you want to increment the height (after formatting for a single line) to by the height of the additional lines if word-wrap happens. – danh Mar 21 '12 at 05:46
-
If you return just the label height, you'll get a pretty small table row, with no padding above/below the label. For similar cases, I've set up my own constants for padding above/below (based on how a standard label looks with one line) and then added them to the label height before returning from `heightForRowAtIndexPath`. – rickster Mar 21 '12 at 05:49
-
you can simply do return (CGFloat)labelsize.height+30; you can add a number to make a table cell height a big than the label height , it is up to you to decide the number of addition. – Gypsa Mar 21 '12 at 07:46
-
1Whaht's the point of creating, configuring, and releasing a UILabel in heightForRowAtIndexPath? The real measurement comes with the call to [NSString sizeWithFont:constrainedToSize:lineBreakMode]. And, what's the point of allocating your own UILabel in cellForRowAtIndexPath? You can configure the textLabel and detailTextLabel to wrap as you like anyway. – Joe Strout Oct 25 '12 at 03:09