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

3

7

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:

enter image description here

How it should look (but without whitespace):

enter image description here

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.

Rosanarosane answered 25/8, 2017 at 15:45 Comment(1)
add a contraint >= your minValueUndulate
S
6

First, add a Width and a Height constraints:

enter image description here

Second, change the Equal to the Bigger Than(both for width and height, height could be small one, like 8):

enter image description here

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:

enter image description here

Sarah answered 25/8, 2017 at 15:49 Comment(10)
but then if i change the font size, the height wont dynamically adopt. In my case i cant have a static definition of a height.Rosanarosane
@KlemenZagar if your font can change you can put a min value with a lower value than your min expected font size let say 8Undulate
setting only the width still leave the height at 0. @ReinierMelian if i have two labels and they are vertically aligned its not good that the empty label has some weird height. I would like that the label would have the same height as in case of having one line of textRosanarosane
Like @ReinierMelian said, set a small value for >= height constraint, like 8.Sarah
@KlemenZagar well i think you question have some contradictions, you need a min value or don't?Undulate
Hi, I need the min value. but to be setup based on the font size. If I set height with constant (for example 8) the height for a bigger font will be the same as for small font. I want that the height changes the same way as it changes when label is non-empty.Rosanarosane
@KlemenZagar, the answer is updated for your situation.Sarah
I think we dont understand each other. Here you wrote: And the label size will be (42,8) if you removed text. And here is the problem that empty label doesnt stay the same height as in case when it is filled the height.Rosanarosane
@KlemenZagar, how long height you want it to stay when the text is empty?Sarah
it should depend on the font size. I want it to behave the same as if the label is filled with text and you are changing the font size.Rosanarosane
U
6

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

Undulate answered 25/8, 2017 at 15:49 Comment(3)
I think it's pretty clear from the OP's question that they want the height of the label to be at a minimum its height when it's not empty. This is a non-trivial requirement and simply stating they need a >= constraint is an oversimplification of the problem.Bustup
@Bustup you are saying that this answer is wrong? and don't work? can you please try it before?Undulate
@ReinierMelian This doesn't completely solve the problem. Setting the constraint will fix the minimum height, but we don't know what the minimum height is because the line height will change if accessibility value is changed to large font in system settings.Lifer
B
2

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.

Bustup answered 15/8, 2019 at 8:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.