Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
CSS
I want to show my long text in a UILabel. But, My design having small size of frame for that UILabel. So, i want to truncate my long text like this[see below]:

Ex:

Given Text: "I want to show my long text in a UILabel"

Recent Result: [Using lineBreakMode:]
1. I want to s........a UILabel
2. I want to s.....
3. I want to s
 
Excepting Result: "I want to...."

[Note:  I want truncation after the word which can fit within their label frame.]

I hope that you can sense about my excepting result. Am weak in English!.
Posted
Updated 4-Jun-14 21:08pm
v3

The UILabel has a property lineBreakMode that is the way to go.

tip: the Apple documentation is very good, so read it carefully to understand all features.
 
Share this answer
 
Comments
Md Sadham 5-Jun-14 3:04am    
Already i know about that property. Even i used all mode of this property, but i got unexpected result. I want truncation after the word which can fit within thier label frame.

Mode given result:
1. I want to s........a UILabel
2. I want to s.....
3. I want to s

But i want, "I want to...."
After 1 week, i found solution myself. Please check the efficient of the code and comment it.

My solution handles multiple number of lines of text you given for your control title of text.

Taste this type of truncation!!!

Solution:
C#
-(NSString *)methodForDisplayTextWithTruncate:(UILabel *)lblFinalResult{

    NSLog(@"Old Frame: %@", NSStringFromCGRect(lblFinalResult.frame));
    NSLog(@"Event Length: %i", [lblFinalResult.text length]);

    NSString *strGivenText, *strFuncResult, *stringThatFits, *backupStr;
    int i,checkpoint=lblFinalResult.numberOfLines,bound=0;
    NSArray *arrForGivenText_Words;
    NSMutableArray *mutArrForWords;

    stringThatFits=@"";
    strFuncResult=@"";
    backupStr=@"";
    mutArrForWords=[[NSMutableArray alloc] initWithObjects:nil];

    //Custom Truncate Function
    strGivenText=lblFinalResult.text;
    arrForGivenText_Words = [strGivenText componentsSeparatedByString:@" "];

    for(int y=0; y<[arrForGivenText_Words count];y++){
        if(![[arrForGivenText_Words objectAtIndex:y] isEqualToString:@""]){
            [mutArrForWords addObject:[arrForGivenText_Words objectAtIndex:y]];
        }
    }
//    NSLog(@"Corrected:%@",mutArrForWords);

    for (i=0; i < [mutArrForWords count]; i++)
    {
        /* must follow @" %@" - a space before %@ */
        NSString *tempString = [stringThatFits stringByAppendingFormat:@"%@ ", mutArrForWords[i]];
        NSLog(@"TempString:%@",tempString);

        CGRect boundingRect = [tempString boundingRectWithSize:CGSizeMake(999, 999)                                                       options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:lblFinalResult.font} context:nil];

        if (boundingRect.size.width > lblFinalResult.frame.size.width) {
            NSLog(@"Bound(%i)", bound++);

            if(i==0){
                [lblFinalResult setText:@"..."];
                NSLog(@"New Frame: %@", NSStringFromCGRect(lblFinalResult.frame));
                return lblFinalResult.text;
            }
            if(checkpoint>1){
                backupStr=@"";
                for (int j = 0 ; j < i; j++){
                    backupStr=[backupStr stringByAppendingFormat:@"%@ ", mutArrForWords[j]];
                }
                NSLog(@"Backup Bound:%@",backupStr);
                stringThatFits=mutArrForWords[i];
                stringThatFits=[stringThatFits stringByAppendingString:@" "];
                checkpoint--;
            }
            else{
                strFuncResult=@"";
                for (int z = 0 ; z < i; z++)
                {
                    strFuncResult = [strFuncResult stringByAppendingFormat:@"%@ ", mutArrForWords[z]];
                }
                strFuncResult = [strFuncResult substringToIndex:strFuncResult.length-(strFuncResult.length>0)];

                lblFinalResult.frame= CGRectMake(lblFinalResult.frame.origin.x, lblFinalResult.frame.origin.y, lblFinalResult.frame.size.width+10, lblFinalResult.frame.size.height);
                strFuncResult=[strFuncResult stringByAppendingString:@"..."];
                [lblFinalResult setText:strFuncResult];
                NSLog(@"Final_1:%@", lblFinalResult.text);
                NSLog(@"Final Length: %i", [lblFinalResult.text length]);
                NSLog(@"New Frame: %@", NSStringFromCGRect(lblFinalResult.frame));
                return lblFinalResult.text;
            }
        }
        else{
            stringThatFits = tempString;
        }
    }
    stringThatFits=@"";
    for (int z = 0 ; z < i; z++)
    {
        stringThatFits = [stringThatFits stringByAppendingFormat:@"%@ ", mutArrForWords[z]];
    }
    stringThatFits = [stringThatFits substringToIndex:stringThatFits.length-(stringThatFits.length>0)];

    [lblFinalResult setText:stringThatFits];
    NSLog(@"Final_2:%@", lblFinalResult.text);
    NSLog(@"Final Length: %i", [lblFinalResult.text length]);
    NSLog(@"New Frame: %@", NSStringFromCGRect(lblFinalResult.frame));
    return lblFinalResult.text;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900