我有一个自定义tableViewCell
。 questionContent
是我设置的标签:
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)
,但不会包装。这是屏幕截图最佳答案
试试这个代码:
添加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
}()