swift 如何使collectionViewCell具有不同宽度的内容

fdbelqdn  于 2022-11-21  发布在  Swift
关注(0)|答案(1)|浏览(225)

我使用collectionViewCell创建了一个选项卡菜单,但是,我有5个单元格菜单。但是单元格中的名称被截断,我想知道是否有一个长名称标签。collectionViewCell中的容器视图应该增长。

我尝试使用collectionViewFlowLayout估计ItemSize,但结果是。单元格变得不像以前那样难看

如何使collectionViewCell增大大小但保持全宽。
这是我的collectionViewCell的设置

private let padding: CGFloat = 8

override init(frame: CGRect) {
        super.init(frame: frame)
        configure()
    }
    
required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
}

private func configure() {
        addSubview(selectionView)
        selectionView.addSubview(titleLabel)

        NSLayoutConstraint.activate([
            selectionView.topAnchor.constraint(equalTo: topAnchor, constant: padding),
            selectionView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 4),
            selectionView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -4),
            selectionView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -padding),

            titleLabel.topAnchor.constraint(equalTo: selectionView.topAnchor, constant: padding),
            titleLabel.leadingAnchor.constraint(equalTo: selectionView.leadingAnchor, constant: 4),
            titleLabel.trailingAnchor.constraint(equalTo: selectionView.trailingAnchor, constant: -4),
            titleLabel.bottomAnchor.constraint(equalTo: selectionView.bottomAnchor, constant: -padding)
        ])
    }

这是我的收藏视图设置

private func setupCollectionView() {
        let layout = UICollectionViewFlowLayout()
        layout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize

        collectionView = UICollectionView(frame: bounds, collectionViewLayout: layout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(TabCollectionViewCell.self, forCellWithReuseIdentifier: TabCollectionViewCell.cellID)

        [notificationLabel, collectionView].forEach { view in
            guard let view = view else { return }
            view.translatesAutoresizingMaskIntoConstraints = false
            addSubview(view)
        }

        NSLayoutConstraint.activate([
            notificationLabel.topAnchor.constraint(equalTo: topAnchor, constant: 8),
            notificationLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: padding),
            notificationLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -padding),

            collectionView.topAnchor.constraint(equalTo: notificationLabel.bottomAnchor, constant: 8),
            collectionView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: padding),
            collectionView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -padding),
            collectionView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -8)
        ])

    }

extension HeaderCollectionView: UICollectionViewDelegateFlowLayout {

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: collectionView.bounds.width / CGFloat(items.count), height: collectionView.bounds.height)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }

}
mm5n2pyu

mm5n2pyu1#

创建一个String扩展方法,该方法将计算字符串的宽度,然后在委托方法中使用该宽度:
步骤1:计算字符串宽度

extension String {
    func getStringwidth(height: CGFloat, font: UIFont) -> CGFloat {
        let constraintRect = CGSize(width: .greatestFiniteMagnitude, height: height)
        let boundingBox = self.boundingRect(with: constraintRect, options: [.usesLineFragmentOrigin, .usesFontLeading], attributes: [NSAttributedString.Key.font: font], context: nil)
        return boundingBox.width
    }
}

步骤3:计算最大宽度

func calculateMaxWidth()-> CGFloat {
        // my Data shource array Data
        var allSize: [CGFloat] = []
        arrayData.forEach{
            allSize.append($0.widthWithConstrainedHeight(height: 30,
                                                         font: UIFont.systemFont(ofSize: 17)))
        }
        return allSize.max() ?? 0.0
        
    }

步骤2:从viewdidLoad调用max

override func viewDidLoad() {
    super.viewDidLoad()
    
    let layout = UICollectionViewFlowLayout()
    layout.estimatedItemSize =  CGSize(width: calculateMaxWidth(), height: 30)

根据您的要求进行调整。
输出:

相关问题