mischa-hildebrand / AlignedCollectionViewFlowLayout

A collection view layout that gives you control over the horizontal and vertical alignment of the cells.
MIT License
1.27k stars 203 forks source link

Multi-line labels break the layout #11

Closed dimovskidamjan closed 6 years ago

dimovskidamjan commented 6 years ago

If you enter a longer string, even in your example project, in one of the cells, the whole layout breaks and none of the cells are loaded. Is there a way to provide support for multi-line labels which would resize the cell dynamically not only by width as it does now but by height also?

mischa-hildebrand commented 6 years ago

That's because you're setting up your cell's layout incorrectly. The flow layout itself can handle multi-line labels without any problem.

💡 The trick with multi-line labels (numberOfLines = 0) in general is that you somehow need to tell them what their maximum width should be. How else would a multi-line label decide when to break into a new line?

There are two ways to achieve this:

  1. You constrain the label with a width inequality constraint, e.g. label.width <= 140 (pseudocode).
  2. You set the labels preferredMaxLayoutWidth manually to that value.

You can do both things in Interface Builder if you want.

dimovskidamjan commented 6 years ago

@mischa-hildebrand Thanks! Just wanted to confirm that I tried these and it works as expected with either of the proposed solutions. I knew I was missing something :)

mischa-hildebrand commented 6 years ago

Awesome! I'm happy to hear that! 🙃