Skip to content

Instantly share code, notes, and snippets.

@WingedDoom
Last active August 29, 2015 13:56
Show Gist options
  • Save WingedDoom/8859406 to your computer and use it in GitHub Desktop.
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
@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: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