Skip to content

Instantly share code, notes, and snippets.

@iSevenDays
Forked from danielphillips/UILabel+dynamicSizeMe.h
Last active September 21, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iSevenDays/6d71435dcde594586685 to your computer and use it in GitHub Desktop.
Save iSevenDays/6d71435dcde594586685 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment