ios - Adjust width of UILabel based on its contents -


i had on before asking question. questions adjusting height of uilabel , not width. tried alternative methods did not work such [label sizetofit];. possible adjust width of label based on text? create label in uitableviewcell in story board. want able adjust width based on text assigned. dont want resize font size.

i set text of label in cellforrowatindexpath.

examples great please.

thanks in advance :)

update 1: have custom cell making in storyboard not programmatically. set contents of each cell in cellforrowatindexpath, example, mylabel.text = recipe.name. name label quite small, extend width based on length of text, not truncate tail or shrink size of text.

update2: have lot of other uielements in cell. have label in top left, top right, bottom left, bottom right, , picture in middle, there default 120 because have background color. set small there not huge amount of empty space in label.

get size of string:

  //replace flt_max maximum height/width want label be, if no maximum leave flt_max. cgsize stringsize = [your_string sizewithfont:your_font constrainedtosize:cgsizemake(flt_max, flt_max) linebreakmode:nslinebreakbywordwrapping]; 

then size label:

[your_label setframe:cgrectmake(0, 0, stringsize.width, stringsize.height)]; 

in ios 7 sizewithfont deprecated, use boundingrectwithsize instead:

nsdictionary *stringattributes = [nsdictionary dictionarywithobject:your_labels_font                                                                  forkey: nsfontattributename];  cgsize stringsize = [text boundingrectwithsize:cgsizemake(flt_max, flt_max)                                              options:nsstringdrawingtruncateslastvisibleline |                                                      nsstringdrawinguseslinefragmentorigin                                           attributes:stringattributes context:nil].size; 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -