Skip to content

CGRect ​

CGRect represents a rectangle: the top-left position x, y plus the width width and height height, all in pt. The coordinate origin is at the top left, and y increases downward.

The rectangle's numbers can be read directly (r.x, r.width) or in the nested form (r.origin.x, r.size.width); both read the same set of numbers.

Fields are read-only; to change one, replace the whole struct with a new one. Where a struct is accepted on write, a plain object with all fields also works. Each property read gives a new object. These rules hold for all structs; see Structs.

Inheritance: None. CGRect is an ordinary JS class; it doesn't inherit from NSObject and isn't an EventTarget.

Constructor ​

CGRect()

Creates a rectangle from four numbers. Arguments not given are 0.

Instance properties ​

PropertyDescription
x read-onlyHorizontal coordinate of the top-left corner, pt
y read-onlyVertical coordinate of the top-left corner, pt
width read-onlyWidth, pt
height read-onlyHeight, pt
origin read-onlyThe top-left corner, a CGPoint. Each read gives a new one
size read-onlyThe size, a CGSize. Each read gives a new one

Where a rectangle is accepted on write, a plain object is recognized only by the four direct fields x, y, width, and height. A shape like { origin, size } isn't accepted and throws as "missing fields".

Examples ​

js
const r = new CGRect(10, 20, 100, 50);

console.log(r.x, r.y, r.width, r.height);
console.log(String(r.origin), String(r.size));
console.log(r.origin.x === r.x);

Result

  • The first line prints 10 20 100 50.
  • The second line prints CGPoint(10, 20) CGSize(100, 50).
  • The third line prints true.

Corresponding iOS API ​

ObjCJS
CGRectMake(0, 0, 100, 50)new CGRect(0, 0, 100, 50)
rect.origin.xrect.origin.x or rect.x
rect.size.widthrect.size.width or rect.width
CGRectZeronew CGRect()

See also ​