UILabel wrapping incorrectly in a UICollectionView - ios

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.

Related

Why UILabel's height become zero if doesn't hold any text?

I expected from the label to keep the minimal height of one line even if you remove the text out of it.
Here's the picture:
How it should look (but without whitespace):
If I enter the one whitespace in it, its height gets corrected. But I dont want to put whitespace into empty labels. there should be a better solution.
The UILabel control have an intrinsic content size so if you don't have any text his height will be zero, If you need a min height then you must define a Height constraint with >= your minValue as I said in my comments
I think you can figured it out how do this
First, add a Width and a Height constraints:
Second, change the Equal to the Bigger Than(both for width and height, height could be small one, like 8):
Finally, you are able to change the font size of label freely, the label will became bigger with the bigger font size. And the label size will be (42,8) if you removed text:
One workaround is to use a UITextField instead, set isEnabled to false, and set the placeholder property to " ".
As long as you don't need it's width to ever be shorter than the width of a single space -- UITextFields' sizes are calculated using the placeholder property by default -- then this will always have the height of a non-empty UITextField regardless of whether it contains text or not.

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.

Unique multiline UILabel looks using auto layout

I have a multiline UILabel that I'd like to look similar on different devices (iPhone only). If it was a single line, I'd simply place the auto layout constraints and enable Autoshrink and set the Minimum Font Scale.
The height of the label is dynamic is calculated based on text. To do that, I need the font, which should also be dynamic. The hack I could do is place an invisible label, and set the text I want to fit in a single line into it and calculate the font, but it seems too hacky.
Another thing I'm not that familiar with are size classes. But from what I've read, the same class is shared between all portraits, meaning 3.5, 4, 4.7 and 5.5 inch devices would be bound to the same class, therefore I couldn't use the separate font value?
How would I implement the 'font scaling' for multiline labels so I'm getting similar look on different screen sizes?
If by similar look you mean that the same words appear on each line, I don't think it's possible.
If the label height is dynamic based on the amount of text, the label will simply expand to the height required to show the text with the specified font size.
Font scaling only kicks in when there is not enough space to display the text with the specified font size. Therefore, you must constrain the height or number of lines.
I was able to approximate this using a multiline UILabel with font scale of .5. I set an Equal Heights constraint between it and its superview, with a multiplier of .25. See screenshots below.
In this approach, you would have to dynamically change the Equal Heights multiplier based on the amount of text you have.

Auto size UILabel text

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.

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