Tuesday, June 21, 2016

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


UITABLEVIEW DYNAMIC CELL HEIGHT


// TableView Delegate Method
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
    
    int width = 300; // width of your label
    
    return ([Self calculateHeightOfString:@"Your String" withFont:[UIFontsystemFontOfSize:15ofWidth: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_MAXlineBreakMode:line];

return suggestedSize.height;

}

No comments:

Post a Comment