UILabel truncate text not working
Solution 1
simple, set the following properties:
label.adjustsFontSizeToFitWidth = NO;
label.lineBreakMode = NSLineBreakByTruncatingTail;
Solution 2
If you're using attributed string and having set paragraph style to the attributed string: make sure you're passing paragraphStyle.lineBreakMode = .byTruncatingTail
.
Solution 3
If using auto layout, in my case, there was a constraint missing. The UILabel grows its width if no constraint is set on its width/trailing. Once its width is limited, for instance to its superview, the truncation occurs.
Solution 4
You probably have a constraint on a label in that section that is making things go haywire.
Double check your constraints or remove them for that label or other controls in that section.
The storyboard option for a label: "Line Breaks:Truncate Tail" will to the work you are looking for.
Solution 5
If you set the label's autoshrink to "Fixed Font Size" in IB, you will always get a truncatation when the string width beyond the label width. I guess you happened to set that to "Minimum Font Scale" or "Minimum Font Font", which will lead a resizing when the string is too long.
(Xcode 4.5, other version of Xcode and IB may be different property name)
Eric Qian
Updated on June 15, 2022Comments
-
Eric Qian almost 2 years
I set the numberOfLines to 1 in the IB, but when I set the text to a long string, it doesn't truncate. If I set the numberOfLines to 2, the truncate works fine.What should I do to truncate a long string into a single line?