ios - Use sizeToFit with lineSpacing -
i have uilabel
several lines. text set dynamically, , label height too, native method :
[mylabel sizetofit];
i have other method sets line spacing in label :
nsmutableattributedstring *attributedstring = [[nsmutableattributedstring alloc] initwithstring:@"my long dynamic text"]; nsmutableparagraphstyle *paragraphstyle = [[nsmutableparagraphstyle alloc] init]; [paragraphstyle setlinespacing:5]; [attributedstring addattribute:nsparagraphstyleattributename value:paragraphstyle range:nsmakerange(0, [labeltext length])]; mylabel.attributedtext = attributedstring;
the problem if set line spacing first, , call sizetofit
, new height of label small. doesn't take in count line spacing.
it problem label in uiscrollview
, need right height.
for getting size of label dynamically based on text length use method:
/*! returns size of label display text provided @param text string displayed @param width width required displaying string @param fontname font name label @param fontsize font size label */ - (cgsize)getsizefortext:(nsstring *)text maxwidth:(cgfloat)width font:(nsstring *)fontname fontsize:(float)fontsize { cgsize constraintsize; constraintsize.height = maxfloat; constraintsize.width = width; nsdictionary *attributesdictionary = [nsdictionary dictionarywithobjectsandkeys: [uifont fontwithname:fontname size:fontsize], nsfontattributename, nil]; cgrect frame = [text boundingrectwithsize:constraintsize options:nsstringdrawinguseslinefragmentorigin attributes:attributesdictionary context:nil]; cgsize stringsize = frame.size; return stringsize; }
Comments
Post a Comment