Created
June 2, 2011 22:54
-
-
Save danielphillips/1005520 to your computer and use it in GitHub Desktop.
Adjust UILabel to change it's frame according to it's content
This file contains 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 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: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
i only wanted to know the size of the label reduces according to the data but it is alwaz in single line....max 45 charc