HorizontalCollectionView Content width and spacing

前端 未结 2 966
野趣味
野趣味 2021-01-24 20:47

How can I make my horizontal collection view labels width to wrap the content width of the label and make them have equal spacing between each of them? Currently I have

2条回答
  •  广开言路
    2021-01-24 21:09

    You have to manually calculate the width of each cell. To calculate the width, you actually calculate the width of the text residing in that cell.

    use following

     boundingRect(with:options:attributes:context:)
    

    https://developer.apple.com/documentation/foundation/nsstring/1524729-boundingrect

    to calculate the width of a given string. So after calculating the returning the size the cell should be wrapping the text.

    Now come the second part, equal spacing between items. use interItemSpacing to define the spacing between each cell.

提交回复
热议问题