7

Im working on an ios app using objective c and i have an issue with uilabel that i could use some help with. Basically i have a label that can change size to fit the text that it will display but it has a max height that it can possible be. the label itself has a fixed width at all times. i have turned on UILineBreakModeWordWrap and UILineBreakModeTailTruncation to make the text fit and truncate but this causes the text to truncate the tail too early when it has only 1 word left to place. rather then moving it onto the next line when there is still room it just truncates it.

self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, fixedWidth, 0);
self.lineBreakMode = UILineBreakModeWordWrap | UILineBreakModeTailTruncation;
self.numberOfLines = 0;
[self sizeToFit];

is there anyway of finding when the uilabel is actually truncating the text so i can then check the label height and add to it if there is still room ? I tried always adding an extra line to the height when there is room and this avoids the early truncation but then im left with inconsistent sizing of the over all label. any ideas on this would be great thanks

glogic
  • 4,042
  • 3
  • 28
  • 44

4 Answers4

13

lineBreakMode is a switch. It can be either (for iOS6+) NSLineBreakByWordWrapping or NSLineBreakByTruncatingTail but not both.

But, to answer your question, you can find the size of some text using the class extensions in NSString+UIKit. Having found the size you could update the frame of the UILabel appropriately.

marsei
  • 7,691
  • 3
  • 32
  • 41
Stephen Darlington
  • 51,577
  • 12
  • 107
  • 152
  • ended up getting the size that it would be from the link you provided (sizeWithFont:) and then set the linebreakmode to wordwrap or tail trunc depending if it was at max or not and then used sizetofit and worked great thanks – glogic Jan 24 '12 at 14:10
  • Nice tip on that category. Didn't knew it existed before. – chakrit May 08 '12 at 12:14
1

I just came to a similar problem, and solved it with a very simple solution (tested on ios 8.4, xcode 7).

In IB (I use autolayout with some constraints):

set UIlabel numberOfLine = 1;
LineBrakeMode = TruncateTail

In Code:

label.numberOfLines = 2; // I can only display 2 row. 
// I supposed you should know how many rows you can displayed.

label.preferredMaxLayoutWidth = label.frame.size.width;
[label sizeToFit];

Tadaa. That's it. Note that this only work with UILabel. With UIButton, it may not work (Haven't tested).

Hlung
  • 13,850
  • 6
  • 71
  • 90
Eddie
  • 1,903
  • 2
  • 21
  • 46
0

I have written a category for working with UILabel's truncation. Works on iOS 7 and later. Hope it helps !

@implementation UILabel (Truncation)

- (NSRange)truncatedRange
{
    NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:[self attributedText]];

    NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
    [textStorage addLayoutManager:layoutManager];

    NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:[self bounds].size];
    textContainer.lineFragmentPadding = 0;
    [layoutManager addTextContainer:textContainer];

    NSRange truncatedrange = [layoutManager truncatedGlyphRangeInLineFragmentForGlyphAtIndex:0];
    return truncatedrange;
}

- (BOOL)isTruncated
{
    return [self truncatedRange].location != NSNotFound;
}

- (NSString *)truncatedText
{
    NSRange truncatedrange = [self truncatedRange];
    if (truncatedrange.location != NSNotFound)
    {
        return [self.text substringWithRange:truncatedrange];
    }

    return nil;
}

@end
Alejandro Cotilla
  • 2,501
  • 1
  • 20
  • 35
0

Using this method:

How to find UILabel's number of Lines

You could set the label to the max height, find out how tall the text is in that label and shrink it as necessary.

Community
  • 1
  • 1
jackslash
  • 8,550
  • 45
  • 56