Reputation: 273
I have 2 labels set up like this:
Orange Containing View:
Left Label:
Right Label:
When the label texts become longer, BOTH labels' fonts shrink together to a minimum font scale (or size) before truncating.
Only 1 label's font auto-shrinks and then truncates, and I get a constraint complaint about needing to make 1 of the label's Compression Resistance higher than the other. Even when satisfying that constraint, both labels' fonts won't auto-shrink.
Is there any way, through autolayout or code, to get both of the labels' fonts to first shrink, and then both truncate if needed? Thanks.
Upvotes: 3
Views: 1426
Reputation: 342
Using Xcode 9.2 autolayout, this works automatically for me.
I have the two labels in a view. This view is constrained to a UITableViewCell.
Left Label:
Right Label:
When the text strings for labelLeft and labelRight are short, the space between them is large. When those strings are long, the space between them is small (nearly exactly 8) and both fonts have shrunk, equally.
Upvotes: 2
Reputation: 1181
It isn't possible by just using autolayout constraints. One thing you can do is as follows:
Set the Autoshrink to Fixed Font Size for both the labels.
Have two font sizes (minFontSize and maxFontSize) between which it is OK for the texts to be rendered with.
Concatenate both the texts and find the font size (calculatedFontSize) that is needed for the concatenated text to fit the available width. You can refer it here.
To calculate the available width, use the following:
availableWidth = width(orangeView) - leftSpace - rightSpace - betweenSpace
For your instance: leftSpace is 8, rightSpace is 8, betweenSpace is 8.
Your target fontSize, targetFontSize = min(max(calculatedFontSize, minFontSize), maxFontSize)
Now, apply the target font to the two labels.
Upvotes: -1