0

我有一个习惯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)不换行。这是屏幕截图

在此处输入图像描述

4

3 回答 3

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

于 2017-07-18T00:04:49.493 回答
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
        }()
于 2015-12-21T07:31:04.130 回答
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() 方法。

于 2015-12-21T07:45:53.507 回答