user198725878
user198725878

Reputation: 6386

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

Upvotes: 0

Views: 189

Answers (2)

Matthias Bauch
Matthias Bauch

Reputation: 90117

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;

Upvotes: 3

brain
brain

Reputation: 5546

You need to set the line break mode e.g.

myLabel.lineBreakMode = UILineBreakModeTailTruncation

Have a look @ http://developer.apple.com/library/ios/#documentation/uikit/reference/NSString_UIKit_Additions/Reference/Reference.html#//apple_ref/doc/c_ref/UILineBreakMode for the other ways to handle text that is too long.

Upvotes: 1

Related Questions