Define CGRectmake with CGPoint and/or CGSize

cgrect, cgrectmake, objective-c, syntax

Solution

I think this is what you have in mind:

    - (void) logRects
    {
        CGFloat
            x      = 10.0,
            y      = 20.0,
            width  = 50.0,
            height = 60.0;

        CGPoint point = {x, y};
        CGSize  size  = {width, height};

        CGRect  rect1 = {1, 3, size};
        CGRect  rect2 = {point, size};
        CGRect  rect3 = {point, size.width, size.height};

            //using designated (named) initialisers
        CGRect  rect4 = {.origin.x=3, .origin.y=5, .size = {100,100}};

            //with designated initialisers, order doesn't matter
        CGRect  rect5 = {.size=size, .origin.x=3, .origin.y=5};

        NSLog (@"rect1 %@",NSStringFromCGRect(rect1));
        NSLog (@"rect2 %@",NSStringFromCGRect(rect2));
        NSLog (@"rect3 %@",NSStringFromCGRect(rect3));
        NSLog (@"rect4 %@",NSStringFromCGRect(rect4));
        NSLog (@"rect5 %@",NSStringFromCGRect(rect5));
    }

But note the discussion here: Why use functions like CGRectMake?

This kind of compound literal syntax seems to me much easier to read and write, although functions have the edge when it comes to futureproofing ( + you get autocomplete).

update see also this more recent q&a:

CGRect syntax I haven't seen before

Problem

This is a very simple question of which i can't seem to find the answer. How to do this : ``` CGRectMake(1, 3, size) ``` or ``` CGRectMake(pointB, size) ``` or ``` CGRectMake(pointB, size.width, size.height) ``` instead of ``` CGRectMake(self.someview.frame.origin.x, self.someview.frame.origin.y, self.someotherview.frame.size.width, self.someotherview.frame.size.height) ``` ?? Thank you ! :) EDIT: The method `CGRectmake` takes `CGFloat`. I would like it to take `CGSize`, and/or `CGpoint` as arguments of the method.

Original source

Related problems