Skip to content

Instantly share code, notes, and snippets.

@ankushkushwaha
ankushkushwaha / Calculate_Label_Height.m
Created June 27, 2015 10:17
Calculate Label Height dynamically in iOS
/**
* Calculate height for a fixed weidht label
* @param UILabel for which height to be calculated
**/
-(double)getLabelHeightForLabel:(UILabel *)label{
CGSize constrainedSize = CGSizeMake(label.frame.size.width, 1000);
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys: label.font, NSFontAttributeName,nil];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:label.text attributes:attributesDictionary];
CGRect requiredHeight = [string boundingRectWithSize:constrainedSize options:NSStringDrawingUsesLineFragmentOrigin context:nil];