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;

}

Swift + Get Height of Label Dynamically


How to set Dynamically UILabel Height in Swift


func heightForLabel(text:String, font:UIFont, width:CGFloat) -> CGFloat
    {
        let label:UILabel = UILabel(frame: CGRectMake(00, width, CGFloat.max))
        label.numberOfLines = 0
        label.lineBreakMode = NSLineBreakMode.ByWordWrapping
        label.font = font
        label.text = text
        
        label.sizeToFit()
        return label.frame.height

    }




let font = UIFont(name: "Verdana", size: 12)
        var detailHeight = heightForLabel("Your String", font: font!, width:self.scrollView.bounds.size.width-10)