How to Calculate Text Size Prior to Creating a CustomCell - iphone

I'm creating a CustomCell that contains a UILabel, by default the UILabel will have two lines of text with wrap around enabled, but there are occasions when the text will require three lines.
The font type and size is fixed and cannot be changed, and I trying to identify a way of calculating the length of the NSString/UILabel prior to creating the UITableView/CustomCell so that the cell height can be set correctly. The text that will be displayed will be made up of a number of different words e.g. 'Your name is XXXXX XXXXX and your birthday is..' and the XXXXXXX is the element that is variable.
Hopefully this makes sense, one idea I have considered is creating a method that contains a UILabel that is never displayed and populating it with the required text and then checking if 2 or 3 lines are used, but not sure how to do this.
Is there a more elegant method of achieving this?

There are several UIKit functions for calculating with size of a string with various linebreak modes and fonts. See this doc for details.
In particular sizeWithFont:constrainedToSize:lineBreakMode: may be useful for this.

I believe NSStrings sizeWithFont: constrainedToSize: lineBreakMode: is the method you are looking for. This returns a CGSize which you can get the height of to determine your cell's height (and set the label's frame to the correct size obviously).
Note: you should pass a CGSize parameter in to the constrainedToSize: part that is a larger height than what you intend it to be but the correct maximum width of the label

Related

UILabel: Get actual font size with adjustFontSizeToFit

I have two labels in my layout.
I want the font size to adjust to fit the label bounds.
I set to true the UILabel adjustFontSizeToFit.
Moreover a need the two labels to have the same font size. So I added an auto-layout constraint to make the labels have the same size.
The issue with it is the labels do not contain the same string. Sometimes the first is longer and vice-versa. The font I use doesn't have the same width for every character which will not allow me to use a space to fill to shorter string. (This solution looks a bit ugly to me).
I tried to get the label.font.size.pointSize after the label has been displayed but the value returned is always the one I set at the beginning (before being reduced to fit).
Does anyone have an idea on how I could achieve it ?

How to determine the number of lines being used in UILabel (Swift)?

cell.bodyText.numberOfLines = 0
cell.bodyText.text = newsBody
cell.bodyText.sizeToFit()
I set the number of lines to 0 because the number of lines can vary (I am retrieving news articles and since every news article is obviously different in length, numberOfLines is 0)
If I do not use sizeToFit() after assinging newsBody to the label, and check the label height (cell.bodyText.frame.size.height), I get really large numbers (when I tested it, an article that had 25 lines of text, the height was apparently 4000). If I use sizeToFitand then check the height, I get 21 no matter how short or long the label is. (21 is the height I set the UILabel in the storyboard).
How can I get an accurate representation of how many lines are in the UILabel or even just the height of the label?
I assume you're doing this work in tableView(_:cellForRowAtIndexPath:). That's not reliable for querying final layout. Try using the delegate method tableView(_:willDisplayCell:forRowAtIndexPath:). That's called immediately before displaying the cell, and is the appropriate place to work out any final layout issues since everything has been applied at that point.

How to correctly put labels on one line

I have three UILabel with different font and I want to put then on one line.
The problem is that as the text length of those label is not fixed, I have to change the location of the second and the third label according to the text length of the label before, otherwise the text will be overlapped.
I plan to set the location according to the text length of the label before and I have to recognize the uppercase and the lowercase. I have to consider more if I use other language.
I want to know is there a better method to solve this problem, thanks!
CGSize size = [#"SomeString" sizeWithFont:[UIFont systemFontOfSize:20.0]];
CGFloat width = size.width;
Look into the CoreText Framework. You'll be able to have much more control over your text characteristics than using UILabels.
Developer Reference:
https://developer.apple.com/library/mac/#documentation/StringsTextFonts/Conceptual/CoreText_Programming/Introduction/Introduction.html
Tutorial:
http://www.raywenderlich.com/4147/how-to-create-a-simple-magazine-app-with-core-text

iPhone: UILabel text does not fit the way i want

I have a UILabel with some text on it, what I want is if the text with the given font does not fit to the label, I want it to be first linebreaked to a second line, and if still does not fit then it should automatically adjust those 2 lines to a smaller font.
I experienced with the IB changing the settings of linebreaks and number of rows, but couldn't get what I want.
Any recommendadtions?
To my knowledge UILabel does not support auto adjusting the font when there is more than one line.
The only way I know of it to iteratively calculate a fitting font size and then to set the appropriate font manually.
Maybe the sizeWithFont: method is a solution for you:
– sizeWithFont:forWidth:lineBreakMode:
This calculates the width / height of a NSString with the appropriate font / settings

Always getting the same height using sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:

The CGSize returned by sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode: contains always the same height. Why is that, and is there a way around this?
I want to align a string vertically and it may not be truncated, unless it can't fit on a single line using the minimum font size. So I try to use this method to get the line height but it always returns 57px no matter what the actualFontSize is.
Any ideas?
Once you have the actualFontSize from sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:
Then you can calculate the height required to render using:
CGFloat actualHeight = [font fontWithSize:actualFontSize].ascender-[font fontWithSize:actualFontSize].descender;
(I found this page useful in understanding the various size values relating to fonts.)
I believe you are misunderstanding the actualFontSize parameter. This is an out parameter only, not an in parameter. It does not matter what value you provide for actualFontSize; if the pointer is non-NULL, then it will be overwritten with the font size used in determining the returned size. The returned actual font size will be between the minFontSize: and the size of the UIFont instance provided as the argument to the sizeWithFont: portion of the selector.
You also must be sure to send this message to the string you intend to render. You would always get the same value, for example, if you are asking a dummy string that's less than a line long how much height it would take for the supplied width.
If you update your question with the actual code used in calling the method, everyone would be better able to help you.