ios - Can I figure out the numberOfLines of NSString without creating UILabel? -
i want know number of lines, given font, constraints, , text. can figure out without creating uilabel
?
+ (int)numberoflines:(nsdictionary *)data{ nsstring *mystring = [some string calculation]; cgsize sizeconstrain = cgsizemake(some constrain calculation); cgsize stringsize = [mystring sizewithfont:somefont constrainedtosize:sizeconstrain]; cgrect labelframe = cgrectmake(0, 0, stringsize.width, stringsize.height + 2); uilabel *label = [[uilabel alloc]initwithframe:labelframe]; label.text = mystring; return label.numberoflines; }
yes
+ (int)numberoflines:(nsdictionary *)data{ nsstring *mystring = [some string calculation]; cgsize sizeconstrain = cgsizemake(some constrain calculation); cgsize stringsize = [mystring sizewithfont:somefont constrainedtosize:sizeconstrain]; return (stringsize.height/somefont.lineheight); }
edit: used uitextview , ios7
- (cgfloat) getrowsfortext:(nsstring*) text{ cgfloat fixedwidth = 300; uifont *font = [uifont fontwithname:@"helveticaneue" size:14]; nsmutableparagraphstyle *paragrapstyle = [[nsmutableparagraphstyle alloc] init]; paragrapstyle.alignment = nstextalignmentleft; textstepattr = [nsdictionary dictionarywithobjectsandkeys: font,nsfontattributename, paragrapstyle, nsparagraphstyleattributename, nil]; nsattributedstring *attributedtext = [[nsattributedstring alloc] initwithstring:text attributes:textstepattr]; cgrect rect = [attributedtext boundingrectwithsize:cgsizemake(fixedwidth, maxfloat) options:nsstringdrawinguseslinefragmentorigin context:nil]; return (rect.size.height / font.lineheight) ; }
Comments
Post a Comment