swift 自適應(yīng)寬度的UICollectionViewCell實(shí)現(xiàn)

本來(lái)一開(kāi)始使用的是TagListView(pod 'TagListView', '~> 1.4.1'),但是需要在固定寬度的情況下,實(shí)現(xiàn)左右滑動(dòng),于是,只覺(jué)得UICollectionView比較適合,只需要UICollectionViewCell寬度自適應(yīng)就好了。

1.定義UICollectionViewFlowLayout和UICollectionView

let layout = UICollectionViewFlowLayout()
layout.estimatedItemSize = CGSize(width: 60, height: 22)
layout.scrollDirection = .horizontal
layout.minimumLineSpacing = 14
collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
collectionView!.backgroundColor = .white
collectionView!.showsVerticalScrollIndicator = false
collectionView!.showsHorizontalScrollIndicator = false
collectionView!.delegate = self
collectionView!.dataSource = self
collectionView!.register(TextLabelCell.self, forCellWithReuseIdentifier: "TextLabelCell")
self.view.addSubview(collectionView!)
collectionView!.snp.makeConstraints { ConstraintMaker in
      ConstraintMaker.left.equalTo(locationIcon.snp.right).offset(17)
      ConstraintMaker.top.equalTo(self.telLabel.snp.bottom).offset(10)
      ConstraintMaker.height.equalTo(22)
      ConstraintMaker.right.equalToSuperview().offset(-24)
 }

2.定義UICollectionViewCell

class TextLabelCell: UICollectionViewCell {
    
    let textLabel = UILabel()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.contentView.layer.cornerRadius = 11
        self.contentView.layer.masksToBounds = true
        self.contentView.layer.borderWidth = 0.5
        self.contentView.layer.borderColor = UIColor(red: 0.31, green: 0.31, blue: 0.31, alpha: 1).cgColor
        
        textLabel.font = UIFont.YTFont(ofSize: 12)
        textLabel.textColor = UIColor(red: 0.31, green: 0.31, blue: 0.31, alpha: 1)
        textLabel.textAlignment = .center
        textLabel.frame = self.contentView.bounds
        self.contentView.addSubview(textLabel)
        
    }
    override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
        let size = self.textLabel.text?.size(withAttributes: [NSAttributedString.Key.font: UIFont.YTFont(ofSize: 12)]) ?? CGSize.zero
        let att = super.preferredLayoutAttributesFitting(layoutAttributes);
        att.frame = CGRect(x: 0, y: 0, width: size.width+28, height: 22)
        self.textLabel.frame = CGRect(x: 0, y: 0, width: att.frame.size.width, height: 22)
        return att;
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

3.基本就搞定了。主要是設(shè)置layout自適應(yīng),以及cell的寬度自適應(yīng)就可以了。cell的自適應(yīng)寬度需要重寫(xiě)一個(gè)系統(tǒng)方法,上面的代碼已給出。我是需要左右滑動(dòng),你也可以使用上下滾動(dòng)試試。

點(diǎn)擊下方贊賞,給作者一點(diǎn)鼓勵(lì)!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容