Ios UID标签don';即使我设置'numberOfLines=0,也不能换行`
我有一个自定义的Ios UID标签don';即使我设置'numberOfLines=0,也不能换行`,ios,swift,uilabel,unwrap,Ios,Swift,Uilabel,Unwrap,我有一个自定义的tableViewCell问题内容是我设置的标签: numberOfLines = 0 lineBreakMode = NSLineBreakMode.ByWordWrapping 但它没有包装 这是questionContent标签: let questionContent: UILabel = { let tempView = UILabel() tempView.numberOfLines = 0
tableViewCell
<代码>问题内容是我设置的标签:
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 ssddfghjkk sssss ffhjk sasdfgghjjkkllljhggggffddsssaaaasfghjkklnnvcczzeefghjkkklllkjhggtrddssdfcvxcbbb”
问题内容框架是(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
}()
它对我有用
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()方法。我也遇到了同样的问题。我将标签添加到单元格中,如下所示:
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
中,您是否错过了标签的宽度约束?比如从右标签到屏幕的右边缘?或者u强制标签高度也会导致出现这种情况i设置框架
。这就是问题所在吗?您是否尝试过设置单元格的估计行高<代码>\u tableView.rowHeight=UITableViewAutomaticDimension代码>\u tableView.estimatedRowHeight=50代码>否,我没有设置估计行高。请检查是否调用了“layoutSubviews”方法。