Christopher Masser
Christopher Masser

Reputation: 829

How to place two UILabels in a row so that they center vertically based on the size of the text in iOS?

I have a table row with a label and a button that should both be centered vertically based on the text displayed. That means that the height of the row should be based on which text is the largest. See the image below:

enter image description here

I have tried doing this by setting the line break to WordWrap and vertically centering both, however the UIButton on the right does not scale accordingly.

Constraints:

*label* top 5 to contentview
*label* bottom 5 to contentview
*label* leading 5 to contentview
*label* width 0.75 of contentview

*button* leading 5 to label
*button* trailing 5 to contentview
*button* bottom 5 to contentview
*button* top 5 to contentview                                  

Upvotes: 0

Views: 196

Answers (3)

rv7284
rv7284

Reputation: 1102

try this setup

Right label Right Label

Left Label

Left Label

Now Put Button over right label like thisenter image description here

Upvotes: 1

Paras Gorasiya
Paras Gorasiya

Reputation: 1351

After setting text to both the labels you can get endY of both labels and return the appropriate one's height+padding inside tableView delegate.

Upvotes: 0

TawaNicolas
TawaNicolas

Reputation: 646

Don't set a height constraint on any of the views. Just set top and bottom constraints for both the label and the button, and the leading and trailing obviously.

You don't even need to make them centered vertically, the UITableViewCell will wrap them perfectly for you if they have the same top and bottom margins.

Upvotes: 0

Related Questions