Skip to content

Instantly share code, notes, and snippets.

@ayushn21
Last active April 18, 2017 03:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ayushn21/d87b835b2efc756e859f to your computer and use it in GitHub Desktop.
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/
@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