CGRect, CGSize and CGPoint Functions

With an understanding of C structures, and the definitions of CGRect, CGSize and CGPoint behind us, let’s look at a handful of functions for working with these structures.

CGRectMake and NSStringFromCGRect

With CGRectMake we can create a new CGRect structure. The rectangles below have differing starting points, however, each have a width and height of 100. The function NSStringFromCGRect returns a string object that defines the rectangle passed in as a parameter:

Note : The output of all examples are shown in the image at the bottom of this post.


To determine if two rectangles intersect, you can write code as follows:


If you need to create a rectangle that is either larger or smaller than an existing rectangle, centered on the same point, try CGRectInset:


A little more uncommon, is creating rectangles from a string, however, if the need arises:

Functions for CGSize and CGPoint

As you would expect, there are similar functions to those above for working with CGSize and CGPoint:

  • CGSizeMake
  • CGSizeEqualToSize
  • CGPointMake
  • CGPointEqualToPoint

The output for the above code examples follows:

  1. Thanks for the post… You do a great job covering stuff of interest to new iPhone developers… and I often find your blog postings in the results of my google searches, so kudos to you!

  2. HI John,

    Thanks for the post. It seems that the quotes in the NSLog(@””) are replaced by the NSLog(@""); in your post.

  3. What if the values I get are the following:
    2011-11-09 14:12:50.176 Robofair2011[4977:207] handleDoubleTap
    2011-11-09 14:12:50.177 Robofair2011[4977:207] zoomRectForScale
    2011-11-09 14:12:50.177 Robofair2011[4977:207] handleDoubleTap zoomRect: {{-inf, -inf}, {inf, inf}}

  4. Your code and blog is really very helpful, Thank you very much.

Comments are closed.