Created
May 22, 2012 01:56
-
-
Save kevboh/2766074 to your computer and use it in GitHub Desktop.
Making UILabel's adjustsFontSizeToFitWidth and minimumFontSize play nice with multi-lined labels
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 NSString (KBAdditions) | |
- (CGFloat)fontSizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size minimumFontSize:(CGFloat)minimumFontSize; | |
@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 "NSString+KBAdditions.h" | |
@implementation NSString (KBAdditions) | |
// Original code from http://stackoverflow.com/a/4383281/463892 | |
- (CGFloat)fontSizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size minimumFontSize:(CGFloat)minimumFontSize { | |
CGFloat fontSize = [font pointSize]; | |
CGFloat height = [self sizeWithFont:font constrainedToSize:CGSizeMake(size.width,FLT_MAX) lineBreakMode:UILineBreakModeWordWrap].height; | |
UIFont *newFont = font; | |
//Reduce font size while too large, break if no height (empty string) | |
while (height > size.height && height != 0 && fontSize > minimumFontSize) { | |
fontSize--; | |
newFont = [UIFont fontWithName:font.fontName size:fontSize]; | |
height = [self sizeWithFont:newFont constrainedToSize:CGSizeMake(size.width,FLT_MAX) lineBreakMode:UILineBreakModeWordWrap].height; | |
}; | |
// Loop through words in string and resize to fit | |
for (NSString *word in [self componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]) { | |
CGFloat width = [word sizeWithFont:newFont].width; | |
while (width > size.width && width != 0 && fontSize > minimumFontSize) { | |
fontSize--; | |
newFont = [UIFont fontWithName:font.fontName size:fontSize]; | |
width = [word sizeWithFont:newFont].width; | |
} | |
} | |
return fontSize; | |
} | |
@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
@interface UILabel (KBAdditions) | |
- (void)sizeToFitMultipleLines; | |
@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+KBAdditions.h" | |
#import "NSString+KBAdditions.h" | |
@implementation UILabel (KBAdditions) | |
- (void)sizeToFitMultipleLines { | |
if (self.adjustsFontSizeToFitWidth) { | |
CGFloat adjustedFontSize = [self.text fontSizeWithFont:self.font constrainedToSize:self.frame.size minimumFontSize:self.minimumFontSize]; | |
self.font = [self.font fontWithSize:adjustedFontSize]; | |
} | |
[self sizeToFit]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fork removing deprecated warnings on iOS7.
https://gist.github.com/mkubenka/d886c534f7082db00540