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
Creates a rectangle from four numbers. Arguments not given are 0.
Instance properties
| Property | Description |
|---|---|
x read-only | Horizontal coordinate of the top-left corner, pt |
y read-only | Vertical coordinate of the top-left corner, pt |
width read-only | Width, pt |
height read-only | Height, pt |
origin read-only | The top-left corner, a CGPoint. Each read gives a new one |
size read-only | The 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
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
| ObjC | JS |
|---|---|
CGRectMake(0, 0, 100, 50) | new CGRect(0, 0, 100, 50) |
rect.origin.x | rect.origin.x or rect.x |
rect.size.width | rect.size.width or rect.width |
CGRectZero | new CGRect() |
See also
- Structs: rules shared by all structs, and converting to strings and JSON
- Where it is used:
frameandboundsonUIView,frameandboundsonCALayer, rectangle and oval paths onUIBezierPath,scrollRectToVisible()onUIScrollView CGPoint,CGSize: the types oforiginandsize