实现如下需求:
titleLabel 和contentLabel的文本内容长度不确定,其中contentLabel可以换行。
image.png
注:主要用到setContentCompressionResistancePriority(_ priority: UILayoutPriority, for axis: NSLayoutConstraint.Axis)
方法
核心代码如下:
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.contentView.addSubview(titleLabel)
self.contentView.addSubview(contentLabel)
configLayout()
}
// 使用Snapkit布局
fileprivate func configLayout() {
titleLabel.snp.makeConstraints { (make) in
make.top.equalTo(self.contentView.snp.top).offset(10)
make.left.equalTo(self.contentView.snp.left).offset(30)
}
contentLabel.snp.makeConstraints { (make) in
make.top.equalTo(titleLabel.snp.top).offset(-1)
make.left.equalTo(titleLabel.snp.right)
make.bottom.equalTo(self.contentView.snp.bottom).offset(-10)
make.right.equalTo(self.contentView.snp.right).offset(-10)
}
}
// 创建Label
fileprivate lazy var titleLabel:UILabel = {
let lb = UILabel()
lb.text = "交易单号:"
// 这句代码起到关键性左右
lb.setContentCompressionResistancePriority(UILayoutPriority.required, for: UILayoutConstraintAxis.horizontal)
lb.textColor = RGBCOLOR(r: 56, g: 56, b: 56)
lb.font = UIFont.systemFont(ofSize: 16)
return lb
}()
fileprivate lazy var contentLabel:JJCustomLabel = {
let lb = JJCustomLabel()
lb.text = "交易单号交易单号交易单号交易单号交易单号交易单号交易单号"
lb.numberOfLines = 0
lb.textColor = RGBCOLOR(r: 56, g: 56, b: 56)
lb.font = UIFont.systemFont(ofSize: 16)
return lb
}()
网友评论