Last active
April 18, 2017 03:50
-
-
Save ayushn21/d87b835b2efc756e859f to your computer and use it in GitHub Desktop.
A small category on UILabel to allow for auto shrinking of text for multi line UILabels. Based on the implementation described here: http://www.11pixel.com/blog/28/resize-multi-line-text-to-fit-uilabel-on-iphone/
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
@implementation UILabel (AutoShrink) | |
- (void)resizeTextToFit | |
{ | |
int currentFontSize = (int) self.font.pointSize; | |
int minimumFontSize = (int) (self.minimumScaleFactor*currentFontSize); | |
UIFont *font = self.font; | |
for(int i = currentFontSize; i > minimumFontSize; i=i-2) | |
{ | |
font = [font fontWithSize:i]; | |
CGRect boundingRect = [self.text boundingRectWithSize:CGSizeMake(self.bounds.size.width, MAXFLOAT) | |
options:NSStringDrawingUsesLineFragmentOrigin | |
attributes:@{NSFontAttributeName:font} | |
context:nil]; | |
if (boundingRect.size.height <= self.bounds.size.height) break; | |
} | |
self.font = font; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment