Truncate part of text in UILabel

41.4k views Asked by At

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?

10

There are 10 answers

0
Waseem05 On

there are many methods in NSString class use -length and then use any of these

– substringFromIndex:
– substringWithRange:
– substringToIndex:

create a temporary string using NSString stringwithFormat, put your desired charecters you get from substringTo index and "....." then your numbers from string by substringFromIndex.

hope this helps

1
Krishna Kumar On

Try this:

 label.lineBreakMode = NSLineBreakByTruncatingMiddle;

UILineBreakModeMiddleTruncation is deprecated from iOS 6.0.

2
mak On

You can start with finding the length of characters that can be placed in a line, say 'n' characters. You can take help of this link to determine 'n' How to know if NSString fits in UILabel or not and index of the last string which fits?. Next, find the length of the string. If it exceeds n, then extract the last two characters. Ex

NSString * fooString = @"a very long string";
NSString * s2 = [fooString substringWithRange:NSMakeRange([fooString length]-3, 2)];
NSString * s1 = [fooString substringWithRange:NSMakeRange(0 , n-5)];
NSString * newString = [NSString stringWithFormat:@"%@...%@",s1,s2];
1
Jyothi On

We have different Line Break modes for UILabel like

Truncate Head, Truncate Middle, Truncate Tail

In Xib you can set the Line Break mode what ever you want

0
jeyachandran On

If you using XIB file..

select --> UILable and select --> Attribute inspector tag and change into Line Breaks-->Truncate tail

simply way to truncate characters...

0
Balram Tiwari On

Here is how to use it, NSLineBreakByTruncatingMiddle

UILabel *temp = [[UILabel alloc]initWithFrame:CGRectMake(5,75, 100, 50)];
[temp setBackgroundColor:[UIColor lightGrayColor]];
temp.lineBreakMode = NSLineBreakByTruncatingMiddle;
temp.text = @"HelloBoss997";

Output : Hello...s997

0
iSrinivasan27 On

In Storyboard

  1. Select the Label which you want to truncate the characters.

  2. Choose Attributes Inspector.

  3. Under Label attributes. You can find Line Break

enter image description here

Done.

0
Melanie Journe On

For everyone looking for more recent solution, swift 3 :

yourLabel.lineBreakMode = .byTruncatingMiddle;
0
Srinivasan N On
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
0
ricks On

Swift 4:

In case someone runs into this issue like i did,

you need to set your label.numberOfLines = 1

if you have it set to 0 it will truncate at a space.

so your code should look like

    label.numberOfLines = 1
    label.lineBreakMode = .byTruncatingTail
    label.adjustsFontSizeToFitWidth = false