我有一个自定义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),但不会包装。这是屏幕截图

ios - 即使我设置`numberOfLines = 0`,UILabel也不会包装-LMLPHP

最佳答案

试试这个代码:

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

08-16 01:39