0

I am trying to achieve this kind of tag layout (collectionview cells with dynamic width and height) but code crashes with some constraint errors when the text for a label within a cell is very long. is there a way to have a label with multiple lines of text with this kind of layout? how do i restrict the maximum width of a cell to be the widthdimension of the group and also support more than one line of text? also if, for example, the label for item 3 (see image) is going to have two lines of text, i want that cell to wrap around to row 2 and occupy rows 2 and 3. item 4 should then start on row 4.

if multiple lines of text in a label is not possible with this layout, how do i restrict the number of lines in a label to 1 and also restrict width of label to be the widthdimension of the group i.e. truncate the text in a label if it is going to occupy more than one line in the label?

enter image description here

func makeLayout() -> UICollectionViewLayout {
    let layout = UICollectionViewCompositionalLayout { (section: Int, environment: NSCollectionLayoutEnvironment) -> NSCollectionLayoutSection? in

        let itemSize = NSCollectionLayoutSize(widthDimension: .estimated(100), heightDimension: .estimated(32))
        let layoutItem = NSCollectionLayoutItem(layoutSize: itemSize)

        let layoutGroupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: itemSize.heightDimension)
        let layoutGroup = NSCollectionLayoutGroup.horizontal(layoutSize: layoutGroupSize, subitems: [layoutItem])

        layoutGroup.interItemSpacing = .fixed(8)

        let section = NSCollectionLayoutSection(group: layoutGroup)
        section.contentInsets = .init(top: 0, leading: 16, bottom: 0, trailing: 16)
        section.interGroupSpacing = 8
            
        return section
    }
    return layout
}

caa5042
  • 166
  • 3
  • 16

0 Answers0