Thursday 18 December 2014

Objective C + Calculate Height of Table's Cell Dynamically or At Run Time



// TableView Delegate Method
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    int width = 300; // width of your label
    
    return ([Self calculateHeightOfString:@"Your String" withFont:[UIFont systemFontOfSize:15] ofWidth:width withLineBreak:NSLineBreakByWordWrapping]+30);
    

}




// Method to calculate height
-(float)calculateHeightOfString:(NSString *)str withFont:(UIFont *)font ofWidth:(float)width withLineBreak:(NSLineBreakMode)line
{
    //NSLog(@"%s",__PRETTY_FUNCTION__);
  
CGSize suggestedSize = [str sizeWithFont:font constrainedToSize:CGSizeMake(width, FLT_MAX) lineBreakMode:line];

return suggestedSize.height;

}

No comments:

Post a Comment