ios - How and where to do dynamic height resize of UITextView inside a custom TableCellView, built in storyboard? -
going crazy...read dozens of "this how it..." answers, differ 1 another, , none work in case. i've messed uitextview frame, bounds, contentsize, etc. , though see these change via nslog, final output reverts original size set in storyboard.
using storyboard placed read-only uitextview in subclassed tableviewcell, named groupchatviewcell.
i load handful of text strings uitextview through controller's viewdidload. strings word wrapped , longer can fit in defined uitextview frame.
i resize tableview cell (which works) in tableview:heightforrowatindexpath delegate , trying resize uitextview (does not work) in tableview:cellforrowatindexpath delegate.
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"groupchatviewcell"; groupchatviewcell *cell = (groupchatviewcell *)[tableview dequeuereusablecellwithidentifier:cellidentifier]; if (cell == nil) { cell = [[groupchatviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier]; } // retrieve messagedata given row groupmessagedata *amessagedata = [messagethreadarray objectatindex:indexpath.row]; [cell.messagelabel settext:amessagedata.msgtext]; [cell.messagelabel sizetofit]; // [cell.messagelabel layoutifneeded]; --> did try this, not work // [cell.messagelabel layoutsubviews]; --> did try this, not work return cell; } - (cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath { groupmessagedata *amessagedata = [messagethreadarray objectatindex:indexpath.row]; groupchatviewcell *cell = (groupchatviewcell *)[tableview dequeuereusablecellwithidentifier:@"grooupchatviewcell"]; uifont *cellfont = [uifont systemfontofsize:12]; cgsize constraintsize = cgsizemake(cgrectgetwidth(cell.bounds), maxfloat); cgsize labelsize = [amessagedata.msgtext sizewithfont:cellfont constrainedtosize:constraintsize linebreakmode:nslinebreakbywordwrapping]; return ceil(labelsize.height + 60); }
i end increased size tablecell, not uitextview unchanged , clipped original size set in storyboard, , showing 3 lines of text fit.
what need do, , where, expand uitextview's height full size allocated in cell , display lines of text???
you need change frame of uitextview inside table cell controller. example:
- (cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath { groupmessagedata *amessagedata = [messagethreadarray objectatindex:indexpath.row]; groupchatviewcell *cell = (groupchatviewcell *)[tableview dequeuereusablecellwithidentifier:@"grooupchatviewcell"]; uifont *cellfont = [uifont systemfontofsize:12]; cgsize constraintsize = cgsizemake(cgrectgetwidth(cell.bounds), maxfloat); cgsize labelsize = [amessagedata.msgtext sizewithfont:cellfont constrainedtosize:constraintsize linebreakmode:nslinebreakbywordwrapping]; [cell adjusttextviewheight]; return ceil(labelsize.height + 60); }
and in groupchatviewcell.m:
- (void) adjusttextviewheight{ cgrect frame = self.textview.frame; self.textview.frame = cgrectmake(frame.origin.x, frame.origin.y, frame.size.width, self.textview.contentsize.height); }
this should change height of textview well. also, if have maximum height cell, can use min function choose right height. example:
instead of : self.textview.contentsize.height
can have: min (maxheight, self.textview.contentsize.height)
Comments
Post a Comment