Tuesday, December 15, 2015

How to create dynamic label height according to the line of text in cell of tableview in iOS

Use this method for making dynamic height of label in cell. And setting its frames according to other label's and its text.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Define Your Cell Here

    //Setting Properties of Labels Here Like this
    cell.countLabel.font = [UIFont systemFontOfSize:22];
    cell.countLabel.numberOfLines = 0;

    cell.detailLabel.font = [UIFont systemFontOfSize:22];
    cell.detailLabel.numberOfLines = 0;

    cell.descriptionLabel.numberOfLines = 0;
    cell.descriptionLabel.font = [UIFont systemFontOfSize:26];

    //Setting label's Text Here
    cell.countLabel.text = @"AAAAAA";
    cell.detailLabel.text = @"Number of people: ";
    cell.descriptionLabel.text = @"ABCDEFGHIJKLM";

    //Setting Label's Dynamic Height Using setlableFrame Method
    cell.countLabel = [self setlableFrame:cell.countLabel fontSize:22];
    cell.detailLabel = [self setlableFrame:cell.detailLabel fontSize:22];
    cell.descriptionLabel = [self setlableFrame:cell.descriptionLabel fontSize:26];

    //Setting Label's Frame According to Above Label
    cell.detailLabel.frame = CGRectMake(cell.detailLabel.frame.origin.x,       cell.countLabel.frame.size.height + cell.countLabel.frame.origin.y, cell.detailLabel.frame.size.width, cell.detailLabel.frame.size.height);

    cell.descriptionLabel.frame =  CGRectMake (cell.descriptionLabel.frame.origin.x, cell.detailLabel.frame.size.height + cell.detailLabel.frame.origin.y, cell.descriptionLabel.frame.size.width, cell.descriptionLabel.frame.size.height);

      return cell;
}
Now, Use following method for dynamic height of cell.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
     CGFloat myCellHeight = 0;//For Cell Height

     //For countLabel
     UILabel * myLabel = [[UILabel alloc] init];
     myLabel.frame = CGRectMake( 0, 0, 290, 21.0);//here set frame same as countLabel label's frame. make sure that, You must have setting width same as that label, otherwise you don't have accuracy for this.
     myLabel.numberOfLines = 0;
     myLabel.text = @"AAAAAA";
     myLabel = [self setlableFrame:myLabel fontSize:22.0];

     //Adding Height of countLabel
     myCellHeight = myLabel.frame.size.height;

     //For detailLabel
     myLabel.text = @"Number of people: ";
     myLabel = [self setlableFrame:myLabel fontSize:22.0];

     //Adding Height of detailLabel
     myCellHeight = myCellHeight + myLabel.frame.size.height;

     //For descriptionLabel
     myLabel.text = @"ABCDEFGHIJKLM";
     myLabel = [self setlableFrame:myLabel fontSize:26.0];

     //Adding Height of detailLabel
     myCellHeight = myCellHeight + myLabel.frame.size.height;

     return myCellHeight;
}
This Method is Used for making dynamic height of label according to text.
//for setting the dynamic height of labels
-(UILabel *)setlableFrame:(UILabel*)myLabel fontSize:(float)size
{
    CGSize possibleSize = [myLabel.text sizeWithFont:[UIFont systemFontOfSize: size] constrainedToSize:CGSizeMake(myLabel.frame.size.width, 9999) lineBreakMode:NSLineBreakByWordWrapping];

    CGRect newFrame = myLabel.frame;
    newFrame.size.height = possibleSize.height;
    //If you want dynamic width of label, then uncomment following line
    //newFrame.size.width = possibleSize.width;
    myLabel.frame = newFrame;
    return myLabel;
}

No comments:

Post a Comment