MarksCode MarksCode - 27 days ago 12
iOS Question

Vertically center text in UILabel

I have a 3 line label inside a collection view cell. I want the text to always be vertically centered, so if there is only 1 line of text it will look like it's on the second line of the label. When there's 2 lines of text, the first line should be in between where the first and second lines of text are, and the second line of text should be in between where the second and third lines are.

Here's a picture of what I have and what I'm trying to achieve:
first
second

Is there any way to achieve such a thing? So the second picture's label will actually be appear where the middle line of the first image's label is?

Answer

You should set a fixed height constraint for your label and the text will automatically be centered vertically just as you described. The constant value should be picked to fit 3 lines as on your first screenshot.

enter image description here

Most probably now you have <= instead of =