Search code examples
iosstoryboarduilabel

Prevent a UILabel from truncating tail


I have two UILabels: name and description. The constraints are setup as the following:

  • name has left and top constraint
  • description has a right and top constraint
  • A minimum 10 pixels spacing constraint between them so that they don't overlap

enter image description here

When I run the program, not consistently, sometimes name is truncated and sometimes description is truncated.

Question is: Is there a way to guarantee that when running out of space alway truncate the right label?


Solution

  • The proper way to address this is to change the Content Hugging Priority for one of the labels. By default, this is set to 251. Just change one of the label's Content Hugging Priority to 250 or 252 depending on how you want it to be "hugged".

    In code:

    func setContentHuggingPriority(_ priority: UILayoutPriority, 
                                  for axis: NSLayoutConstraint.Axis)
    

    Or interface builder:

    And check out this post if you need more info about this topic: https://medium.com/@abhimuralidharan/ios-content-hugging-and-content-compression-resistance-priorities-476fb5828ef