Reputation: 2125
I have a NSCollectionView which presents items in a list.
my layout is defined by
private func configureCollectionView() {
let flowLayout = NSCollectionViewFlowLayout()
flowLayout.itemSize.width = self.view.frame.width
//flowLayout.itemSize.height = 50
flowLayout.minimumLineSpacing = 8
myCollectionView.collectionViewLayout = flowLayout
view.wantsLayer = true
}
When a long peice of text is entered as an item, I am trying to get the item height for that 'cell' to grow/expand with the content of the text. I have setup my contsraints using AutoLayout and I fairly certain that it is setup correctly for this, but my item height remains fixed at 50 (I can't tell where this is getting set).
I am trying to take advantage of
func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize
But I'm not having much luck with this either as I don't know how to best figure out what the Size should be (the height in this case)
Any suggestions on this problem?
Thanks!
Upvotes: 3
Views: 2170
Reputation: 61
You can use sizeThatFits with your desired width and a big enough height
func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
let string = strings[indexPath.item]
let size = NSTextField(labelWithString: string).sizeThatFits(NSSize(width: 400, height: 800))
return NSSize(width: 400, height: size.height > 80 ? size.height : 80)
}
Upvotes: 6