【问题标题】:UILabel don't wrap even if I set `numberOfLines = 0`即使我设置了`numberOfLines = 0`,UILabel 也不会换行
【发布时间】:2015-12-21 07:24:01
【问题描述】:

我有一个自定义的tableViewCellquestionContent 是我设置的标签:

numberOfLines = 0
lineBreakMode = NSLineBreakMode.ByWordWrapping

但它不换行。

这是questionContent标签:

let questionContent: UILabel = {

            let tempView = UILabel()
            tempView.numberOfLines = 0
            tempView.font = UIFont.systemFontOfSize(15)
            tempView.lineBreakMode = NSLineBreakMode.ByWordWrapping
            tempView.backgroundColor = UIColor.redColor()
            return tempView
        }()

layoutSubviews()我设置了它的框架

//questionContent
            let questionContentX = avatorImageX
            let questionContentY = CGRectGetMaxY(avatorImage.frame) + answeModel!.marginTopBottomLeftOrRight
            let questionContentWidth = kScreenWidth - answeModel!.marginTopBottomLeftOrRight * 2
            let questionContentHeight = answeModel!.contentRealSize.height
            questionContent.frame = CGRectMake(questionContentX, questionContentY, questionContentWidth, questionContentHeight)

questionContent.text = "Ssddfghjj ssddfghjjkk sssssffhjjk sasdfgghjjjkkllljhhgggggffdddssssaaaasfghjkkllnnvcczzzeefggghjjkkkklllkjhggtrrrddssdfcvvvxxcbbb"questionContent.frame 的第一个数据是 (8.0, 46.0, 304.0, 84.0),但它不会换行。这是截图

【问题讨论】:

  • 也许你错过了标签的宽度限制?喜欢从右标签到屏幕右边缘?或者你强制标签高度也会导致这种情况发生
  • 我设置了frame。这是问题吗?
  • 您是否尝试过设置单元格的估计行高? _tableView.rowHeight = UITableViewAutomaticDimension;_tableView.estimatedRowHeight = 50;
  • 不,我没有设置估计的行高。
  • 请检查“layoutSubviews”方法是否被调用。

标签: ios swift uilabel unwrap


【解决方案1】:

试试这个代码:

添加sizeToFit 行并尝试。

let questionContent: UILabel = {

            let tempView = UILabel()
            tempView.numberOfLines = 0
            tempView.font = UIFont.systemFontOfSize(15)
            tempView.lineBreakMode = NSLineBreakMode.ByWordWrapping
            tempView.sizeToFit()
            tempView.backgroundColor = UIColor.redColor()
            return tempView
        }()

【讨论】:

    【解决方案2】:

    它对我有用。

    let questionContent: UILabel = {
    
            let tempView = UILabel()
            tempView.numberOfLines = 0
            tempView.font = UIFont.systemFontOfSize(15)
            tempView.lineBreakMode = NSLineBreakMode.ByWordWrapping
            tempView.backgroundColor = UIColor.redColor()
            tempView.text = " This is two long text.This is two long text.This is two long text.This is two long text.This is two long text.This is two long text.This is two long text.";
            tempView.frame = CGRectMake(0, 0, 200, 10.0);
            tempView.sizeToFit()
            return tempView
            }()
    

    无需写入 layoutsubview() 方法。

    【讨论】:

      【解决方案3】:

      我也遇到了同样的问题。我正在像这样将标签添加到单元格中:

      override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
          super.init(style: style, reuseIdentifier: reuseIdentifier)
          addSubview(messageText) // add to cell itself
      }
      

      然后又过了很久,改成:

      override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
          super.init(style: style, reuseIdentifier: reuseIdentifier)
          contentView.addSubview(messageText)
      }
      

      只需要将标签添加到contentView

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-11-02
        • 2023-03-04
        • 2020-10-03
        • 2021-05-18
        • 2017-02-26
        • 1970-01-01
        • 1970-01-01
        • 2022-01-18
        相关资源
        最近更新 更多