UILabel与UITableViewCell的自适应

1.UILabel 的自适应
(1)无其他控件,使用纯代码

  dOtherDescribeLabel.numberOfLines = 3
        dOtherDescribeLabel.textAlignment = .left
        dOtherDescribeLabel.textColor = UIColor.gray
        // 设置label的最大宽度
        dOtherDescribeLabel.preferredMaxLayoutWidth = 100
        dOtherDescribeLabel.font = UIFont.systemFont(ofSize: 15)
        // label做自适应,多行显示的时候,只设置宽就好,高会根据字号自            适应显示
        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布局,此时我们需要自适应的label的上述(1)中的设置最好在xib上设置,理论上代码与xib都是一样的,但是视图用代码设置,xib与代码存在未知冲突,导致设置不管用。所以在xib依次设置,切记优先级不能任性写,优先级越高越好.
(1)设置label最大宽度

UILabel与UITableViewCell的自适应_第1张图片
label最大宽度.png
UILabel与UITableViewCell的自适应_第2张图片
设置label宽度优先级.png

(3)UITableViewCell 的高度自适应

  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)
        }
        
    }

你可能感兴趣的:(UILabel与UITableViewCell的自适应)