Search code examples
iphoneiosobjective-cios7core-text

How to vertically center two UILabel with dynamic heights


How to center vertically two UILabel (dynamic heights) inside a view, like this with Label1 (2 lines, truncated) and Label2 (1 line, truncated):

+------------------------------------------+
|                                          |
|                                          |
|Label1 Label1 Label1 Label1 Label1 Label1 |
|Label1 Label1 Label1 Label1 Label1 ...    |
|Label2 Label2 Label2 Label2 Label2 ...    |
|                                          |
|                                          |
+------------------------------------------|

I guess this is not possible with a single UILabel and a NSAttributedString (to truncate one part on 2 lines, and another part on 1 line).


Solution

  • If you are using auto layout, you can embed the two labels in a parent UIView, and then use a constraint to vertically center the parent UIView in its superview.