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

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -