Calculate max font size that fits in a rect? - ios

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;
}

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)]

It may be impossible to fill a rectangle completely.
Say at a certain font size you have two lines of text, both filling the screen horizontally, but vertically you have almost but not quite three lines of space.
If you increase the font size just a tiny bit, then the lines don't fit anymore, so you need three lines, but three lines don't fit vertically.
So you have no choice but to live with the vertical gap.

Related

How can I set same font-scale for multiple labels depending on which one is autoshrinked?

I have 4 label views of which one is supposed to show large numerical values and is set for autoshrink.
My requirement would be to set the same font-scaling or size this label has, after its been auto-adjusted to fit its content, to the other labels too so that the text content looks uniform throughout.
Setting the minimum scale factor didn't help for the other labels because they have content within the frame limits.
There's no way to do this directly, since querying the font of the label where the text has been shrunk to fit, still shows the original font size. You have to do it by iterating over smaller and smaller font sizes until you find the size that fits in your label, and then use that font size to adjust your other labels. In my example below, labelLong is the one whose text can shrink, and labelShort is the one whose text doesn't need to shrink.
-(void)updateFont {
NSStringDrawingContext *ctx = [NSStringDrawingContext new];
ctx.minimumScaleFactor = 1.0;
UIFont *startingFont = self.labelLong.font;
NSString *fontName = startingFont.fontName;
CGFloat startingSize = startingFont.pointSize;
for (float i=startingSize*10; i>1; i--) { // multiply by 10 so we can adjust font by tenths of a point with each iteration
UIFont *font = [UIFont fontWithName:fontName size:i/10];
CGRect textRect = [self.labelLong.text boundingRectWithSize:self.labelLong.frame.size options:NSStringDrawingTruncatesLastVisibleLine attributes:#{NSFontAttributeName:font} context:ctx];
if (textRect.size.width <= [self.labelLong textRectForBounds:self.labelLong.bounds limitedToNumberOfLines:1].size.width) {
NSLog(#"Font size is: %f", i/10);
self.labelShort.font = [UIFont fontWithName:fontName size:i/10];
break;
}
}
}

How much text a label can adopt

In my project there is a specific requirement, When UILabel's text gets truncated I need to give view more functionality. Initially there will be a CGRect given. Accordingly we need to show the label if text truncated we need to at the end of label ...View more text should be shown. Upon tapping on ...View more view more I need to make my label bigger. So I m doing
NSMutableString *truncatedString = [text mutableCopy];
[truncatedString appendString:ellipsis];
NSRange range = NSMakeRange(truncatedString.length - (ellipsis.length + 1), 1);
do {
[truncatedString deleteCharactersInRange:range];
range.location--;
[self setText:truncatedString];
} while ([self isTextTruncated]);
it works fine for smaller text since I m using it for UITableViewCell. It is lagging for bigger texts since above operation happens for every time. So I want to know the text that is adopted in UILabel so that I can do any operation with new text. Any help would be greatly appreciated.
EDIT:
I have a label and bigger text. say my text is
"Apple Inc. is an American multinational corporation headquartered in Cupertino, California, that designs, develops, and sells consumer electronics, computer software, online services, and personal computers." if my label would adopt only "Apple Inc. is an American multinational corporation.." I need this text alone
Use this method to calculate height that would be required for the text to get fit into the provided UILabel:
- (CGFloat)getLabelHeight:(UILabel*)label
{
CGSize constraint = CGSizeMake(label.frame.size.width, 20000.0f);
CGSize size;
NSStringDrawingContext *context = [[NSStringDrawingContext alloc] init];
CGSize boundingBox = [yourString boundingRectWithSize:constraint
options:NSStringDrawingUsesLineFragmentOrigin
attributes:#{NSFontAttributeName:label.font}
context:context].size;
size = CGSizeMake(ceil(boundingBox.width), ceil(boundingBox.height));
return size.height;
}
Compare the returned height with height of your label:
CGFloat heightRequired = [self getLabelHeight:myLabel];
if(myLabel.frame.size.height < heightRequired) {
//you need to show more because the text is more than the label width and height.
}
else {
//you don't need to show more because the text is not more than the label width and height.
}
EDIT: The purpose of comparing height is to check whether frame is enough to show text or not. So, even if you want to increase the width of label to show more text, it will give you desired result.
You need to calculate size of text as bellow and if returned size is bigger than text field size than you need to show ...View more
CGSize requiredSize = [text sizeWithFont:withFont constrainedToSize:textViewSize lineBreakMode:lineBreakMode];
If you didn't find an answer, i recommend this workaround:
By trying using a text with known length, get the max number of characters that the label fits,and use that value to do the following:
int maxNumOfChar = 15; //For example
if (text.length > maxNumOfChar){
NSString* viewMore = #"...View More";
text = [[text substringToIndex:maxNumOfChar - [viewMore length]] stringByAppendingString: viewMore];
}

Get UILabel font pointsize after minimumFontSize

I have a UILabel with a font of point size 17. If I call label.font.pointSize I get 17, which is all good. BBUUUUTTT I also have a minimumfontsize set to 8, now if I cram some text in the label which causes the point size to shrink and then call label.font.pointsize I still get 17 even though I know the point size is smaller
Any ideas how to get the true point size after system has resized the font?
I don't know of an API to get the current point size of the UILabel when it is scaling your content down. You can try to approximate "scaling factor" using sizeWithFont APIs.
Just an idea:
// Get the size of the text with no scaling (one line)
CGSize sizeOneLine = [label.text sizeWithFont:label.font];
// Get the size of the text enforcing the scaling based on label width
CGSize sizeOneLineConstrained = [label.text sizeWithFont:label.font constrainedToSize:label.frame.size];
// Approximate scaling factor
CGFloat approxScaleFactor = sizeOneLineConstrained.width / sizeOneLine.width;
// Approximate new point size
CGFloat approxScaledPointSize = approxScaleFactor * label.font.pointSize;
As savner pointed out in the comments, this is a duplication question. The cleanest solution is found here: How to get UILabel (UITextView) auto adjusted font size?. However, Sanjit's solution also works! Thanks Everybody!
CGFloat actualFontSize;
[label.text sizeWithFont:label.font
minFontSize:label.minimumFontSize
actualFontSize:&actualFontSize
forWidth:label.bounds.size.width
lineBreakMode:label.lineBreakMode];
Swift 4 and iOS 7+ version (sizeWithFont is now deprecated) of #Sanjit Saluja's answer:
// Get the size of the text with no scaling (one line)
let sizeOneLine: CGSize = label.text!.size(withAttributes: [NSAttributedStringKey.font: label.font])
// Get the size of the text enforcing the scaling based on label width
let sizeOneLineConstrained: CGSize = label.text!.boundingRect(with: label.frame.size, options: .usesLineFragmentOrigin, attributes: [NSAttributedStringKey.font: label.font], context: nil).size
// Approximate scaling factor
let approxScaleFactor: CGFloat = sizeOneLineConstrained.width / sizeOneLine.width
// Approximate new point size
let approxScaledPointSize: CGFloat = approxScaleFactor * label.font.pointSize

Positioning a UILabel directly beneath another UILabel

I have an addition to NSString which automatically resizes a UILabel depending on the text that's being read into it (I have a simple app showing quotations, so some are a few words, some a couple sentences). Below that quote label, I also have an author label, which (oddly enough) has the author of the quote in it.
I'm trying to position that author label directly beneath the quote label (as in, its y coordinate would be the quote label's y coordinate plus the quote label's height. What I'm seeing is some space being placed between the two labels, that depending on the length of the quote, changes size. Smaller quotes have more space, while longer quotes have less space. Here's a quick diagram of what I'm seeing:
Note the gap between the red and blue boxes (which I've set up using layer.borderColor/borderWidth so I can see them in the app), is larger the shorter the quote is.
If anyone can sift through the code below and help point me towards exactly what's causing the discrepancy, I'd be really grateful. From what I can see, the author label should always be 35 pixels beneath the quote label's y + height value.
Just to confirm: everything is hooked up correctly in Interface Builder, etc. The content of the quote's getting in there fine, everything else works, so it's hooked up, that isn't the issue.
To clarify, my question is: Why is the gap between the labels changing dependant on the quote's length, and how can I get a stable, settable gap of 35 pixels correctly?
Here's the code I'm using to position the labels:
// Fill and format Quote Details
_quoteLabel.text = [NSString stringWithFormat:#"\"%#\"", _selectedQuote.quote];
_authorLabel.text = _selectedQuote.author;
[_quoteLabel setFont: [UIFont fontWithName: kScriptFont size: 28.0f]];
[_authorLabel setFont: [UIFont fontWithName: kScriptFontAuthor size: 30.0f]];
// Automatically resize the label, then center it again.
[_quoteLabel sizeToFitMultipleLines];
[_quoteLabel setFrame: CGRectMake(11, 11, 298, _quoteLabel.frame.size.height)];
// Position the author label below the quote label, however high it is.
[_authorLabel setFrame: CGRectMake(11, 11 + _quoteLabel.frame.size.height + 35, _authorLabel.frame.size.width, _authorLabel.frame.size.height)];
Here's my custom method for sizeToFitMultipleLines:
- (void) sizeToFitMultipleLines
{
if (self.adjustsFontSizeToFitWidth) {
CGFloat adjustedFontSize = [self.text fontSizeWithFont: self.font constrainedToSize: self.frame.size minimumFontSize: self.minimumScaleFactor];
self.font = [self.font fontWithSize: adjustedFontSize];
}
[self sizeToFit];
}
And here's my fontSizeWithFont:constrainedToSize:minimumFontSize: method:
- (CGFloat) fontSizeWithFont: (UIFont *) font constrainedToSize: (CGSize) size minimumFontSize: (CGFloat) minimumFontSize
{
CGFloat fontSize = [font pointSize];
CGFloat height = [self sizeWithFont: font constrainedToSize: CGSizeMake(size.width, FLT_MAX) lineBreakMode: NSLineBreakByWordWrapping].height;
UIFont *newFont = font;
// Reduce font size while too large, break if no height (empty string)
while (height > size.height && height != 0 && fontSize > minimumFontSize) {
fontSize--;
newFont = [UIFont fontWithName: font.fontName size: fontSize];
height = [self sizeWithFont: newFont constrainedToSize: CGSizeMake(size.width, FLT_MAX) lineBreakMode: NSLineBreakByWordWrapping].height;
};
// Loop through words in string and resize to fit
for (NSString *word in [self componentsSeparatedByCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]) {
CGFloat width = [word sizeWithFont: newFont].width;
while (width > size.width && width != 0 && fontSize > minimumFontSize) {
fontSize--;
newFont = [UIFont fontWithName: font.fontName size: fontSize];
width = [word sizeWithFont: newFont].width;
}
}
return fontSize;
}
After you called size to fit on both labels, calculate the distance between their frames and change them accordingly:
[quoteLabel sizeToFit];
[authorLabel sizeToFit];
float distance = authorLabel.frame.origin.y - quoteLabel.frame.size.height;
float difference = distance - 35;
authorLabel.frame = CGRectMake(authorLabel.frame.origin.x,(authorLabel.frame.origin.y - difference),authorLabel.frame.size.width,authorLabel.frame.size.height);
The reason the gap changes is that the quote label frame changes its height dependent on its content when you call sizeToFit.
UPDATE
Given the recent developments in the comments, I think you have 3 possibilities:
resize the whitespace instead of only the words, so that the string
actually fits in the frame correctly
somehow access the CTFramesetter of UILabel to see what the actual
frame, when all is said and done, amounts to
make your own UIView subclass that handles Core Text drawing in its
draw rect method (should be easy in your case), since after all you
are trying to give to UILabel a behavior that it's not meant for
It probably is moving where you want it, but then an auto-layout constraint or a spring/strut is moving it afterwards.
EDIT:
My first thought (which I ruled out because you said that the box around the words was the label frame. In later comments, you say that this is not an actual screen shot, but just a representation of it, so it could still be correct) was that you are doing this wrong:
[_quoteLabel sizeToFitMultipleLines];
[_quoteLabel setFrame: CGRectMake(11, 11, 298, _quoteLabel.frame.size.height)];
In the first line, you are sizing the text to fit in whatever the current width of the label might be, and then you turn around in the second line and change the width of the label. So most likely, what is happening is that you are sizing the label for some smaller width, which makes it tall. You then make the label wider than it was before and the text expands to fit the wider label, leaving a blank area beneath the actual text, although the frame has not changed. This makes the space betwee the labels exactly 35 as you want, however the top label's text does not go all of the way to the bottom of its frame so the white space is more than you want. Basically, you have this:
*************
* text text *
* text text *
* *
* *
* *
*************
*************
* text text *
*************
If this is the case, then you would fix it by setting the width first, like this:
// You could put anything for the 200 height since you will be changing it in the next line anyway.
[_quoteLabel setFrame: CGRectMake(11, 11, 298, 200];
[_quoteLabel sizeToFitMultipleLines];
I ended up solving the problem by using a single UILabel, and CoreText with an NSAttributedString. Kind of a cop-out, but it works.

determining UIFont's pointSize for a specific CGSize

I have a specific CGSize of a UILabel, where I cannot expand the frame of UILabel and since it is a multi-line UILabels adjustsFontSizeToFitWidth method does not work.
So I figured I should create such function which looked like;
- (CGFloat)fontSizeWithText:(NSString*)text andFont:(UIFont*)font constrainedSize:(CGSize)size LBM:(UILineBreakMode)LBM
{
// check if text fits to label
CGSize labelSize = [text sizeWithFont:font constrainedToSize:CGSizeMake(size.width, 9999) lineBreakMode:LBM];
// if not, decrease font size until it fits to the given size
while (labelSize.height > size.height) {
font = [UIFont fontWithName:font.fontName size:font.pointSize - 0.5];
labelSize = [text sizeWithFont:font constrainedToSize:CGSizeMake(size.width, 9999) lineBreakMode:LBM];
}
return font.pointSize;
}
Usage:
// fit detail label by arranging font's size
CGFloat fontSize = [self fontSizeWithText:self.titleLabel.text andFont:self.titleLabel.font constrainedSize:self.titleLabel.frame.size LBM:self.titleLabel.lineBreakMode];
self.titleLabel.font = [UIFont fontWithName:self.titleLabel.font.fontName size:fontSize];
But with this method, I see that some of my texts doesn't fit to the UILabel s frame and gets truncated. There must be something I am missing. Any help for the code or any other suggestions on resolving "fitting a text with given font to a specific multi-line UILabel" would be great.
First lets quickly take a look at the problem as a whole. You're trying to fit text into a predefined frame and adjust the font size. This generally will not work terribly well, as you will quickly hit sizes FAR too small to read, even on a retina display. You should adjust the frame of your label to accommodate the excess text (where possible. Sometimes, truncation is the only option.)
Now that, that is out of the way, lets take a look at adjusting the font size. Despite not recommending it, I will still explain how best to go about it.
Important, this code is untested, and will more than likely require some tweaks, but that can be an exercise of the reader.
So the first thing we need to know is the height of a single line. Now, we have the height of the label, and the number of lines it can display, so we can determine this by simply dividing the label height by the number of lines.
CGFloat optimalLineHeight = CGRectGetHeight(label.frame) / label.numberOfLines;
You may have noticed that this may return lines taller than are actually needed. You will be able to implement additional checks and constraints to deal with this. At current though, the font size will also be able to grow, and not just shrink.
Now, getting the optimal line height is just part of the story. We now need to optimise the font size. Here's some code:
CGFloat optimumFontSize = 0.0;
BOOL optimumFontSizeFound = NO;
do {
CGSize charSize = [#"M" sizeWithFont:[UIFont systemFontOfSize:optimumFontSize]
constrainedToSize:CGSizeMake(100, 9999)
lineBreakMode:0];
if ( CGSizeGetHeight(charSize) > optimalLineHeight ) {
optimumFontSizeFound = YES;
}
else {
optimumFontSize++;
}
} while ( !optimumFontSizeFound );
So what does this do? In this we keep track of the optimumFontSize so far. We start with the assumption of a font size of 0, and we see how tall a single character using that font size is. If that height is greater than the optimal line height previously calculated, then the previous height is the optimal one. If not, we increase the size and repeat until we do find the optimal one.
There are still a lot of issues to overcome in this to make it work perfectly in all situations. This should ensure that you don't get visible vertical clipping of characters, but it can't ensure that all the text content will display in the frame. To do that you'll need to be more intelligent in how you determine the number of lines required, but again I'll leave that as an exercise of the reader.
Hope this helps you towards your goal.

Resources