Monday, November 21, 2016

Dynamic CollectionView Cell size create based on text length size .



Firstly we need to calculate text length and configure to collection view cell size layout.

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
        CGSize calCulateSize =[(NSString*)[_categoryList objectAtIndex:indexPath.row] sizeWithAttributes:NULL];
        calCulateSize.width = calCulateSize.width+50; // for better look we add more 50 pixel
        calCulateSize.height = 50; // fixed height
        return calCulateSize;
}



as usual, we implement all of the required collection view delegate and data source, then we see look like collection view cell. demo attached here, if any things need, let me know.


Demo here

No comments:

Post a Comment