ios - Custom UICollectionViewLayout: Get size of a certain cell -


i'm building custom uicollectionviewlayout client based on design specifications achieve , flow want. there issue i'm running involves size of cell in i'm positioning.

it is possible cells can of different sizes , layout style i'm going requires specific spacing requirements cells don't overlap each other, means during overloaded -layoutattributesforitematindexpath: in layout i'd peek items size can set , used in calculations properly.

the issue i'm having appears uicollectionview depending on method set size of cells because until set attributes method use fetch size cell result in cgsizezero.

is i'm going have design around? sizes cells in storyboard aren't accessible (at least methods i'm aware of) haven't been able ot solve other use specific width/height (which works 1 of 2 possible cells) test , write layout code.

ultimately: how fetch size of uicollectionviewcell in uicollectionviewlayout use in calculations?.

edit:

i've tried following methods:

cgrect frame = [[self.collectionview cellforitematindexpath:path] frame]; cgsize size = [[self.collectionview cellforitematindexpath:path] intrinsiccontentsize]; cgrect frame = [[[self.collectionview cellforitematindexpath:path] contextview] frame]; 

i've never found way avoid duplicating size calculations in code. here's do:

getting size in layout:

the layout can talk collection view find sizes (as things minimum inter-item spacing, etc.). can in -preparelayout or -layoutattributesforitematindexpath::

if ([self.collectionview.delegate respondstoselector:@selector(collectionview:layout:sizeforitematindexpath:)]) {     id<uicollectionviewdelegateflowlayout> delegate = (id<uicollectionviewdelegateflowlayout>) self.collectionview.delegate;     size = [delegate collectionview:self.collectionview layout:self sizeforitematindexpath:indexpath]; } else {     size = self.itemsize; } 

now, collection view delegate needs able respond collectionview:layout:sizeforitematindexpath:, have been necessary without custom layout class.

calculating size in first place:

it little troublesome provide correct sizes sometimes. there's no magical way size of cell without instantiating , laying out, end writing code myself. of collection view cells have class method:

+ (cgsize)sizewithitem:(id<mydisplayableitem>)item; 

i call collection view delegate when need size cell take:

-(cgsize)collectionview:(uicollectionview *)collectionview layout:(uicollectionviewlayout *)collectionviewlayout sizeforitematindexpath:(nsindexpath *)indexpath {     note* note = self.notes[indexpath.item];     cgsize result = [csdetailnotecell sizewithnote:note];     // or [mycell sizewithitem:myitem andmyotheritem:somethingelse]     return result; } 

unfortunately, +sizewithitem: method doesn't come free, @ least keeps size calculation encapsulated within cell class. tend store constant in class representing size of things never change (padding, maybe image). then, combine fixed size things need calculated dynamically find final size. dynamic stuff includes calls nsstring's -boundingrectwithsize:options:attributes:context:, etc.

sample size calculation:

this example uses cell width 320, think it's enough point across. code csdetailnotecell.m.

static cgfloat const csdetailnotecellwidth = 320.f; static cgfloat const csdetailnotecellheightconstant =         10 + // top padding         16 + // date label height         0 + // padding date label textview         8 + // textview top padding         8 + // textview bottom padding         22 // bottom padding ;  ...  + (cgfloat)textwidth {     return csdetailnotecelltextviewwidth - 10; // internal textview padding }  + (cgsize)sizewithnote:(csnote *)note {     cgfloat textheight = [self _textheightwithtext:note.text];     cgsize result = cgsizemake(csdetailnotecellwidth, csdetailnotecellheightconstant + textheight);     return result; }  + (cgfloat)_textheightwithtext:(nsstring*)text {     nsmutableparagraphstyle *paragraphstyle = [[nsmutableparagraphstyle alloc] init];      cgsize maxsize = cgsizemake(csdetailnotecell.textwidth, cgfloat_max);     nsdictionary* attributes = @{nsfontattributename: csdetailnotecell.font, nsparagraphstyleattributename: paragraphstyle};     cgrect boundingbox = [text boundingrectwithsize:maxsize options:nsstringdrawinguseslinefragmentorigin attributes:attributes context:nil];     cgfloat textheight = ceilf(boundingbox.size.height);     return textheight; } 

Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -