Getting UILabel to produce an ellipsis rather than shrinking the font
Asked Answered
S

4

39

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.

Shriner answered 11/10, 2011 at 12:7 Comment(0)
F
101

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.

Forswear answered 11/10, 2011 at 12:11 Comment(3)
UILineBreakModeTailTruncation is marked as deprecated, docs recommend using NSLineBreakByTruncatingTail. Which does the same thing.Forswear
I also believe that the default for UILabels is to have adjustsFontSizeToFitWidth set to NOArsphenamine
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?Gethsemane
B
20

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
Bronchial answered 1/4, 2019 at 4:4 Comment(3)
What is the textString your referencing? Is that supposed to be the "example long strong" ?Gangrene
Sorry I can see how that was confusing, just fixed up the example and confirmed it works in a playground.Bronchial
Great tip, yeah you need to explicitly set it when using attributed text.Icicle
O
15

Swift solution:

label.lineBreakMode = .ByTruncatingTail

Swift 3:

label.lineBreakMode = .byTruncatingTail
Omentum answered 1/6, 2015 at 10:37 Comment(2)
can't you just do label.lineBreakMode = .ByTruncatingTail?Selsyn
Looks like it - yes :)Omentum
S
2

In the interface builder this is the way to go:

Line Break setting in Interface Builder

Susquehanna answered 29/4, 2021 at 23:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.