Reputation: 387
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
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
Reputation: 1426
In Storyboard
Select the Label which you want to truncate the characters.
Choose Attributes Inspector.
Under Label attributes. You can find Line Break
Done.
Upvotes: 1
Reputation: 1379
For everyone looking for more recent solution, swift 3 :
yourLabel.lineBreakMode = .byTruncatingMiddle;
Upvotes: 5
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
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
Reputation: 1652
Try this:
label.lineBreakMode = NSLineBreakByTruncatingMiddle;
UILineBreakModeMiddleTruncation
is deprecated from iOS 6.0.
Upvotes: 1
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
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
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
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