objective c - UILabel automatic height as per the length ios -
i have uicollectionview shown below.the titlelabel can see should vary height per content.the background view labelview should change height.how can that?
i used...
-(cgfloat) heightfortext:(nsstring *)text withwidth:(cgfloat) textwidth{ cgsize constraint = cgsizemake(textwidth, 20000.0f); cgrect rect = [text boundingrectwithsize:constraint options:(nsstringdrawinguseslinefragmentorigin|nsstringdrawingusesfontleading) attributes:@{nsfontattributename:cell.titlelabel.font} context:nil]; cgfloat height = rect.size.height; height = ceilf(height); // nslog(@"height %f", height); return height; }
i used ...
cgrect newframe = cell.titlelabel.frame; newframe.size.height = height; cell.titlelabel.frame = newframe;
i getting new frame label.but height increases fixed y towards down.here have lift y according height.is there other way?
use ankit's code height required label , use autolayout shown below.
add view ---> set leading,trailing , bottom constraints respect cell superview.
then add label subview view.add 4 constraints.ie,set leading,trailing,top , bottom constraints
now, use above ankit's code (i have pasted below) height of label , set label frame.
-(cgfloat) heightfortext:(nsstring *)text withwidth:(cgfloat) textwidth{ cgsize constraint = cgsizemake(textwidth, 20000.0f); cgrect rect = [text boundingrectwithsize:constraint options:(nsstringdrawinguseslinefragmentorigin|nsstringdrawingusesfontleading) attributes:@{nsfontattributename:cell.titlelabel.font} context:nil]; cgfloat height = rect.size.height; height = ceilf(height); // nslog(@"height %f", height); return height; }
set new frame label now.
cgrect newframe = cell.titlelabel.frame; newframe.size.height = height; cell.titlelabel.frame = newframe;
hope works.it setting autolayout.try , let me know.
Comments
Post a Comment