10

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.

Robin Daugherty
  • 7,115
  • 4
  • 45
  • 59
ppp
  • 713
  • 1
  • 8
  • 23

4 Answers4

15

It is fairly easy:

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

  2. Create an IBOutlet.

  3. Do:

    aLabel.adjustsFontSizeToFitWidth = false
    aLabel.lineBreakMode = .byTruncatingTail
    
Carmen
  • 6,177
  • 1
  • 35
  • 40
Coder1000
  • 4,071
  • 9
  • 35
  • 84
  • 3
    thanks for your reply. I tried it but no effect. Still chops the text without an eclipse at the end. Could it be that I added a width auto-layout constraint to the UILabel? – ppp May 08 '16 at 08:05
  • @Polis This is extremely weird. When I create a UILabel and give it constraints, an ellipsis automatically appears if the text is too long. And I did add a width constraint. Please try recreating your UILabel :) – Coder1000 May 08 '16 at 11:22
3

If you are adding UIlabel through a Storyboard, following steps can be useful:

  1. Select the label which you want to display '...' at the end.

  2. Go to attributes inspector.

  3. Select 'Line Breaks' and choose an option Truncate Tail

  4. Check it by giving a text of more content size than label size.

pkamb
  • 33,281
  • 23
  • 160
  • 191
1

Objective C:

yourUILabel.adjustsFontSizeToFitWidth = false;
yourUILabel.lineBreakMode = NSLineBreakByTruncatingTail;
0

If your label is using attributedText or parsing HTML to string label you should add self.messageContentLabel.lineBreakMode = .byTruncatingTail on the last code like this:

let convertedTextToAttributeText = NSMutableAttributedString(attributedString: customTextStyle.htmlToAttributedString)
self.messageContentLabel.attributedText = convertedTextToAttributeText
// Other line of code
self.messageContentLabel.lineBreakMode = .byTruncatingTail