Skip to content

Instantly share code, notes, and snippets.

@ktakayama
Created April 17, 2012 04:11
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ktakayama/2403396 to your computer and use it in GitHub Desktop.
#import <Foundation/Foundation.h>
@interface UILabel (ktlib)
- (void) fitSizeOfFont:(UIFont *)font;
@end
#import "UILabel+ktlib.h"
@implementation UILabel (ktlib)
- (void) fitSizeOfFont:(UIFont *)font {
NSString *label = self.text;
CGFloat width = self.frame.size.width;
CGFloat height = self.frame.size.height;
CGSize constraintSize = CGSizeMake(width, MAXFLOAT);
CGSize labelSize;
int i;
for(i = font.pointSize; i > self.minimumFontSize; i--) {
font = [font fontWithSize:i];
labelSize = [label sizeWithFont:font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
if(labelSize.height <= height)
break;
}
self.font = font;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment