Reputation: 33
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).
Upvotes: 1
Views: 974
Reputation: 24195
You can get labels frame in view did appear and replace them on the screen again. I tried that before and it worked. may you also need to call setNeedsDisplay.
- (void)viewDidAppear:(BOOL)animated {
float heights = lbl1.frame.size.height + lbl2.frame.size.height;
lbl1.frame = CGRectMake( lbl1.frame.origin.x, screenheight/2 - heights/2, lbl1.frame.size.width, lbl1.frame.size.height );
lbl2.frame = CGRectMake( lbl2.frame.origin.x, screenheight/2 - heights/2 + lbl1.frame.size.height, lbl2.frame.size.width, lbl2.frame.size.height );
[lbl1 setNeedsDisplay];
[lbl2 setNeedsDisplay];
}
Upvotes: 0
Reputation: 33650
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.
Upvotes: 1