Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UILabel adds unnecessary top and bottom padding when growing vertically and breaking lines

Let's say we have a UILabel, that is pinned from all sides to its superview. Number of Lines is set to 0 (so we can display multiline strings) and we use Truncate Tails strategy for line breaks.

If we have a single, short line of text, which does not break lines, the label's inside padding/inset looks fine:

Single line of text

When using multiline text, that will force the label to start breaking lines, ellipsis appear at the end of the label, but artificial padding/inset is added to top/bottom of the label as well. The label is inflated vertically, even though it is still displaying only 1 line of text:

Multiline text

Constraints:

Constraints

Line breaks:

Line breaks

What I tried already

Changed Content Hugging Priority to 1000 for both Horizontal and Vertical axis, so the UILabel doesn't grow more than required, but that does not solve the issue.

Switching between different Line Breaks strategies (Clip, Character Wrap, Truncate, ...) does not solve the issue either.

I am able to reproduce this behavior in both Xcode 6 and Xcode 7 beta, Interface Builder and while running the app.

like image 675
Juraj Blahunka Avatar asked Aug 07 '15 12:08

Juraj Blahunka


1 Answers

Looks like the only way to fix the unintentional padding is to set Lines to a constant (eg. 3). The UILabel will still grow dynamically and will refrain from adding any padding.

like image 116
Juraj Blahunka Avatar answered Nov 06 '22 21:11

Juraj Blahunka