Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I arrange two UILabels so that one autosizes and the other takes up the remaining space?

I have two labels arranged horizontally in my TableViewCell. The right should use as much space as it needs, while the one on the should left use up the remaining space. In the case where the length of the two labels is greater than the width of the cell, the left label should be trimmed so that the right has enough space to fully display. The labels are aligned to their respective sides, if that makes a difference.

The "Arrange > Fill Container Vertically" option under the Size Inspector is similar to what I need for the left label, but I want it to only occupy space not needed by the right label.

Example

If I add a constraint to the trailing space on the left label (see the third constraint below), the result is almost what I need, but instead of the right label being given the needed space, the left label takes the needed space (see final image).

Constraints

Example 2

like image 819
Nathan Van Dyken Avatar asked Jan 22 '26 04:01

Nathan Van Dyken


1 Answers

I think this is easier than you might expect...

Constrain "LeftLabel" leading, constrain "RightLabel" trailing, constrain Horizontal Spacing between the two to >= 8, and then give "LeftLabel" a Content Compression Resistance Priority of Low (250).

That tells auto-layout to expand each label based on its text content, but if the distance between them ends up being less-than 8, let "RightLabel" expand to its text, but expand "LeftLabel" only to the point that it is 8-pts from "RightLabel".

enter image description here

Left Label selected:

enter image description here

Results:

enter image description here

like image 57
DonMag Avatar answered Jan 23 '26 16:01

DonMag