Travis Griggs
Travis Griggs

Reputation: 22252

iOS UILabel zero height with empty String

I'm using constraint based layouts. I have some layouts that stack vertically like:

TOP LABEL

errorLabel

OTHER CONTROL

Using constraints, I set the vertical spacing between each to 10. The problem lies in when there is no error state. In that case, I set the UILabel.text = "". When I do that, it shifts the OTHER CONTROL upwards. I don't want that to happen. I want the errorLabel's height to be same as one line of text regardless of how many characters I put there. Is there an elegant way to do that?

Some workarounds I have considered include:

1) Set a filler label (e.g. XXX) and then use alpha/color to make the label dissapear. This makes me write more code than just setting the errorLabel to an empty string.

2) Constrain the height of the errorLabel. This creates an invariant between a constant height and whatever I set the font size to be (in other words, if I want to tune the font height, I have to remember to adjust the hardcoded height).

A VerticalStackView isn't an option because of some other constraints I have with the UI overall.

Upvotes: 0

Views: 1111

Answers (1)

agibson007
agibson007

Reputation: 4373

Add a string of “ “ to give the label an intrinsic height.

Upvotes: 2

Related Questions