Tail truncation

I am working on an ios application using object c, and I have a problem with uilabel with which I could use some help. Basically, I have a shortcut that can resize to fit the text it will display, but it has the maximum height that it can be. the label itself has a fixed width at all times. I included UILineBreakModeWordWrap and UILineBreakModeTailTruncation to make the text fit and truncated, but this causes the text to trim the tail too soon when it has only one word left. instead of moving it to the next line when there is still space, 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 a search where uilabel actually truncates the text, so I can check the height of the label and add to it if there is still room? I always tried to add an extra line to the height when there is room, and this avoids early truncation, but then I left with an inconsistent size over the whole label. Any ideas on this subject would be greatly appreciated.

+5
ios objective-c uilabel
Jan 24 '12 at 10:50
source share
4 answers

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

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

+11
Jan 24 '12 at 11:00
source share

I wrote a category for working with UILabel truncation. Works on iOS 7 and later. Hope this 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 
+1
Jun 12 '15 at 23:42 on
source share

Using this method:

How to find the number of rows UILabel

You can set the label to maximum height, find out how tall the text is on that label, and compress it if necessary.

0
Jan 24 2018-12-12T00:
source share

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

In IB (I use autorun with some limitations):

 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. It. Please note that this only works with UILabel. It may not work with UIButton (not tested).

0
Sep 28 '15 at 9:57
source share



All Articles