Hide a UILabel when there is not enough space to display the full text with autolayout

Viewed 1612

I have a horizontal stack view with three controls from left to right: a custom UIView subclass (which has an intrinsic content size), and two UILabels. Both labels are configured like this:

label.numberOfLines = 1
label.adjustsFontSizeToFitWidth = false
label.adjustsFontForContentSizeCategory = true

I set the stack view distribution to .fill and the content hugging priority and content compression resistance priority of the three controls such that the custom view and first label's frames hug the content and the second label stretches to fill the remaining space. It looks like this:

|[custom view][label 1][label 2     ]|

My issue comes when I adjust the system text size to be larger such that there is not enough horizontal space to display the full text of both labels. Instead of truncating the text of the first label with "...", I would like for it to be hidden entirely when there is not enough space to display the full text. Is there any way to do this with autolayout?

It feels as though there should be a way to do this with a width = 0 constraint with a priority carefully calibrated around the compression resistance priority, but I can't quite wrap my brain around how to do it.

2 Answers
Related