Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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
@kballard

Why does your -expectedHeight method change the number of lines and line break mode?

@danielphillips

Hi Kevin, in order to correctly calculate what the height of the newly stretched UILabel will be, we need to set the number of lines to zero, this means that the number of lines can be anything, by using zero it lets the text fill as many lines as it needs, it's sort of a wildcard. Also in order for the text to actually fill the lines, it's necessary to change the line break mode to something that will cause a line break when the text reaches the set width.

The line break mode could be UILineBreakModeCharacterWrap but shouldn't be anything else such as a clip mode or truncation, because in this case the content wouldn't fill out the lines anyway rendering finding the expected height impossible.

@kballard

Daniel, the method you have is called -expectedHeight. Presumably, it returns the height the label can be expected to have when it's resized to fit its contents. The problem is, -expectedHeight is changing the height it can be expected to have when you call it. Say I have a label with enough text to fill 3 lines, but I have numberOfLines set to 2. It'll only render two lines. Say I resize the label to be tall enough that I see 2 lines plus a few "lines" of empty space. Now I call -resizeToFit, and instead of resizing to fit those two visible lines (remember, I configured the label to only have 2 lines), it suddenly renders a third line of text. That's just wrong.

Instead -expectedHeight should just use the current values of numberOfLines and lineBreakMode and return the height that the label will render using those values. This allows the user to configure the label however they want and get correct behavior.

@Diwann

danielphillips : would you have a solution to calculate the minimum width for a fixed height ?

@danielphillips

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

@Diwann

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.

@drkmullins

Thanks for this!

@SashaBugayev

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

@skyebook

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.

@zaveri

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

eg: [_yourLabel setPreferredMaxLayoutWidth:200.f];

http://developer.apple.com/library/ios/#documentation/uikit/reference/UILabel_Class/Reference/UILabel.html#//apple_ref/occ/instp/UILabel/preferredMaxLayoutWidth

@pokono

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

@bobmoff

Nice cat(egory)!

I added it to cocoacats.com

@berkus

Shorter version:

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

@Abizern

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.

@ajubbal

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

@WingedDoom

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;

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.