Reputation: 5019
If you take a close look an an iMessage conversation cell, you’ll notice that the preview text is always two lines long. This can’t be a hard coded row height because the rows adjust to dynamic type. How can you always force a label to take up a certain number of lines even if there isn’t enough text to do so?
Upvotes: 3
Views: 1583
Reputation: 3850
One way I think this could be possible is having a UIView
as a parent of the UILabel
.
UIView
based on device size class. Let's say for example 50 points for Width = Compact and Height = Regular.UILabel
in UIView
UILabel
UILabel
leading , trailing ,top edge with the superview and leave the height as it is , also don't set the bottom constraints.UIView
and UILabel
together and select Equal Heights.Open the constraint window and change label height less than or equal to SuperView height.
Short Message
Long Message
Height Constraint of the UILabel with respect to SuperView.
Upvotes: 0
Reputation: 534893
Set the label's numberOfLines
to 2
and end the label's text
with a linefeed \n
. Set the wrapping to word wrap to prevent the ellipses from appearing.
This guarantees that the label text consists of at least 2 lines worth of material. Thus it can never be less than 2 lines, and since the maximum number of lines is 2, it can never be more than 2 lines. Thus it will always be (wait for it) 2 lines.
Upvotes: 4