Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get truncated text from UILabel

Is there anyway I can get the truncated version of the text for a UILabel?

In short, I have a paragraph of text, and two UILabels - label A, which is 2 lines long, and label B, which is a variable height. Label A is above label B. The idea is that label A shows the first two lines of the paragraph of text, and upon a certain user action, label B because visible and displays the rest of the text.

I'm having trouble determining what should go in label B, as I don't know what's being shown in label A. I'd need to also remove the "..." from label A.

Note: I realize this is a bit convoluted but there are some good reasons for it, which I won't clutter up the question with.

like image 227
Ben Williams Avatar asked Jul 18 '11 02:07

Ben Williams


People also ask

How do you check if a label is truncated?

You are dividing width by height and if it is bigger than number of line (which might very well be 0) you are saying label is truncated.

How do I know if my text is truncated?

We can check if a piece of text is truncated with the CSS text-overflow property by checking whether the offsetWidth of the element is less than its scrollWidth .

How do I truncate a string in Swift?

string-truncate.swift Truncates the string to the specified length number of characters and appends an optional trailing string if longer. - Parameter trailing: A 'String' that will be appended after the truncation. - Returns: 'String' object. let str = "I might be just a little bit too long".


1 Answers

I wonder if you could use the methods in the NSString UIKit Additions to figure out how much fits into label A.

A crude way might be to start with the first character of your text and test for the size it would take up (-sizeWithFont:forWidth:lineBreakMode: maybe?) and then keep adding characters one at a time until it doesn't fit into your label A any more.

I hope somebody else can come up with a better way to do this, but the above should work.

Update

Last night I looked a bit into Core Text for my own app and came across CTFramesetterSuggestFrameSizeWithConstraints. You could maybe use this to figure out how much of your string fits into the label, by looking at the fitRange in that function.

Update 2:

I think this should work, but I have just typed this in here, so it may not even compile:

UIFont *uiFont = [UIFont systemFontOfZise:13.0f]; // whichever font you're using
CTFontRef ctFont = CTFontCreateWithName((CFStringRef)uiFont.fontName, uiFont.pointSize, NULL);
NSDictionary *attr = [NSDictionary dictionaryWithObject:(id)ctFont forKey:(id)kCTFontAttributeName];
CFRelease(ctfont);
NSAttributedString *attrString  = [[NSAttributedString alloc] initWithString:yourLabelText attributes:attr];
CTFrameSetterRef frameSetter = CTFrameSetterCreateWithAttributedString((CFAttributedStringRef)attrString);
[attrString release];
CFRange fitRange;
CTFrameSetterSuggestFrameSizeWithConstrains(
    frameSetter,
    CFRangeMake(0, 0),
    NULL,
    CGSizeMake(labelWidth, labelHeight),
    &fitRange);
CFRelease(frameSetter);
CFIndex numberOfCharactersThatFit = fitRange.length;
like image 52
Thomas Müller Avatar answered Sep 29 '22 03:09

Thomas Müller