1.UILabel 的自適應(yīng)
(1)無(wú)其他控件,使用純代碼
dOtherDescribeLabel.numberOfLines = 3
dOtherDescribeLabel.textAlignment = .left
dOtherDescribeLabel.textColor = UIColor.gray
// 設(shè)置label的最大寬度
dOtherDescribeLabel.preferredMaxLayoutWidth = 100
dOtherDescribeLabel.font = UIFont.systemFont(ofSize: 15)
// label做自適應(yīng),多行顯示的時(shí)候,只設(shè)置寬就好,高會(huì)根據(jù)字號(hào)自 適應(yīng)顯示
self.dOtherDescribeLabel.mas_makeConstraints { (make) in
make?.top.mas_equalTo()(self.dLeftDescribeLabel.mas_top)
make?.left.mas_equalTo()(self.dLeftDescribeLabel.mas_right)?.offset()(30)
make?.width.mas_equalTo()(60)?.with().priority()(MASLayoutPriority(600))
}
(2)有其他控件,所有控件都用xib布局,此時(shí)我們需要自適應(yīng)的label的上述(1)中的設(shè)置最好在xib上設(shè)置,理論上代碼與xib都是一樣的,但是視圖用代碼設(shè)置,xib與代碼存在未知沖突,導(dǎo)致設(shè)置不管用。所以在xib依次設(shè)置,切記優(yōu)先級(jí)不能任性寫,優(yōu)先級(jí)越高越好.
(1)設(shè)置label最大寬度
label最大寬度.png
設(shè)置label寬度優(yōu)先級(jí).png
(3)UITableViewCell 的高度自適應(yīng)
var contentLabel: UILabel!
var countryLabel: UILabel!
var newsPaparLabel: UILabel!
var timeLabel: UILabel!
let marginTop: CGFloat = 10
let marginLeft: CGFloat = 15
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.setupView()
self.setupConstraint()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setupView() {
let preferredWidth: CGFloat = UIScreen.main.bounds.size.width - marginLeft * 2
let contentLabel = UILabel()
contentLabel.preferredMaxLayoutWidth = preferredWidth
contentLabel.numberOfLines = 4
contentLabel.textAlignment = .left
contentLabel.textColor = UIColor.black
contentLabel.font = UIFont.systemFont(ofSize: 15)
self.contentView.addSubview(contentLabel)
self.contentLabel = contentLabel
let countryLabel = UILabel()
countryLabel.numberOfLines = 1
countryLabel.textAlignment = .left
countryLabel.textColor = UIColor.red
countryLabel.font = UIFont.systemFont(ofSize: 11)
countryLabel.setBorder(UIColor.red, 1, 3)
self.contentView.addSubview(countryLabel)
self.countryLabel = countryLabel
let newsPaparLabel = UILabel()
newsPaparLabel.numberOfLines = 1
newsPaparLabel.textAlignment = .left
newsPaparLabel.textColor = UIColor.init(red: 153.0/255.0, green: 153.0/255.0, blue: 153.0/255.0, alpha: 1)
newsPaparLabel.font = countryLabel.font
self.contentView.addSubview(newsPaparLabel)
self.newsPaparLabel = newsPaparLabel
let timeLabel = UILabel()
timeLabel.numberOfLines = 1
timeLabel.textAlignment = .left
timeLabel.textColor = newsPaparLabel.textColor
timeLabel.font = countryLabel.font
self.contentView.addSubview(timeLabel)
self.timeLabel = timeLabel
}
func setupConstraint() {
let sueprView = self
self.contentLabel.mas_makeConstraints { (make) in
make?.top.mas_equalTo()(sueprView.marginTop)
make?.right.mas_equalTo()(0-sueprView.marginLeft)
make?.bottom.equalTo()(sueprView.contentView)?.with().offset()(0-sueprView.marginLeft)?.with().priority()(749)
make?.left.mas_equalTo()(sueprView.marginLeft)
}
self.countryLabel.mas_makeConstraints { (make) in
make?.top.equalTo()(sueprView.contentLabel.mas_bottom)?.with().offset()(25)
make?.bottom.mas_equalTo()(0-sueprView.marginTop)
make?.height.mas_equalTo()(20)?.with().priority()(1000)
make?.left.equalTo()(sueprView.contentLabel)
}
self.newsPaparLabel.mas_makeConstraints { (make) in
make?.top.equalTo()(sueprView.contentLabel.mas_bottom)?.with().offset()(25)
make?.bottom.mas_equalTo()(0-sueprView.marginTop)
make?.left.equalTo()(sueprView.countryLabel.mas_right)?.with().offset()(sueprView.marginLeft)
}
self.timeLabel.mas_makeConstraints { (make) in
make?.top.equalTo()(sueprView.contentLabel.mas_bottom)?.with().offset()(25)
make?.bottom.mas_equalTo()(0-sueprView.marginTop)
make?.left.equalTo()(sueprView.newsPaparLabel.mas_right)?.with().offset()(sueprView.marginLeft)
}
}