Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Adjust UILabel to change it's frame according to it's content
@interface UILabel (dynamicSizeMe)
#import "UILabel+dynamicSizeMe.h"
@implementation UILabel (dynamicSizeMe)
float height = [self expectedHeight];
CGRect newFrame = [self frame];
newFrame.size.height = height;
[self setFrame:newFrame];
return newFrame.origin.y + newFrame.size.height;
[self setNumberOfLines:0];
[self setLineBreakMode:UILineBreakModeWordWrap];
CGSize maximumLabelSize = CGSizeMake(self.frame.size.width,9999);
CGSize expectedLabelSize = [[self text] sizeWithFont:[self font]
lineBreakMode:[self lineBreakMode]];
return expectedLabelSize.height;
Copy link

danielphillips commented Jul 13, 2012

That can be done, please let me have a think about it and come back to you in a bit. However, although I understand the geometry of what you're asking, how could this be useful ? You want a label to be as thin as it can be to hold a certain text with a fixed height? Please let me know

Copy link

ErwannRobin commented Jul 13, 2012

It's because I offer the ability to my users to resize their label size but I don't want the text to overstep the frame size. So I am trying to evaluate the text min height for the current width and the text min width for the current height.

no problem for the height with your method. But this is not working very well for the width :
I add a small text to the end of the text to estimate if the future resizment would cause the text to overstep, but it doesn't work if the user has used carriage return.

Copy link

drkmullins commented Aug 6, 2012

Thanks for this!

Copy link

serkanerkan commented Nov 22, 2012

thanks! im a newbie. can you say how can we use this in our projects. i couldnt import.

Copy link

skyebook commented Feb 13, 2013

I know this predates iOS 6, but anyone using this might be interested to know that UILineBreakModeWordWrap is deprecated as of 6.0. Apple recommends the use of NSLineBreakByWordWrapping.

Copy link

zaveri commented Feb 15, 2013

With iOS6 also comes auto layout and constraints. Might want take a look at preferredMaxLayoutWidth

eg: [_yourLabel setPreferredMaxLayoutWidth:200.f];

Copy link

pokono commented Mar 20, 2013

Thanks! I modified this to calculate the width, but it took just a second!

Copy link

bobmoff commented Jul 2, 2013

Nice cat(egory)!

I added it to

Copy link

berkus commented Jul 24, 2013

Shorter version:

[label sizeToFit];
[label sizeThatFits:maxSize];

Copy link

Abizern commented Jul 29, 2013

Daniel, It's also a good idea to use prefixes on method names when you are adding a category to an existing system class. If Apple ever adds resizeToFit this is going to cause breakage.

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.

Copy link

WingedDoom commented Feb 7, 2014

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,

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

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

return expectedLabelSize->height;


Copy link

emotality commented Apr 13, 2016

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)];

Copy link

niveshchauhan commented Nov 2, 2016

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