I'm attempting to find the maximum font size that will fit in a given rect for a given string. The goal of the algorithm is to fill as much of the rect as possible with as large of a font as possible. My approach -- which is modified from one I found online -- does a fair job, but it often doesn't fill the entire rect. I'd love to see some collaboration on how to improve this algorithm so that everyone might benefit from it:
-(float) maxFontSizeThatFitsForString:(NSString*)_string
inRect:(CGRect)rect
withFont:(NSString *)fontName
onDevice:(int)device
{
// this is the maximum size font that will fit on the device
float _fontSize = maxFontSize;
float widthTweak;
// how much to change the font each iteration. smaller
// numbers will come closer to an exact match at the
// expense of increasing the number of iterations.
float fontDelta = 2.0;
// sometimes sizeWithFont will break up a word
// if the tweak is not applied. also note that
// this should probably take into account the
// font being used -- some fonts work better
// than others using sizeWithFont.
if(device == IPAD)
widthTweak = 0.2;
else
widthTweak = 0.2;
CGSize tallerSize =
CGSizeMake(rect.size.width-(rect.size.width*widthTweak), 100000);
CGSize stringSize =
[_string sizeWithFont:[UIFont fontWithName:fontName size:_fontSize]
constrainedToSize:tallerSize];
while (stringSize.height >= rect.size.height)
{
_fontSize -= fontDelta;
stringSize = [_string sizeWithFont:[UIFont fontWithName:fontName
size:_fontSize]
constrainedToSize:tallerSize];
}
return _fontSize;
}
A font is often measured in pt (points). Points dictate the height of the lettering. There are approximately 72 (72.272) points in one inch or 2.54 cm. For example, the font size 72 would be about one inch tall, and 36 would be about a half of an inch.
CSS doesn't have max-font-size , so if we need something that does something along those lines, we have to get tricky. Why would you need it at all? Well, font-size itself can be set in dynamic ways. For example, font-size: 10vw; .
The maximum font-size available in Microsoft Word 2010 from the dropdown list is 72; however, the font size can be set up to 1638 by typing the size manually for the font.
Use the following method to calculate the font which can fit, for a given rect and string.
You can change the font to the one which you require. Also, If required you can add a default font height;
Method is self explanatory.
-(UIFont*) getFontTofitInRect:(CGRect) rect forText:(NSString*) text {
CGFloat baseFont=0;
UIFont *myFont=[UIFont systemFontOfSize:baseFont];
CGSize fSize=[text sizeWithFont:myFont];
CGFloat step=0.1f;
BOOL stop=NO;
CGFloat previousH;
while (!stop) {
myFont=[UIFont systemFontOfSize:baseFont+step ];
fSize=[text sizeWithFont:myFont constrainedToSize:rect.size lineBreakMode:UILineBreakModeWordWrap];
if(fSize.height+myFont.lineHeight>rect.size.height){
myFont=[UIFont systemFontOfSize:previousH];
fSize=CGSizeMake(fSize.width, previousH);
stop=YES;
}else {
previousH=baseFont+step;
}
step++;
}
return myFont;
}
There is no need to waste time doing loops. First, measure the text width and height at the max and min font point settings. Depending on whichever is more restrictive, width or height, use the following math:
If width is more restrictive (i.e., maxPointWidth / rectWidth > maxPointHeight / rectHeight
) use:
pointSize = minPointSize + rectWidth * [(maxPointSize - minPointSize) / (maxPointWidth - minPointWidth)]
Else, if height is more restrictive use:
pointSize = minPointSize + rectHeight * [(maxPointSize - minPointSize) / (maxPointHeight - minPointHeight)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With