Tim Bernikovich
Tim Bernikovich

Reputation: 5945

UILabel with numberOfLines and lineBreakMode

I am working on a project that has to support both iOS6 and iOS7. My problem is it works different on different systems. I'm trying to create UILabel with number of lines equal to 2, but when I set it's line break mode to NSLineBreakByTruncatingTail it works different.

Explanation (numberOfLines = 2, text = @"long teeexxxttt"):

    iOS7                    iOS6
      NSLineBreakByWordWrapping
 ----------              ----------
|long      |            |long      |
|teeeexxxtt|            |teeeexxxtt|
 ----------              ----------

     NSLineBreakByTruncatingTail
 ----------              ----------
|long      |            |long te...|
|teeeexx...|            |          |
 ----------              ----------
     ^                       ^
     |                       |
  correct                incorrect - shows only one line

How do I fix it?

Upvotes: 7

Views: 19613

Answers (3)

Goines
Goines

Reputation: 81

I know this is an old question, but I recently had the same problem. I found that with constraints I had to set the preferred width to get the ellipsis to behave properly:

yourLabel.preferredMaxLayoutWidth = width; 

UILable.preferredMaxLayoutWidth

Upvotes: 7

Juan Boero
Juan Boero

Reputation: 6417

Swift 2.1

yourLabel.text = "your text"
yourLabel.numberOfLines = 0
yourLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
yourLabel.sizeToFit()

Upvotes: 3

Tim Bernikovich
Tim Bernikovich

Reputation: 5945

The problem is iOS6 and prior won't update multiline UILabels with custom UIFont and NSLineBreakByTruncatingTail, but you can archive the same result by using autoresizing or autolayout.

Upvotes: 3

Related Questions