0

i want to make labels dynamic(height and width change when text size increase), is it possible in iOS7?? post the complete tutorial or any post. thanks in advance.

Anu
  • 89
  • 5

2 Answers2

1

Yes it is. It can be done like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";


ItemCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

...

int fontSize = 13;
int height = ROW_HEIGHT_SCALE;

if(height==5){
        fontSize = 10;
    }else if(height>5 && height <100){
        fontSize = 12;
    }else if(height>100 && height <300){
        fontSize = 16;
    }else if(height>300){
        fontSize = 18;
}

[cell.textLabel setFont:[UIFont fontWithName: @"Arial" size: fontSize]];

...

return cell;
}
Sergey Neskoromny
  • 1,188
  • 8
  • 15
0
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ 
  static NSString *cellIdentifier =@"cell";
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

  if (!cell) {
      cell =[tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    }
 UILabel *itemNameLabel = (UILabel *)[cell viewWithTag:1];
 itemNameLabel.text = @"Your text";
 CGSize labelSize = [self WidthOfCellWithIngredientLine:itemNameLabel];
 itemNameLabel.frame = CGRectMake(10, 10, 250, labelSize.height);

}

- (CGSize)WidthOfCellWithIngredientLine:(UILabel *)stringLabel
{
    stringLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:15.0f];
    CGSize maximumLabelSize = CGSizeMake(310, 9999);
    CGSize expectedSize = [stringLabel sizeThatFits:maximumLabelSize];
    return expectedSize;
}

Please note : Based on the label height set tableCell height also.

in stringLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:15.0f];

set the font-name and font-size that you are set on your label

Vineesh TP
  • 7,755
  • 12
  • 66
  • 130