UILabel truncate text not working
Asked Answered
L

9

9

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?

Lapland answered 10/12, 2012 at 4:3 Comment(3)
@borrrden, in my case it just clips the text at the border of the label container, without showing the trailing dotsIdeality
finally 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://mcmap.net/q/433750/-nsattributedstring-tail-truncation-in-uilabelIdeality
Hi @igrek, your comment helped! Thanks. :)Erik
E
11

If you're using attributed string and having set paragraph style to the attributed string: make sure you're passing paragraphStyle.lineBreakMode = .byTruncatingTail.

Erik answered 26/6, 2019 at 9:13 Comment(0)
M
9

simple, set the following properties:

label.adjustsFontSizeToFitWidth = NO;
label.lineBreakMode = NSLineBreakByTruncatingTail;
Mochun answered 10/12, 2012 at 4:13 Comment(0)
C
5

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.

Chios answered 7/3, 2016 at 12:58 Comment(1)
Thanks, Shaked. The solution worked for me. I set label.trailing equal to superview.trailing and NumberOFLines as 1. The label got truncated!Arnulfoarny
O
3

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.

Oesophagus answered 14/10, 2014 at 13:18 Comment(1)
This was definitely my problem. In my case, I just removed all constraints and re-added just the ones I wanted.Provolone
A
1

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.

enter image description here

(Xcode 4.5, other version of Xcode and IB may be different property name)

Arjuna answered 10/12, 2012 at 4:19 Comment(1)
It 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.Trotman
G
0

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;
}
Gypsophila answered 5/7, 2013 at 14:0 Comment(0)
K
0

Ensure you are not calling sizeToFit() on your label. It will override the label and constraint settings.

Kneecap answered 1/8, 2019 at 16:36 Comment(0)
U
0

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

Ulland answered 6/8, 2019 at 10:31 Comment(0)
C
-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.

Cottle answered 10/12, 2012 at 4:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.