Search code examples
iosxcodeswiftautolayoutuilabel

How to make UILabel text appear with ellipsis at the end when text is longer than label's width


My issue is with a UILabel.

The text it holds is much longer than the actual width of the UILabel. So I would like it to appear with "..." (an ellipsis) at the end to denote that there's more text.

I played around with horizontal content hugging priority (made it less than 251) but it doesn't seem to make a difference.

Right now it just chops the text when the width fills up.


Solution

  • It is fairly easy:

    1. Create a UILabel called "aLabel" for example.

    2. Create an IBOutlet.

    3. Do:

      aLabel.adjustsFontSizeToFitWidth = false
      aLabel.lineBreakMode = .byTruncatingTail