objective c - Finding the width of an NSString as a CGFloat -


i know has been posted already, solution found

-(cgfloat)widthoftext{     cgrect idealframe = [self.text boundingrectwithsize:self.frame.size                                            options:nsstringdrawinguseslinefragmentorigin                                          attributes:@{ nsfontattributename:self.font }                                             context:nil];      return idealframe.size.width; } 

self uilabel

isn't working.

i'm trying place button @ end of string need width of string calculate buttons absolute placement

can explain why isn't working or suggest different method?

one or more of inputs not expect, nslog() self.text, self.frame, self.font -- basic debugging.

it better pass in parameters use ivars, allows easier testing. removes dependency on class.

example works properly:

nsstring *text = @"1235"; cgsize framesize = cgsizemake(300, 50); uifont *font = [uifont systemfontofsize:12];  cgrect idealframe = [text boundingrectwithsize:framesize                                        options:nsstringdrawinguseslinefragmentorigin                                     attributes:@{ nsfontattributename:font }                                        context:nil]; nslog(@"width: %0.1f", idealframe.size.width); 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -