Truncate part of text in UILabel
Asked Answered
D

10

21

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?

Disbursement answered 6/12, 2013 at 6:47 Comment(3)
Try this label.lineBreakMode = NSLineBreakByTruncatingMiddle;Subsocial
I cannot use middle truncation because i need to display only the last few character as "abcdefgijk...10". But NSLineBreakByTruncatingMiddle will give something like "abcde...lmn10"Disbursement
I think this post will help Truncate UILabel text before 5 character of stringEphemeron
R
16
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
Rackety answered 25/4, 2014 at 14:21 Comment(0)
Q
10

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
Quarterdeck answered 13/3, 2018 at 15:47 Comment(0)
F
5

For everyone looking for more recent solution, swift 3 :

yourLabel.lineBreakMode = .byTruncatingMiddle;
Fuentes answered 7/11, 2016 at 14:13 Comment(0)
N
1

Try this:

 label.lineBreakMode = NSLineBreakByTruncatingMiddle;

UILineBreakModeMiddleTruncation is deprecated from iOS 6.0.

Nitrate answered 6/12, 2013 at 6:59 Comment(1)
Is it Co-incident that Krishna - Balram is together. Well thanks for Answer and CorrectionOutdistance
A
1

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.

Allegorist answered 14/12, 2017 at 5:20 Comment(0)
C
0

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

Compliant answered 6/12, 2013 at 6:57 Comment(0)
M
0

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];
Mccourt answered 6/12, 2013 at 6:59 Comment(2)
What do you mean by n here ? is that n = [fooString length] ?Coquetry
no, here n is the maximum characters that can come in that lineMccourt
P
-1

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

Pharmacology answered 6/12, 2013 at 7:1 Comment(1)
Yes, we are. But they do not give us full control on the exact '...' place.Tenia
B
-1

If you using XIB file..

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

simply way to truncate characters...

Bewray answered 6/12, 2013 at 7:5 Comment(0)
C
-1

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

Coquetry answered 6/12, 2013 at 7:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.