Monday, September 29, 2014

Calculate Height of Text from Width of an UILabel

UILabel *type;
type.frame=[self calculateHeightOfTextFromWidth:type withText:type.text];

-(CGRect) calculateHeightOfTextFromWidth:(UILabel*)detailLabel withText:(NSString*)text
{
    
    CGRect currentFrame = detailLabel.frame;
    CGSize max = CGSizeMake(detailLabel.frame.size.width, 500);
    CGSize expected = [text sizeWithFont:detailLabel.font constrainedToSize:max lineBreakMode:detailLabel.lineBreakMode]; 
    currentFrame.size.height = expected.height;
    return currentFrame;
}

No comments:

Post a Comment