[ACCEPTED]-How do I create a CGRect from a CGPoint and CGSize?-cgrectmake

Accepted answer
Score: 124

Two different options for Objective-C:

CGRect aRect = CGRectMake(aPoint.x, aPoint.y, aSize.width, aSize.height);

CGRect aRect = { aPoint, aSize };

Swift 1 3:

let aRect = CGRect(origin: aPoint, size: aSize)
Score: 7

Building on the most excellent answer from 3 @Jim, one can also construct a CGPoint and 2 a CGSize using this method. So these are 1 also valid ways to make a CGRect:

CGRect aRect = { {aPoint.x, aPoint.y}, aSize };
CGrect aRect = { aPoint, {aSize.width, aSize.height} };
CGRect aRect = { {aPoint.x, aPoint.y}, {aSize.width, aSize.height} };
Score: 3
CGRectMake(yourPoint.x, yourPoint.y, yourSize.width, yourSize.height);

0

Score: 0

you can use some sugar syntax. For example:

This 2 is something like construction block you 1 can use for more readable code:

CGRect rect = ({
   CGRect customCreationRect

   //make some calculations for each dimention
   customCreationRect.origin.x = CGRectGetMidX(yourFrame);
   customCreationRect.origin.y = CGRectGetMaxY(someOtherFrame);

   customCreationRect.size.width = CGRectGetHeight(yetAnotherFrame);
   customCreationRect.size.height = 400;

   //By just me some variable in the end this line will
   //be assigned to the rect va
   customCreationRect;
)}

More Related questions