Skip to content

Instantly share code, notes, and snippets.

@danielphillips
Created June 2, 2011 22:54
Show Gist options
  • Star 81 You must be signed in to star a gist
  • Fork 30 You must be signed in to fork a gist
  • Save danielphillips/1005520 to your computer and use it in GitHub Desktop.
Save danielphillips/1005520 to your computer and use it in GitHub Desktop.
Adjust UILabel to change it's frame according to it's content
@interface UILabel (dynamicSizeMe)
-(float)resizeToFit;
-(float)expectedHeight;
@end
#import "UILabel+dynamicSizeMe.h"
@implementation UILabel (dynamicSizeMe)
-(float)resizeToFit{
float height = [self expectedHeight];
CGRect newFrame = [self frame];
newFrame.size.height = height;
[self setFrame:newFrame];
return newFrame.origin.y + newFrame.size.height;
}
-(float)expectedHeight{
[self setNumberOfLines:0];
[self setLineBreakMode:UILineBreakModeWordWrap];
CGSize maximumLabelSize = CGSizeMake(self.frame.size.width,9999);
CGSize expectedLabelSize = [[self text] sizeWithFont:[self font]
constrainedToSize:maximumLabelSize
lineBreakMode:[self lineBreakMode]];
return expectedLabelSize.height;
}
@end
@ajubbal
Copy link

ajubbal commented Sep 26, 2013

It won't cause breakage Abizern, it will merely override the already existing method. That is the behavior of categories.

@WingedDoom
Copy link

I've changed -(float)expectedHeight method to work in iOS7 and Xcode 5 without any warnings. Hope it will help someone:

-(float)expectedHeight {

[self setNumberOfLines:0];
[self setLineBreakMode:NSLineBreakByCharWrapping];

UIFont *font = [UIFont systemFontOfSize:14.0]; //Warning! It's an example, set the font, you need

NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                      font, NSFontAttributeName,
                                      nil];

CGSize maximumLabelSize = CGSizeMake(self.frame.size.width,9999);

CGRect expectedLabelRect = [[self text] boundingRectWithSize:maximumLabelSize
                                                     options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
                                                  attributes:attributesDictionary
                                                     context:nil];
CGSize *expectedLabelSize = &expectedLabelRect.size;

return expectedLabelSize->height;

}

@emotality
Copy link

Thanks @WingedDoom

Your answer made me create this:

    ...
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 80)];
    [titleLabel setText:response[@"title"]];
    [titleLabel setFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:22]];
    [ATStringHelper setExpectedHeightForLabel:titleLabel maxHeight:80];
    [self.view addSubview: titleLabel];
}

- (void)setExpectedHeightForLabel:(UILabel *)label maxHeight:(CGFloat)maxHeight
{
    [label setNumberOfLines:0];
    [label setLineBreakMode:NSLineBreakByWordWrapping];
    CGSize currentLabelSize = CGSizeMake(label.frame.origin.x, label.frame.origin.y);

    NSDictionary *attributesDictionary = @{NSFontAttributeName:label.font};
    CGSize maximumLabelSize = CGSizeMake(label.frame.size.width, maxHeight);
    CGRect expectedLabelRect = [label.text boundingRectWithSize:maximumLabelSize options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading) attributes:attributesDictionary context:nil];
    CGSize expectedLabelSize = expectedLabelRect.size;
    [label setFrame:CGRectMake(currentLabelSize.width, currentLabelSize.height, expectedLabelSize.width, expectedLabelSize.height)];
}

@niveshchauhan
Copy link

i only wanted to know the size of the label reduces according to the data but it is alwaz in single line....max 45 charc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment