user2990765
user2990765

Reputation: 387

Truncate part of text in UILabel

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?

Upvotes: 21

Views: 42628

Answers (10)

ricks
ricks

Reputation: 3324

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

Upvotes: 10

iSrinivasan27
iSrinivasan27

Reputation: 1426

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.

Upvotes: 1

Melanie Journe
Melanie Journe

Reputation: 1379

For everyone looking for more recent solution, swift 3 :

yourLabel.lineBreakMode = .byTruncatingMiddle;

Upvotes: 5

Srinivasan N
Srinivasan N

Reputation: 611

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

Upvotes: 16

mak
mak

Reputation: 1183

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];

Upvotes: 0

Krishna Kumar
Krishna Kumar

Reputation: 1652

Try this:

 label.lineBreakMode = NSLineBreakByTruncatingMiddle;

UILineBreakModeMiddleTruncation is deprecated from iOS 6.0.

Upvotes: 1

Balram Tiwari
Balram Tiwari

Reputation: 5667

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

Upvotes: -1

jeyachandran
jeyachandran

Reputation: 325

If you using XIB file..

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

simply way to truncate characters...

Upvotes: -1

Jyothi
Jyothi

Reputation: 384

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

Upvotes: -1

Waseem05
Waseem05

Reputation: 1224

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

Upvotes: 0

Related Questions