Auto size UILabel text - ios

I have a UILabel with multi lines. I'm trying to get the text to auto size, and fit on the line it's in. So instead of:
longe
st
The text size should become smaller so that it can fit on the same line:
longest
Note: I only need it for the first line.
I tried the following:
label.adjustsFontSizeToFitWidth = YES;
label.minimumScaleFactor=3.0;
That didn't work, so I tried label.minimumScaleFactor=3.0 / 9; (9 is the font size.) That also didn't work.

I have a UILabel with multi lines
Well, that's the problem. The automatic size adjustment feature of UILabel works only for 1-line UILabels (i.e. numberOfLines must be 1).
You might be happier with two labels, one being a single-line UILabel for the first line which can shrink its size, the other for the remaining lines.

Related

iOS UILabel bounding rectangle not sized correctly

I have a UILabel (as highlighted in yellow), which has the following conditions applied to it.
Label has variable text length
Font set to 40
Minimum font-size set to 20
Number of lines set to 3
Although this looks like a duplicated question I believe it is not. The issue I am having is that when the text exceeds the available 3 line length after being sized-down to 20 points, the UILabel's bounding box is sized incorrectly (i.e. note the extraneous spacing above and below the text).
The end result should be a UILabel without any spacing. Is there are solution to this, while keeping the number of lines set to 3?
That looks like a bug. If you increase your base font size, you will see the space increase. Also, if you inspect the layout at runtime, you will see the content size to be calculated as too big.
My guess is, UILabel takes your original font size (40) to calculate the content size for 3 lines of text and does not take into account that the font size has already been decreased before truncation.
I fiddled with content hugging/compression priorities but could not make it work either.
The only workaround I found was to manually set the font size down to 20. That will get you the frame you want.

How to truncate UILabel without affecting line spacing?

I need to truncate some multi-line labels with ellipsis. However, setting
mylabel.lineBreakMode = .ByTruncatingTail
mylabel.adjustsFontSizeToFitWidth = false
reduces line spacing. Is there a way to keep the original line spacing?
Ended up truncating manually by whitespace which has two advantages:
words are not broken in the middle, and
sizeToFit sets height more precisely
(I need not-more-than certain height and the label has shadow so exact height is unwanted).

UILabel wrapping incorrectly in a UICollectionView

Here I have a UICollectionViewCell, with a UILabel. My labels are character wrapping long words onto a second line. I thought autoshrink would take precedence over line breaks, since "Cappuccino" can easily be autoshrunk onto just 1 line. I even have Word Wrap on.
Why is this label being character wrapped as opposed to autoshrunk with a minimum font size?
EDIT: I'm aware that numberOfLines needs to be > 0, but programmatically determining numberOfLines based on a label's text and bounding rect doesn't consider autoshrink. I can check if the font size with 1 line != my desired font size, and adjust numberOfLines by guessing, but there's no isTruncated property for me to tell whether the line has broken. So the right question is, how can I programmatically account for autoshrink while determining an appropriate numberOfLines?
you must set a number of lignes > 0 when using this config for all UI text containers.

Multi-line UILabel with auto layout, how to adjust font size according to content without changing label's frame?

I have a multi-line UILabel, which will display contents at run time, contents may only need 1 line or up to 3 lines.
When there is less contents, I'd like the label using a larger font size, and when there are more contents, I want the label to use multiple lines and smaller font size.
Due to autolayout, I can't set the frame of UILabel, and the frame of the label is unknown until run time, so that it can be compatible with different devices.
Currently I set line number of the label as a fixed number, which will divide contents into multiple lines when there is more contents, but when there is less contents, the single line uses a font size as small as when there is more contents. This is not the best experience.
set num of lines and font scale factor 0.5 or 0.75
If your content is small it would use the occupied lines and font size will be the actual size. When your content grows these properties will check if with actual fontsize the content can be adjusted to all lines or not. if its not adjustable with full size then font will be scalled to adjust in contentview.

UILabel text is not displaying properly

I want to show the text in uilabel. The text is ,000124520.061112782.,3299985343, I have assign line break mode to character wrap and number of lines to 0. But in label first line is displayed only the , and then other characters are displayed in next line. But i want to display the text in two lines only. But it is displayed in three lines. I don't know why , is displayed in first line. Please anyone help me.
You should set the numberOfLines of your UILabel to 1.
When the numberOfLines property of a UILabel is set to 0 the UILabel will render its text over as many lines as it needs - e.g. its infinite. To fix your text to a single line you should therefore set the property to 1.
Obviously your text may not all fit on a single line so you should also set the UILabel's minimumScaleFactor property to allow the UILabel to adjust its font size downward (to the supplied limit) to fit the content into the frame of the label. You will also need to set the UILabel's adjustsFontSizeToFitWidth property to YES.
Further to the above an easy way to calculate the value for the minimumScaleFactor property is to divide the minimum font size by the maximum font size. So, say for example your label's default font size was 12 and you were happy for it to drop to 10, then assign the minimum scale factor as follows:
myLabel.minimumScaleFactor = 10/12.0f;

Resources