Getting UILabel to produce an ellipsis rather than

2019-01-11 09:40发布

问题:

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.

回答1:

Set the following properties:

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

You can also set these properties in interface builder.



回答2:

Swift solution:

label.lineBreakMode = .ByTruncatingTail

Swift 3:

label.lineBreakMode = .byTruncatingTail


回答3:

I have achieved by following steps:

1.Increase height of label as I did so that 2 lines fit.

2.select label, go to attributes inspector

3.select Line Breaks = Word Wrap

4.also increase lines, like 2,3

Worked for me