Thursday 15 August 2013

ios - Fit Text in UItableView Cell -



ios - Fit Text in UItableView Cell -

i have long label first label, , want fit in cell. have isn't working.

i have custom uitabelviewcell few labels in it.

-(cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath { switch (indexpath.row) { case 0:{ cgsize constraint = cgsizemake(cell_content_width - (cell_content_margin * 2), 20000.0f); cgsize size = [dininghalltimes[indexpath.row][@"description"] sizewithfont:[uifont systemfontofsize:font_size] constrainedtosize:constraint linebreakmode:nslinebreakbywordwrapping]; cgfloat height = max(size.height, 44.0f); homecoming height + (cell_content_margin * 2) + 40; // homecoming mystringsize.height; break; } default: homecoming 40; break; } }

here cell row

- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { dininginfotableviewcell *cell = [tableview dequeuereusablecellwithidentifier:@"cell" forindexpath:indexpath]; cgsize constraint = cgsizemake(cell_content_width - (cell_content_margin * 2), 20000.0f); cgsize size = [dininghalltimes[indexpath.row][@"description"] sizewithfont:[uifont systemfontofsize:font_size] constrainedtosize:constraint linebreakmode:nslinebreakbywordwrapping]; cgfloat height = max(size.height, 44.0f); cgfloat = height + (cell_content_margin * 2); cell.descriptionlabel.numberoflines = 0; [cell.descriptionlabel setframe:cgrectmake(20, 0, 280, the)]; nslog(@"%f", the); cell.descriptionlabel.text = dininghalltimes[indexpath.row][@"description"]; cell.dayslabel.text = dininghalltimes[indexpath.row][@"days"]; cell.timelabel.text = dininghalltimes[indexpath.row][@"time"]; homecoming cell; }

but cell looks like

not sure why happening, running ios8, need work both , 7.

thanks help in advance.

you need calculate boundingrectwithsize: label text. need calculate number of lines required updated content also. seek below method calculate new frame label.

- (uilabel *) updatelabelframe:(uilabel *)label { cgrect lblrect = label.frame; cgsize maxsize = cgsizemake(label.frame.size.width, maxfloat); cgrect labelrect = [label.text boundingrectwithsize:maxsize options:nsstringdrawinguseslinefragmentorigin attributes:@{nsfontattributename:label.font} context:nil]; cgfloat labelheight = labelrect.size.height; // below line 16 default height (the height before content set). alter value // per requirement int lines = labelheight / 16; // here 16 prepare height (default height) label. [label setnumberoflines:lines]; lblrect.size.height = labelheight; [label setframe:lblrect]; homecoming label; }

edit:

you can place method want. base of operations class or in same view controller. here modified above method homecoming label dynamic frame.

for case need place method in view controller class. phone call method in -cellforrowatindexpath after content updated label. see below code:

- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { dininginfotableviewcell *cell = [tableview dequeuereusablecellwithidentifier:@"cell" forindexpath:indexpath]; cgsize constraint = cgsizemake(cell_content_width - (cell_content_margin * 2), 20000.0f); cgsize size = [dininghalltimes[indexpath.row][@"description"] sizewithfont:[uifont systemfontofsize:font_size] constrainedtosize:constraint linebreakmode:nslinebreakbywordwrapping]; cgfloat height = max(size.height, 44.0f); cgfloat = height + (cell_content_margin * 2); cell.descriptionlabel.numberoflines = 0; [cell.descriptionlabel setframe:cgrectmake(20, 0, 280, the)]; nslog(@"%f", the); cell.descriptionlabel.text = dininghalltimes[indexpath.row][@"description"]; cell.dayslabel.text = dininghalltimes[indexpath.row][@"days"]; cell.timelabel.text = dininghalltimes[indexpath.row][@"time"]; // update descriptionlabel height. cell.descriptionlabel = [self updatelabelframe:cell.descriptionlabel]; homecoming cell; }

ios objective-c uitableview uilabel

No comments:

Post a Comment