ios - NSString wordwrapping like UILabel NSLineBreakByWordWrapping -
i want wordwrapping string wanted dynamic height calculations purpose, because in uilabel nslinebreakbywordwrapping, counting string height dynamic purpose easy adjust uilabel height please can buddy let me know wordwrapping in string ?
cgsize size = cgsizezero; nsstring *carnamestring = cars.name; carnamestring = [carnamestring stringbyreplacingoccurrencesofstring:@"\n" withstring:@"\n\n"]; cgrect f1 = [carnamestring boundingrectwithsize:cgsizemake([[uiscreen mainscreen]bounds].size.width, cgfloat_max) options:nsstringdrawinguseslinefragmentorigin attributes:@{ nsfontattributename:[uifont fontwithname:@"copperplate" size:20] } context:nil]; size = cgsizemake(f1.size.width, f1.size.height + 1);
note: using ios version 6.3
edit: here uilabel code:
[carlabel setbackgroundcolor:[uicolor clearcolor]]; [carlabel settext:carnamestring]; [carlabel setadjustsfontsizetofitwidth:yes]; [carlabel setlinebreakmode:nslinebreakbywordwrapping]; [carlabel setnumberoflines:0]; [carlabel settextcolor:[uicolor whitecolor]];
you don't seem use size calculated bounding rectangle.
also, i'd advise use same font label used calculate bounding rectangle instead of:
[label setadjustsfontsizetofitwidth:yes];
code should work:
uilabel *label = [[uilabel alloc] initwithframe:cgrectmake(0, 0, size.width, size.height)]; label.numberoflines = 0; label.font = [uifont fontwithname:@"copperplate" size:20]; label.text = carnamestring;
however, code doesn't provide initialization of uilabel , don't let know how set frame of label.
if wish use auto layout, have set translatesautoresizingmaskintoconstraints
no
, use size calculated bounding rect set size of container view.
here's sample based on code describing it:
uiview *containerview = [[uiview alloc] initwithframe:cgrectmake(0, 0, size.width, size.height)]; [self.view addsubview:containerview]; uilabel *label = [uilabel new]; label.translatesautoresizingmaskintoconstraints = no; [containerview addsubview:label]; [containerview addconstraints:[nslayoutconstraint constraintswithvisualformat:@"h:|[label]|" options:0 metrics:nil views:nsdictionaryofvariablebindings(label)]]; [containerview addconstraints:[nslayoutconstraint constraintswithvisualformat:@"v:|[label]|" options:0 metrics:nil views:nsdictionaryofvariablebindings(label)]]; label.numberoflines = 0; label.font = [uifont fontwithname:@"copperplate" size:20]; label.text = carnamestring;
Comments
Post a Comment