2015-12-21 712 views
0

我有一个自定义的tableViewCell。 questionContent是一个标签,我设置了numberOfLines = 0lineBreakMode = NSLineBreakMode.ByWordWrapping但它没有换行。即使我设置了`numberOfLines = 0`,UILabel也不会换行

存在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)但它不换行。这里是截图

enter image description here

+0

也许ü错过标签的宽度约束?比如从正确的标签到屏幕的右边缘?或者你强迫标签高度也会导致这种情况发生 – Tj3n

+0

我设置了'frame'。这是问题吗? – rose

+0

您是否尝试过设置单元格的估计行高? '_tableView.rowHeight = UITableViewAutomaticDimension;''_tableView.estimatedRowHeight = 50;' –

回答

0

试试这个代码:

添加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 
     }() 
+0

我试过了,但没有奏效 – rose

0

它对我很有用。

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()方法。

0

我只是陷入了同样的问题。我添加标签的电池是这样的:

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

相关问题