UILabel與UITableViewCell的自適應(yīng)

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)
        }
        
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • UITableViewCell 父類是UIView UITableView的每一行都是一個(gè)UITableViewC...
    翻這個(gè)墻閱讀 6,676評(píng)論 0 1
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,251評(píng)論 4 61
  • 行動(dòng)力是多么重要的能力啊
    要贏飛天小豬閱讀 190評(píng)論 0 0
  • 如果這是一場(chǎng)明知無(wú)結(jié)果卻還是要向前的相戀,那我一定會(huì)將它看得很偉大。這不是不慮后果的不負(fù)責(zé),而是敢于嘗試的飛蛾撲火...
    舊城歡閱讀 184評(píng)論 0 0
  • 你走后 我睡在 一個(gè)人的房間 一個(gè)足夠兩個(gè)人躺的床 現(xiàn)在只有我一個(gè) 你那一半還是離開(kāi)的樣子 就讓它保持著吧 回來(lái)你...
    布老頭和他的家人們閱讀 102評(píng)論 1 0