Created
December 24, 2013 11:53
-
-
Save Ramshandilya/8112232 to your computer and use it in GitHub Desktop.
UILabel category to resize the frame height according to content text.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface UILabel (Resize) | |
- (void)resizeToFit; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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