ios4 - How do I create a multiline table cell in iOS? -
how can second cell expand fit text rather scaling text? there built in way of doing in ios or have come home-cooked solution? if in ios contacts application, there's box address. can't find how implement though.
for looking achieve in future, here's code solution:
header file:
#define font_size 22.0f #define cell_content_width 320.0f #define cell_content_margin 5.0f
implementation file:
- (cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath { if (indexpath.section == 0 && indexpath.row == 1) { nsstring *text = [atmannotation address]; cgsize constraint = cgsizemake(cell_content_width - (cell_content_margin * 2), 20000.0f); cgsize size = [text sizewithfont:[uifont systemfontofsize:font_size] constrainedtosize:constraint linebreakmode:uilinebreakmodewordwrap]; nslog(@"size address height:%f width:%f",size.height,size.width); cgfloat height = max(size.height, 44.0f); return height + (cell_content_margin * 2); } return 44.0f; }
here's screenshot of result:
unfortunately, going have implement feature yourself. there variety of methods , callbacks need make use of calculate height of rows , labels. if need getting started, can amend answer include sample code. otherwise, i'm sure there related questions here on or google can started. in summary, however:
- determine height of row in
uitableviewdelegate
method-tableview:heightforrowatindexpath:
. you'll need use nsstring uikit addition methods calculate how tall string given font size, etc. return proper height delegate method. - make sure label (either in ib or configured programmatically) set use multiple lines. if set
numberoflines
0, label use many lines necessary. - in
uitableviewdatasource
implementation of-tableview:cellforrowatindexpath:
, you'll need use same logic before determine frame of labels. set text want, change frame label tall enough barely fit text.
Comments
Post a Comment