Getting UILabel to produce an ellipsis rather than shrinking the font

37,117

Solution 1

Set the following properties:

Objective C

label.adjustsFontSizeToFitWidth = NO;
label.lineBreakMode = NSLineBreakByTruncatingTail;

Swift

label.adjustsFontSizeToFitWidth = false
label.lineBreakMode = .byTruncatingTail

You can also set these properties in interface builder.

Solution 2

I had an issue producing ellipsis after I styled a UILabel and needed to use UILabel.attributedText instead of UILabel.text. There is a line break mode on the paragraph style that will overwrite the UILabel.lineBreakMode when using attributed text. You'll need to set the lineBreakMode to .byTruncatingTail on the attributed string's paragraph style if you want to achieve ellipsis.

e.g.

    let text = "example long string that should be truncated"
    let attributedText = NSMutableAttributedString(
        string: text, 
        attributes: [.backgroundColor : UIColor.blue.cgColor]
    )
    let range = NSRange(location: 0, length: attributedText.length)
    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.lineBreakMode = .byTruncatingTail
    attributedText.addAttribute(.paragraphStyle, value: paragraphStyle, range: range)
    uiLabel.attributedText = attributedText

Solution 3

Swift solution:

label.lineBreakMode = .ByTruncatingTail

Swift 3:

label.lineBreakMode = .byTruncatingTail
Share:
37,117
Eric Brotto
Author by

Eric Brotto

Updated on May 06, 2021

Comments

  • Eric Brotto
    Eric Brotto about 3 years

    When I dynamically change the text of a UILabel I would prefer to get an ellipsis (dot, dot, dot) rather then have the text be automatically resized. How does one do this?

    In other words, if I have UILabel with the word Cat with size font 14 and then I change the word to Hippopotamus the font shrinks to fit all the word. I would rather the word be automatically truncated followed by an ellipsis.

    I assume there is a parameter that can be changed within my UILabel object. I'd rather not do this programmatically.

  • jbat100
    jbat100 almost 11 years
    UILineBreakModeTailTruncation is marked as deprecated, docs recommend using NSLineBreakByTruncatingTail. Which does the same thing.
  • Jeff Grimes
    Jeff Grimes almost 11 years
    I also believe that the default for UILabels is to have adjustsFontSizeToFitWidth set to NO
  • Victor Engel
    Victor Engel over 9 years
    What if you have two labels side by side and want one to be able to truncate with an ellipsis and the other one to neither adjust font size nor truncate (assume there is enough space for the text). Their frames adjust using constraints. Is there a constraint that will force a size to be at least as big as the content size needs to be to prevent an ellipsis or truncation?
  • Ky -
    Ky - over 8 years
    can't you just do label.lineBreakMode = .ByTruncatingTail?
  • dijipiji
    dijipiji over 8 years
    Looks like it - yes :)
  • Koen
    Koen about 3 years
    What is the textString your referencing? Is that supposed to be the "example long strong" ?
  • Jacob Lange
    Jacob Lange about 3 years
    Sorry I can see how that was confusing, just fixed up the example and confirmed it works in a playground.