Iglistkit: IGListBindingSectionController with dynamic size cell

Created on 23 Nov 2017  路  4Comments  路  Source: Instagram/IGListKit

New issue checklist

General information

  • IGListKit version: 3.1.1
  • iOS version(s): 11.1.2
  • CocoaPods/Carthage version: 1.3.1
  • Xcode version: 9.1

I'm having issues with having a type of cell in a IGListBindingSectionController with dynamic size. What I'm trying to achieve is similar to the Objective-C demo or the IGListKit-Binding-Guide example but the comments would resize based on the text size.

I tried with self sizing nibs and static calculating the label height (similar with the LabelCell in the example code) but I wasn't able to get the correct size every time, maybe I'm doing something in the wrong place?

question

Most helpful comment

Hey @brurend! We do sizing almost exactly like that in Instagram, just with Core Text. You can even do stuff like calculate and cache these sizes on a background thread before coming back to main and reloading data!

For some more advanced examples, check out my hobby app:

https://github.com/rnystrom/GitHawk

Sent with GitHawk

All 4 comments

How are you calculating your label height?
I have implemented dynamic sized cells in an IGListBindingSectionController by returning the correct size in sectionController(_ sectionController: ListBindingSectionController<ListDiffable>, sizeForViewModel viewModel: Any, at index: Int) -> CGSize

It just requires computing the label height/width based upon the cell contents and returning a size that accounts for that label size in addition to any other elements or padding.

Yes, this is a on-going topic it's quite difficult to support self-sizing cells with UICollectionView due to weird behaviour and/or bugs in UICollectionView. Currently it's advised to manually calculate the size of yours to avoid yourself a lot of suffering.

For more information see ticket #516

I'm using this method to calculate the height:

+(CGFloat)textHeight:(NSString *)text andWidth:(CGFloat)width {
    UIFont *font = [UIFont systemFontOfSize:14];
    UIEdgeInsets insets = UIEdgeInsetsMake(3, 11, 3, 11);
    CGSize constrainedSize = CGSizeMake(width - insets.left - insets.right, FLT_MAX);
    NSDictionary *attributes = @{ NSFontAttributeName : font };
    NSStringDrawingOptions options = NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin;
    CGRect bounds = [text boundingRectWithSize:constrainedSize options:options attributes:attributes context:nil];
    return ceilf(bounds.size.height) + insets.top + insets.bottom;
}

And returning it at (CGSize)sectionController:(IGListBindingSectionController *)sectionController sizeForViewModel:(id)viewModel atIndex:(NSInteger)index

Hey @brurend! We do sizing almost exactly like that in Instagram, just with Core Text. You can even do stuff like calculate and cache these sizes on a background thread before coming back to main and reloading data!

For some more advanced examples, check out my hobby app:

https://github.com/rnystrom/GitHawk

Sent with GitHawk

Was this page helpful?
0 / 5 - 0 ratings