3

I have text like this: "My text is blabla blabla, lala lala ".

I would like to have the text in my UILabel like this: "My text is ...lala".

How can I configure my UILabel to display the text to have the ellipsis in the middle?

Juan Boero
  • 6,281
  • 1
  • 44
  • 62
samir
  • 4,501
  • 6
  • 49
  • 76

3 Answers3

4

The word you are looking for is "ellipsis" ;)

Set the following properties:

label.adjustsFontSizeToFitWidth = false;
label.lineBreakMode = .byTruncatingMiddle;

You can also set these properties in interface builder.

Example stolen from here: Getting UILabel to produce an ellipsis rather than shrinking the font

UPDATE:

This was deprecated in iOS 6. The current solution would be the slightly modified:

label.adjustsFontSizeToFitWidth = NO;
label.lineBreakMode = NSLineBreakByTruncatingMiddle 
Yash Jadhav
  • 95
  • 1
  • 14
Derek Tomes
  • 3,989
  • 3
  • 27
  • 41
0

@DerekTomes answer in Swift 2.x:

label.adjustsFontSizeToFitWidth = false
label.lineBreakMode = .ByTruncatingMiddle
Juan Boero
  • 6,281
  • 1
  • 44
  • 62
  • There is no need to have the part before `.ByTruncatingMiddle`. It is redundant. – Wyetro Jul 21 '16 at 22:26
  • 1
    This fails to answer the question, given that it is for Swift 2 and the question is clearly tagged for Objective-C. Additionally, this adds no new information to the post since it is just a copy of the already accepted answer. Including the NSLineBreakMode is redundant regardless of experience level, as it is unneeded code. – Wyetro Jul 22 '16 at 16:31
  • @WMios thanks for your feedback, now the tag is added and the author was given its credit. Besides that we should let the community choose if this answer helps someone or not, your only criteria should not be used for that purpose, since you might be neglecting people the right to know, and totally misleading the purpose of the site. – Juan Boero Jul 22 '16 at 20:59
0

Try to select the label, then in attribute inspector set line break to truncate middle image reference

CAPSLOCK
  • 6,243
  • 3
  • 33
  • 56
Talha Ahmed
  • 160
  • 1
  • 10