Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 753
Timur Bernikovich Avatar asked Jan 03 '14 12:01

Timur Bernikovich


2 Answers

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

like image 58
Goines Avatar answered Oct 02 '22 22:10

Goines


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.

like image 25
Timur Bernikovich Avatar answered Oct 03 '22 00:10

Timur Bernikovich