Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 544
Afonso Avatar asked Mar 16 '16 13:03

Afonso


People also ask

How do I resize a collection view cell?

First you have to Add the UICollectionViewDelegateFlowLayout delegate and then use following delegate method it will work fine for me. Show activity on this post. Implement the below method,, it is autoresized base on the screen you have.

What is Preferredlayoutattributesfitting?

Gives the cell a chance to modify the attributes provided by the layout object.

How do I make my UICollectionView scroll horizontal?

You need to reduce the height of UICollectionView to its cell / item height and select " Horizontal " from the " Scroll Direction " as seen in the screenshot below. Then it will scroll horizontally depending on the numberOfItems you have returned in its datasource implementation.


1 Answers

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.
like image 113
Epaga Avatar answered Oct 12 '22 14:10

Epaga