- (UIFont*)adjustsFontSize:(UILabel*)label{
float curFontSize = label.font.pointSize;
UIFont *font = label.font;
CGRect rect;
rect = [label bounds];
if (rect.size.width == 0.0f || rect.size.height == 0.0f) {
return 0;
}
while (curFontSize > label.minimumScaleFactor && curFontSize > 0.0f) {
CGSize size = CGSizeZero;
// A single line of text should be clipped
if (label.numberOfLines == 1) {
size = [label.text sizeWithFont:font constrainedToSize:CGSizeMake(rect.size.width, 0.0f) lineBreakMode:NSLineBreakByClipping];
// size = [label.text boundingRectWithSize:CGSizeMake(rect.size.width, 0.0f)
// options:NSStringDrawingUsesFontLeading
// attributes:@{NSFontAttributeName: _font}
// context:NULL].size;
} else {
//Multiple lines of text should be wrapped
size = [label.text sizeWithFont:font constrainedToSize:CGSizeMake(rect.size.width, 0.0f) lineBreakMode:NSLineBreakByWordWrapping];
}
if (size.width < rect.size.width && size.height <= rect.size.height) {
break;
}
curFontSize -= 1.0f;
font = [font fontWithSize:curFontSize];
}
if (curFontSize < label.minimumScaleFactor) {
curFontSize = label.minimumScaleFactor;
}
if (curFontSize < 0.0f) {
curFontSize = 1.0f;
}
font = [font fontWithSize:curFontSize];
return font;
}