ios - UiTableView label word wrap -
i define separate uilabel class margin label in cell:
- (void)drawtextinrect:(cgrect)rect { uiedgeinsets insets = {5, 10,5, 10}; [super drawtextinrect:uiedgeinsetsinsetrect(rect, insets)]; }
with following code, define height cell:
- (cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath{ if (!self.customcell) { self.customcell = [self.chattableview dequeuereusablecellwithidentifier:@"cellchat"]; } self.customcell.sender.text = [array objectatindex:indexpath.row]; [self.customcell layoutifneeded]; cgfloat height = [self.customcell.contentview systemlayoutsizefittingsize:uilayoutfittingcompressedsize].height; return height +1; }
my question: when test application not take word wrap on right place. why? have uilabel class?
edit:
thanks, still doesn't work. don't have error, when load table view not display text. @ moment, code looks that:
imports:
#import "chatviewcontroller.h" #import "chattableviewcell.h" @interface chatviewcontroller () @property (strong, nonatomic) chattableviewcell *customcell; @end
next:
- (cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath{ uilabel *gettingsizelabel = [[uilabel alloc] init]; gettingsizelabel.font = [uifont fontwithname:@"arial" size:15]; pfobject *tempobject = [array objectatindex:indexpath.row]; gettingsizelabel.text = [tempobject objectforkey:@"sender"]; gettingsizelabel.numberoflines = 0; cgsize maximumlabelsize = cgsizemake(140, maxfloat); cgsize expectedsize = [gettingsizelabel sizethatfits:maximumlabelsize]; return expectedsize.height + (10*2); }
my cellforrowatindexpath:
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifer = @"cellchat"; chattableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifer]; pfobject *tempobject = [array objectatindex:indexpath.row]; cell.sender.text = [tempobject objectforkey:@"sender"]; cell.receptor.text = [tempobject objectforkey:@"receptor"]; cell.sender.linebreakmode = nslinebreakbywordwrapping; cell.sender.numberoflines = 0; return cell; }
edit
do not use self.customcell
inside - (cgfloat)tableview:tableview heightforrowatindexpath:indexpath
instead, calculate cell height this:
- (cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath{ uilabel *gettingsizelabel = [[uilabel alloc] init]; gettingsizelabel.font = font; gettingsizelabel.text = [array objectatindex:indexpath.row]; // text inside [array objectatindex:indexpath.row]; gettingsizelabel.numberoflines = 0; cgsize maximumlabelsize = cgsizemake(yourmaxwidth, maxfloat); cgsize expectedsize = [gettingsizelabel sizethatfits:maximumlabelsize]; return expectedsize.height /*then add margin 'y' margin multiplied 2 top , bottom margin*/; }
and dont forget set
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellid = @"cellchat"; self.customcell = [tableview dequeuereusablecellwithidentifier:cellid]; if (!self.customcell) { self.customcell = [[yourcellclass alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellid]; } // dont forget set self.customcell.textlabel.linebreakmode = nslinebreakbywordwrapping; self.customcell.textlabel.numberoflines = 0; // [self.customcell sizetofit]; optional depending want return self.customcell; }
hope have helped you, happy coding.. cheers! :)
Comments
Post a Comment