Thursday 15 January 2015

objective c - UILabel setFrame is not resizing the label, even in layoutSubviews -



objective c - UILabel setFrame is not resizing the label, even in layoutSubviews -

i've got:

#import "customtableviewcell.h" @interface customtableviewcell () @property (assign) cgrect titleframe; @property (assign) cgrect detailframe; @end @implementation customtableviewcell - (id)initwithstyle:(uitableviewcellstyle)style reuseidentifier:(nsstring *)reuseidentifier { self = [super initwithstyle:style reuseidentifier:reuseidentifier]; if (self) { // initialization code } homecoming self; } - (void)awakefromnib { if (self.title != nil) { self.titleframe = self.title.frame; } if (self.detail != nil) { self.detailframe = self.detail.frame; } } - (void)setselected:(bool)selected animated:(bool)animated { [super setselected:selected animated:animated]; // configure view selected state } - (void) settitleto: (nsstring *)text { if (self.title == nil || text == nil) { return; } cgsize maximumlabelsize = cgsizemake(self.title.frame.size.width, flt_max); cgrect expectedlabelsize = [text boundingrectwithsize:maximumlabelsize options:nslinebreakbywordwrapping | nsstringdrawinguseslinefragmentorigin attributes:@{nsfontattributename:self.title.font} context:nil]; cgrect newframe = self.title.frame; newframe.size.height = expectedlabelsize.size.height; self.titleframe = newframe; self.detail.linebreakmode = nslinebreakbywordwrapping; self.detail.numberoflines = 3; [self.title settext:text]; [self setneedslayout]; } - (void) setdetailto: (nsstring *)text { if (self.detail == nil || text == nil) { return; } cgsize maximumlabelsize = cgsizemake(self.detail.frame.size.width, flt_max); cgrect expectedlabelsize = [text boundingrectwithsize:maximumlabelsize options:nslinebreakbywordwrapping | nsstringdrawinguseslinefragmentorigin attributes:@{nsfontattributename:self.title.font} context:nil]; cgrect newframe = self.detail.frame; newframe.size.height = expectedlabelsize.size.height; self.detailframe = newframe; self.detail.linebreakmode = nslinebreakbywordwrapping; self.detail.numberoflines = 3; [self.detail settext:text]; [self setneedslayout]; } - (void) layoutsubviews { [super layoutsubviews]; if (self.title != nil) { nslog(@"t: %@", self.title.text); [self.title setframe:self.titleframe]; } if (self.detail != nil) { nslog(@"d: %@", self.detail.text); [self.detail setframe:self.detailframe]; } } @end

from i'm reading here should resize fit text.

and nslog output:

2014-10-08 20:48:29.474 test[57397:613] t: largest metro 2014-10-08 20:48:29.475 test[57397:613] d: oahu metropolitan area

my understanding "oahu metropolitan area" should visible "area" on sec line.

three things should change:

move size computation of label layoutsubviews. the precise way compute label's size utilize -[uilabel sizethatfits:] it's desired size. turn off auto layout

objective-c uilabel frame

No comments:

Post a Comment