Structure

CGRect

A structure that contains the location and dimensions of a rectangle.

Overview

In the default Core Graphics coordinate space, the origin is located in the lower-left corner of the rectangle and the rectangle extends towards the upper-right corner. If the context has a flipped-coordinate space—often the case on iOS—the origin is in the upper-left corner and the rectangle extends towards the lower-right corner.

Topics

Creating Rectangle Values

init(origin: CGPoint, size: CGSize)

Creates a rectangle with the specified origin and size.

init(x: Double, y: Double, width: Double, height: Double)

Creates a rectangle with coordinates and dimensions specified as floating-point values.

init(x: Int, y: Int, width: Int, height: Int)

Creates a rectangle with coordinates and dimensions specified as integer values.

init(x: CGFloat, y: CGFloat, width: CGFloat, height: CGFloat)

Creates a rectangle with coordinates and dimensions specified as CGFloat values.

Special Values

static let infinite: CGRect

A rectangle that has infinite extent.

static let null: CGRect

The null rectangle, representing an invalid value.

static var zero: CGRect

The rectangle whose origin and size are both zero.

init()

Creates a rectangle with origin (0,0) and size (0,0).

Basic Geometric Properties

var origin: CGPoint

A point that specifies the coordinates of the rectangle’s origin.

var size: CGSize

A size that specifies the height and width of the rectangle.

Calculated Geometric Properties

var height: CGFloat

Returns the height of a rectangle.

var width: CGFloat

Returns the width of a rectangle.

var minX: CGFloat

Returns the smallest value for the x-coordinate of the rectangle.

var midX: CGFloat

Returns the x- coordinate that establishes the center of a rectangle.

var maxX: CGFloat

Returns the largest value of the x-coordinate for the rectangle.

var minY: CGFloat

Returns the smallest value for the y-coordinate of the rectangle.

var midY: CGFloat

Returns the y-coordinate that establishes the center of the rectangle.

var maxY: CGFloat

Returns the largest value for the y-coordinate of the rectangle.

Creating Derived Rectangles

var standardized: CGRect

Returns a rectangle with a positive width and height.

var integral: CGRect

Returns the smallest rectangle that results from converting the source rectangle values to integers.

func applying(CGAffineTransform) -> CGRect

Applies an affine transform to a rectangle.

func insetBy(dx: CGFloat, dy: CGFloat) -> CGRect

Returns a rectangle that is smaller or larger than the source rectangle, with the same center point.

func offsetBy(dx: CGFloat, dy: CGFloat) -> CGRect

Returns a rectangle with an origin that is offset from that of the source rectangle.

func union(CGRect) -> CGRect

Returns the smallest rectangle that contains the two source rectangles.

func intersection(CGRect) -> CGRect

Returns the intersection of two rectangles.

enum CGRectEdge

Coordinates that establish the edges of a rectangle.

Checking Characteristics

func intersects(CGRect) -> Bool

Returns whether two rectangles intersect.

func contains(CGPoint) -> Bool

Returns whether a rectangle contains a specified point.

func contains(CGRect) -> Bool

Returns whether the first rectangle contains the second rectangle.

var isEmpty: Bool

Returns whether a rectangle has zero width or height, or is a null rectangle.

var isInfinite: Bool

Returns whether a rectangle is infinite.

var isNull: Bool

Returns whether the rectangle is equal to the null rectangle.

Alternate Representations

var dictionaryRepresentation: CFDictionary

Returns a dictionary representation of the provided rectangle.

init?(dictionaryRepresentation: CFDictionary)

Creates a rectangle from a canonical dictionary representation.

var customMirror: Mirror

A representation of the rectangle's structure and display style for use in debugging.

var customPlaygroundQuickLook: PlaygroundQuickLook

A representation of the rectangle for use in Playgrounds.

Deprecated

Comparing Rectangles

func equalTo(CGRect) -> Bool

Returns whether two rectangles are equal in size and position.

See Also

Geometric Data Types

struct CGFloat

The basic type for floating-point scalar values in Core Graphics and related frameworks.

struct CGPoint

A structure that contains a point in a two-dimensional coordinate system.

struct CGSize

A structure that contains width and height values.

struct CGVector

A structure that contains a two-dimensional vector.

struct CGAffineTransform

An affine transformation matrix for use in drawing 2D graphics.