Truncate part of text in UILabel

user2990765 picture user2990765 · Dec 6, 2013 · Viewed 32.1k times · Source

My requirement is that I need to display text in label in such a way that if the length of text is too big to accommodate in one line, i need to truncate it at the end in such a way that only the last few characters(usually a number b/w 1-1000 so text length may vary.) are visible and the text before it is truncated with "...".

So the text will look something like "abcdefgijk...10"

Is there any way I can achieve this?

Answer

Srinivasan N picture Srinivasan N · Apr 25, 2014
UILabel *contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(50,100, 150, 30)];
contentLabel.text = @"abcdefghijklmnopqrstuvwxyz10";
contentLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;

Add this label to your display. You should get a output something like this

abcdefghijklmnopq...10