I want to auto resize the text size of a UILabel with numberOfLines>1. The UILabel width and height have to be fix. Is there a better solution instead of counting the characters and setting the size manualy? I am using iOS6.
Asked
Active
Viewed 3,353 times
2
-
Please refer this previous SO answer – pradeepa Jan 18 '13 at 12:50
-
see http://agilewarrior.wordpress.com/2012/06/15/how-to-multiline-label-ios-objective-c/ – Rachel Gallen Jan 18 '13 at 12:56
-
you can do it programmatically, the iOS does not adjust the font size for the multiline `UILabel` views. – holex Jan 18 '13 at 12:57
2 Answers
4
I modified a solution I found. This can be used now in a UITableViewCell:
- (void)resizeFontForLabel:(UILabel*)aLabel maxSize:(int)maxSize minSize:(int)minSize lblWidth: (float)lblWidth lblHeight: (float)lblHeight {
// use font from provided label so we don't lose color, style, etc
UIFont *font = aLabel.font;
// start with maxSize and keep reducing until it doesn't clip
for(int i = maxSize; i >= minSize; i--) {
font = [font fontWithSize:i];
CGSize constraintSize = CGSizeMake(lblWidth, MAXFLOAT);
// NSString* string = [aLabel.text stringByAppendingString:@"..."];
CGSize labelSize = [aLabel.text sizeWithFont:font constrainedToSize:constraintSize lineBreakMode:aLabel.lineBreakMode];
if(labelSize.height <= lblHeight)
break;
}
// Set the UILabel's font to the newly adjusted font.
aLabel.font = font;
}

tuvok
- 185
- 2
- 10