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?
-
@borrrden, in my case it just clips the text at the border of the label container, without showing the trailing dots – igrek Aug 22 '17 at 13:05
-
8finally solved the problem.. the problem was an attributed string... I had my text specified with it, and it used custom pagargaph style. When i specified lineBreakMode of a paragraph the problem was fixed. credits go to https://stackoverflow.com/a/41945661/907692 – igrek Dec 21 '17 at 16:33
-
Hi @igrek, your comment helped! Thanks. :) – Hemang Jun 26 '19 at 08:30
9 Answers
simple, set the following properties:
label.adjustsFontSizeToFitWidth = NO;
label.lineBreakMode = NSLineBreakByTruncatingTail;

- 257
- 5
- 13

- 4,596
- 2
- 22
- 38
If you're using attributed string and having set paragraph style to the attributed string: make sure you're passing paragraphStyle.lineBreakMode = .byTruncatingTail
.

- 26,840
- 19
- 119
- 186
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.

- 5,712
- 2
- 31
- 38
-
Thanks, Shaked. The solution worked for me. I set label.trailing equal to superview.trailing and NumberOFLines as 1. The label got truncated! – A_G Dec 16 '16 at 02:41
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.

- 1,198
- 1
- 9
- 8
-
This was definitely my problem. In my case, I just removed all constraints and re-added just the ones I wanted. – livingtech Jun 11 '15 at 15:45
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)

- 4,591
- 1
- 25
- 31
-
1It still doesn't truncate the text correctly (using character wrap/word wrap). It just cuts off the graphic of the text. Unless you use multiple lines, in which case it changes the position of the first line to where you can't even see it. – CommaToast Oct 15 '13 at 06:02
I make two functions, that will help you to do your work.
Basic:
This solution I made for task:
"minimize font to my min size of font and then put as much info, as possible, but not bigger then maximum width"
takeFineFont... function parameters:
(UIFont*)font
- font of the your label (titleLabel.font
)
(NSString*)string
- text in your label (titleLabel.text
)
(CGSize)limitStringSize
- limit size.
limitStringSize.width
- width limit of your label (Upper limit)
limitStringSize.height
- height limit of your label (Lower limit)(actually, size of font)
-(UIFont*)takeFineFontSize:(UIFont*)font
forText:(NSString*)string
andLimit:(CGSize)limitStringSize{
UIFont* resultFont = [UIFont fontWithName:[font fontName] size:[font pointSize]];
if(limitStringSize.width != 0 && limitStringSize.height != 0){
CGSize currentSize = [string sizeWithFont:resultFont];
while(/* change font width with upper bound */
currentSize.width > limitStringSize.width
&&
/* change font height with lower bound */
currentSize.height > limitStringSize.height){
/*change height and take new width*/
currentSize.height -= 1;
currentSize.width = [string sizeWithFont:[resultFont fontWithSize:currentSize.height]].width;
}
resultFont = [resultFont fontWithSize:currentSize.height];
}
return resultFont;
}
-(double)takeFineWidthForFont:(UIFont*)font
forString:(NSString*)string
andLimit:(double)widthLimit{
return MIN([string sizeWithFont:font].width, widthLimit);
}
Suppose, that you have big string in UILabel* titleLabel
And you define somewhere:
#define maximumLengthOfYourLabel 300
#define minimumSizeOfFont 14
what you will do now? just do this peace of code:
-(void)updateTitleLabelWithBigText:(NSString*)string{
/*change text*/
self.titleLabel.text = string;
/*take pretty small font*/
self.titleLabel.font = [self takeFineFontSize:self.titleLabel.font
forText:self.titleLabel.text
andLimit:CGSizeMake(maximumLengthOfYourLabel,minimumSizeOfFont)
];
/*if your text still big, take minimal width and trunctate it*/
self.titleLabel.width = [self takeFineWidthForFont:self.titleLabel.font
forString:self.titleLabel.text
andLimit:maximumLengthOfYourLabel];
self.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
}

- 3,642
- 9
- 35
- 64
Ensure you are not calling sizeToFit()
on your label. It will override the label and constraint settings.

- 311
- 2
- 21
set numberOfLines to 0, this will let you expand uilabel content vertially without truncating its width.

- 97
- 6
Perhaps this method can help you:
[myLabel sizeToFit];
The label won't be truncated but it will adjust the label size to fit in one line.

- 148
- 1
- 7