Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

chrono1081

macrumors G3
Original poster
Jan 26, 2008
8,452
4,149
Isla Nublar
Sorry guys, another question :(

I have this code here:

Code:
//Add some text
    NSString *text = @"You are getting sleepy.";
    
    //Get a font to draw it in
    UIFont *font = [UIFont boldSystemFontOfSize:28];
    
    //Where am I going to draw it?
    CGRect textRect;
    [B]textRect.size = [text sizeWithFont:font]; //Confusion here[/B]
    textRect.origin.x = center.x - textRect.size.width  * 0.5;
    textRect.origin.y = center.y - textRect.size.height * 0.5;
    
    //Set the fill color of the current context to black
    [[UIColor blackColor] setFill];
    
    //Set the shadow to be offset 4 points right, 3 points down
    //and dark grey with a blur radius of 2 points
    CGSize offset = CGSizeMake(4, 3);
    CGColorRef color = [[UIColor darkGrayColor] CGColor];
    CGContextSetShadowWithColor(context, offset, 2.0, color);
    
    //Draw the string
   [B] [text drawInRect:textRect withFont:font]; //Confusion here[/B]
}

The parts in bold is what I am confused on. Neither "sizeWithFont" or "drawInRect" are a methods of NSString so how am I able to call these on an NSString object?

Both are methods of type CGSize, and I found they are defined in the UIStringDrawing.h file that is part of the UIKit but I can't understand the link between them or how I am able to call these methods on NSString objects.

Is there some kind of exception where because this code is all within an overridden drawRect function of a UIView subclass I can call these methods on strings?

I've been digging around the documentation for awhile and haven't found an answer yet :/
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
The parts in bold is what I am confused on. Neither "sizeWithFont" or "drawInRect" are a methods of NSString so how am I able to call these on an NSString object?

sizeWithFont: is an NSString method under UIKit. What makes you think it isn't? Make sure you are looking at iOS documentation and not Mac docs.
 

chrono1081

macrumors G3
Original poster
Jan 26, 2008
8,452
4,149
Isla Nublar
sizeWithFont: is an NSString method under UIKit. What makes you think it isn't? Make sure you are looking at iOS documentation and not Mac docs.

Thanks so much! Sorry my questions always end up being obvious :( I'm not sure where I was looking (Mostly in NSString, and around UIView and such) but the link you provided had it all right at the top.

Thanks again for the help :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.