UILabel in UITableViewCell takes up extra space when set to fixed number of lines

Published

I have a label inside a table view cell. I have set the label to a number of lines, 2. However, if the text is larger it takes up the space as if it had no line restriction and was displaying the full text. Even though it is displaying just two lines.

Constraints for that display label are below. All the other elements are top to bottom 5 points between from the top of the container down to the bottom. If the text is 2 lines or less, there is no extra space. Has anyone experienced this or found a fix for it?

enter image description here
enter image description here
enter image description here

Source: Swift

Published
Categorised as swift, uilabel, uitableview

Answers

If you do not want you label to grow higher than expected.

label.setContentCompressionResistancePriority(.required, for: .vertical)

If you are using nib/storyboard. simply change content hugging priority to 1000


Dillon Quitzon

Leave a Reply

Your email address will not be published. Required fields are marked *

Still Have Questions?


Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us
faq