I have a UILabel
whose property text
I would like to set paragraphs of text to using NSString
.
I have an array in which I store a
In storyboard click on adjust to fit and pick a minimum size.
+(void)resizeFontForLabel:(UILabel*)aLabel{
// use font from provided label so we don't lose color, style, etc
UIFont *font = aLabel.font;
float lblWidth = aLabel.frame.size.width;
float lblHeight = aLabel.frame.size.height;
CGFloat fontSize = [font pointSize];
UIFont *newFont = font;
TRC_DBG(@"%@", aLabel.text);
CGFloat height = [aLabel.text sizeWithFont:font constrainedToSize:CGSizeMake(lblWidth,MAXFLOAT) lineBreakMode:aLabel.lineBreakMode].height;
TRC_DBG(@"Label Height %f Constraint height %f", lblHeight, height);
//Reduce font size while too large, break if no height (empty string)
while (height > lblHeight && height != 0) {
fontSize--;
newFont = [UIFont fontWithName:font.fontName size:fontSize];
height = [aLabel.text sizeWithFont:newFont constrainedToSize:CGSizeMake(lblWidth,MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping].height;
TRC_DBG(@"Constrained Height %f", height);
};
TRC_DBG(@"Font size before adjustment %f", aLabel.font.pointSize);
// Set the UILabel's font to the newly adjusted font.
aLabel.font = newFont;
TRC_DBG(@"Adjust to font size of %f", newFont.pointSize);
[aLabel setNeedsLayout];
}