UILabel with numberOfLines and lineBreakMode

拜拜、爱过 提交于 2019-12-21 12:06:05

问题


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?


回答1:


Swift 2.1

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



回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/20903975/uilabel-with-numberoflines-and-linebreakmode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!