Klemen
Klemen

Reputation: 2180

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:

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.

Upvotes: 7

Views: 4226

Answers (3)

shoe
shoe

Reputation: 1080

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.

Upvotes: 2

Yun CHEN
Yun CHEN

Reputation: 6648

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

Upvotes: 6

Reinier Melian
Reinier Melian

Reputation: 20804

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

Upvotes: 6

Related Questions