Search code examples
iphoneiphone-sdk-3.0

How to set text with ending .... on UILabel


I am setting the UILabel text as below

myLabel.text = name;

What I would like to ask is if the text goes longer I want to show like below

stackoverflowuserhere.........

How can I done it...

Thanks for any help


Solution

  • If you want your text to truncate at 20 characters you have to do it manually.

    NSString *truncatedName = name;
    if ([truncatedName length] > 20)
        truncatedName = [NSString stringWithFormat:@"%@...", [truncatedName substringToIndex:20]];
    myLabel.text = truncatedName;