Skip to content

Instantly share code, notes, and snippets.

@Ramshandilya
Created December 24, 2013 11:53
Show Gist options
  • Save Ramshandilya/8112232 to your computer and use it in GitHub Desktop.
Save Ramshandilya/8112232 to your computer and use it in GitHub Desktop.
UILabel category to resize the frame height according to content text.
@interface UILabel (Resize)
- (void)resizeToFit;
@end
#import "UILabel+Resize.h"
@implementation UILabel (Resize)
- (void)resizeToFit
{
CGFloat expectedHeight = [self expectedHeight];
CGRect newFrame = self.frame;
newFrame.size.height = expectedHeight;
[self setFrame:newFrame];
}
- (CGFloat)expectedHeight
{
[self setNumberOfLines:0];
CGSize expectedLabelSize = [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(self.frame.size.width,9999) lineBreakMode:NSLineBreakByWordWrapping];
return expectedLabelSize.height;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment