Last active
August 29, 2015 13:56
-
-
Save WingedDoom/8859406 to your computer and use it in GitHub Desktop.
Another version of danielphilips' UIlabel+dynamicSizeMe files (https://gist.github.com/danielphillips/1005520) optimized for iOS 7
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 (dynamicSizeMe) | |
-(float)resizeToFit; | |
-(float)expectedHeight; | |
@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+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:NSLineBreakByCharWrapping]; | |
UIFont *font = [UIFont systemFontOfSize:14.0]; // 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; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment