Reputation: 641
I am trying to resize the height of UILabel in a Table Cell. INnmy case the table is dynamic and I have customized it using two UILabel. The Table will show exactly 11 items, but two of the cells will need to accommodate multi-line text UILabel. I have selectively increase the height of two cells as follows:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == 3 || indexPath.row == 10)
return 100;
else
return 46;
}
But I also need to increase the height of the UILabel for those two cells only. I have tried using following code (also tried some other examples from this site).
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CellId";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
UILabel *DetailLabel = (UILabel *)[cell.contentView viewWithTag:10];
UILabel *DetailText = (UILabel *)[cell.contentView viewWithTag:20];
if(indexPath.row == 3 || indexPath.row ==10){
[DetailText setText:[self.EmployerViewText objectAtIndex:[indexPath row]]];
DetailText.lineBreakMode = NSLineBreakByWordWrapping;
DetailText.font = [UIFont systemFontOfSize:17.0f];
DetailText.numberOfLines = 0;
DetailText.frame = CGRectMake(0, 0, 317, 80);
[DetailText sizeToFit];
}else{
[DetailText setText:[self.EmployerViewText objectAtIndex:[indexPath row]]];
}
[DetailLabel setText:[self.EmployerViewLabel objectAtIndex:[indexPath row]]];
return cell;
}
Unfortunately, the size of the UILable remained unchanged. Can anyone point out what I am doing wrong?
Upvotes: 0
Views: 358
Reputation: 4272
The problem is when you change the text in DetailText, you don't call [DetailText sizeToFit] again.
DetailText = [[UILabel alloc]initWithFrame:CGRectMake(60, 200, 100, 30)];
DetailText.backgroundColor = [UIColor orangeColor];
DetailText.lineBreakMode = NSLineBreakByWordWrapping;
DetailText.font = [UIFont systemFontOfSize:17.0f];
DetailText.numberOfLines = 0; // Here is the key
DetailText.text = @"test message test message test message test message test message test message";
DetailText.frame = CGRectMake(100, 0, 100, 80);
[DetailText sizeToFit];
after when you change the text:
DetailText.text = @"this is the modified text";
[DetailText sizeToFit];
I tried this code and it resized.
Upvotes: 1
Reputation: 2019
From UILabel.h:
// this determines the number of lines to draw and what to do when sizeToFit is called. default value is 1 (single line). A value of 0 means no limit
// if the height of the text reaches the # of lines or the height of the view is less than the # of lines allowed, the text will be
// truncated using the line break mode.
@property(nonatomic) NSInteger numberOfLines;
Just allow multiline UILabel:
...
[DetailText setText:[self.EmployerViewText objectAtIndex:[indexPath row]]];
DetailText.lineBreakMode = NSLineBreakByWordWrapping;
DetailText.font = [UIFont systemFontOfSize:17.0f];
DetailText.numberOfLines = 0; // Here is the key
DetailText.frame = CGRectMake(0, 0, 317, 80);
[DetailText sizeToFit];
...
You may want to set numberOfLines to 1 for other cases.
Upvotes: 0