Afonso
Afonso

Reputation: 417

How to wrap self sizing UICollectionViewCell

My goal is to have a Swift implementation of a UICollectionView with dynamic content where each cell hold parts of a sentence. Each part of the sentence can be edited by the user. The problem is that some parts of a sentence might be longer than the container itself. Using sizeToFit is not an alternative because all content should have the same font size to maintain readability throughout the collection view.

Right now the behaviour I get, when I have a part of a sentence longer than the container is the following: As you can see the third row has leading ellipsis

As you can see the third row has leading ellipsis.

What I would like to achieve is the following, the overflowing part of the cell should wrap as analogous to a span tag in HTML, like so: The third row wraps

Is this possible? How can I achieve such a thing?

Upvotes: 17

Views: 638

Answers (1)

John Goering
John Goering

Reputation: 39040

As far as I can tell, it's possible but complex - here are a few pointers of what I'd say you're going to need:

  • 2 additional collection view cell types: in the first, only the left edges are rounded, in the second only the right
  • Use Core Text API to measure where your text needs to be wrapped
  • In your datasource, you're going to have to then recognize the situation before you create the cells and then instead of creating a single cell, you create two, using those new types you have.

Upvotes: 1

Related Questions