ios - Sizing a UICollectionViewCell that contains UITextView to fit the contentSize -


i have uicollectionview, 1 of uicollectionviewcells contains uitextview. goal autosize uicollectionviewcell fit content of text view.

i have overridden collectionview:sizeforitematindexpath, i'm struggling @ content height value text view without creating infinite loop.

typically autosize uitextview, following:

cgrect frame = textview.frame; frame.size.height = textview.contentsize.height; textview.frame = frame; 

but within sizeforitematindexpath, need pointer uitextview. pointer, find myself calling cellforitematindexpath, since calls sizeforitematindexpath it's infinite loop.

i suspect i'm missing way pre-size view , have uicollectionview respect that, appears default 50x50 value uicollectionviewflowlayout defines.

other (hack) ideas i'm loathe do:

  • have hidden uitextview can load content , it's height.
  • implement equivalent logic calculate content height on own.

thanks!

you set collection view's delegate text view's delegate, , in textviewdidchange: update property holding up-to-date height calculated textview.textcontainer , textview.textcontainerinset. reference property in sizeforitematindexpath:. in case you'd want type of reloading of cell , possibly layout invalidation every time value of property change.


Comments