N
N
Nikita2019-07-16 23:52:48
iOS
Nikita, 2019-07-16 23:52:48

How to center text vertically in UILabel?

I have a UILabel whose text is automatically sized to fit the available width:

label.adjustsFontForContentSizeCategory = true
label.adjustsFontSizeToFitWidth = true

On the Layout, this UILabel has 4 constraints:
Center on X and Y; indents left and right.
If you enter a short text, then the text in the UILabel is in the middle (x = y):
5d2e3847999b1176675895.png
If you enter a long text, then the text "goes down" (the font size automatically decreases, the distance from the top border of the UILabel to the text increases). At the same time, the height of the UILabel itself does not change.
5d2e38d2c4c46237501380.png
How do you center text vertically?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
briahas, 2019-07-23
@snitron

As I see it, there are no clear rules "what to do in what situation"
For example - why should the text be transferred to the second line, and not reduce the font so that all the text fits?
If you have a clearly defined logic in your head of how the text should look in different cases, then it is better to describe it in logic and turn off autofit. Because you don't know what rules she uses.

A
Aleksandr Govorukhin, 2019-08-05
@SnapSh0t

I didn’t understand the question, but with given constraints, your text will always be centered, regardless of its content
5d47a747b231c032785201.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question