Class

NSBezierPath

An object that can create paths using PostScript-style commands.

Overview

Paths consist of straight and curved line segments joined together. Paths can form recognizable shapes such as rectangles, ovals, arcs, and glyphs; they can also form complex polygons using either straight or curved line segments. A single path can be closed by connecting its two endpoints, or it can be left open.

An NSBezierPath object can contain multiple disconnected paths, whether they are closed or open. Each of these paths is referred to as a subpath. The subpaths of a Bézier path object must be manipulated as a group. The only way to manipulate subpaths individually is to create separate NSBezierPath objects for each.

For a given NSBezierPath object, you can stroke the path’s outline or fill the region occupied by the path. You can also use the path as a clipping region for views or other regions. Using methods of NSBezierPath, you can also perform hit detection on the filled or stroked path. Hit detection is needed to implement interactive graphics, as in rubber banding and dragging operations.

The current graphics context is automatically saved and restored for all drawing operations involving Bézier path objects, so your application does not need to worry about the graphics settings changing across invocations.

Topics

Creating an NSBezierPath Object

init(ovalIn: NSRect)

Creates and returns a new NSBezierPath object initialized with an oval path inscribed in the specified rectangle.

init(rect: NSRect)

Creates and returns a new NSBezierPath object initialized with a rectangular path.

init(roundedRect: NSRect, xRadius: CGFloat, yRadius: CGFloat)

Creates and returns a new NSBezierPath object initialized with a rounded rectangular path.

var flattened: NSBezierPath

A flattened version of the path object.

var reversed: NSBezierPath

A path containing the reversed contents of the current path object.

Constructing Paths

func move(to: NSPoint)

Moves the receiver’s current point to the specified location.

func line(to: NSPoint)

Appends a straight line to the receiver’s path

func curve(to: NSPoint, controlPoint1: NSPoint, controlPoint2: NSPoint)

Adds a Bezier cubic curve to the receiver’s path.

func close()

Closes the most recently added subpath.

func relativeMove(to: NSPoint)

Moves the receiver’s current point to a new point whose location is the specified distance from the current point.

func relativeLine(to: NSPoint)

Appends a straight line segment to the receiver’s path starting at the current point and moving towards the specified point, relative to the current location.

func relativeCurve(to: NSPoint, controlPoint1: NSPoint, controlPoint2: NSPoint)

Adds a Bezier cubic curve to the receiver’s path from the current point to a new location, which is specified as a relative distance from the current point.

Appending Common Shapes to a Path

func append(NSBezierPath)

Appends the contents of the specified path object to the receiver’s path.

func appendPoints(NSPointArray, count: Int)

Appends a series of line segments to the receiver’s path.

func appendOval(in: NSRect)

Appends an oval path to the receiver, inscribing the oval in the specified rectangle.

func appendArc(from: NSPoint, to: NSPoint, radius: CGFloat)

Appends an arc to the receiver’s path.

func appendGlyph(NSGlyph, in: NSFont)

Appends an outline of the specified glyph to the receiver’s path.

Deprecated
func appendGlyphs(UnsafeMutablePointer<NSGlyph>, count: Int, in: NSFont)

Appends the outlines of the specified glyphs to the receiver’s path.

Deprecated
func appendPackedGlyphs(UnsafePointer<Int8>)

Appends an array of packed glyphs to the receiver’s path.

Deprecated
func appendRect(NSRect)

Appends a rectangular path to the receiver’s path.

func appendRoundedRect(NSRect, xRadius: CGFloat, yRadius: CGFloat)

Appends a rounded rectangular path to the receiver’s path.

Accessing the Path Attributes

var windingRule: NSBezierPath.WindingRule

The winding rule used to fill the path.

var lineCapStyle: NSBezierPath.LineCapStyle

The line cap style for the path.

var lineJoinStyle: NSBezierPath.LineJoinStyle

The line join style for the path.

var lineWidth: CGFloat

The width of stroked path lines.

var miterLimit: CGFloat

The limit at which miter joins are converted to bevel joins.

var flatness: CGFloat

The accuracy with which curves are rendered.

func setLineDash(UnsafePointer<CGFloat>?, count: Int, phase: CGFloat)

Sets the line-stroking pattern for the receiver.

Configuring the Default Path Attributes

class var defaultWindingRule: NSBezierPath.WindingRule

Returns the default winding rule used to fill all paths.

class var defaultLineCapStyle: NSBezierPath.LineCapStyle

Returns the default line cap style for all paths.

class var defaultLineJoinStyle: NSBezierPath.LineJoinStyle

Returns the default line join style for all paths.

class var defaultLineWidth: CGFloat

Returns the default line width for the all paths.

class var defaultMiterLimit: CGFloat

Returns the default miter limit for all paths.

class var defaultFlatness: CGFloat

Returns the default flatness value for all paths.

Drawing Paths

func stroke()

Draws a line along the receiver’s path using the current stroke color and drawing attributes.

func fill()

Paints the region enclosed by the receiver’s path.

class func fill(NSRect)

Fills the specified rectangular path with the current fill color.

class func stroke(NSRect)

Strokes the path of the specified rectangle using the current stroke color and the default drawing attributes.

class func strokeLine(from: NSPoint, to: NSPoint)

Strokes a line between two points using the current stroke color and the default drawing attributes.

class func drawPackedGlyphs(UnsafePointer<Int8>, at: NSPoint)

Draws a set of packed glyphs at the specified point in the current coordinate system.

Clipping Paths

func addClip()

Intersects the area enclosed by the receiver's path with the clipping path of the current graphics context and makes the resulting shape the current clipping path.

func setClip()

Replaces the clipping path of the current graphics context with the area inside the receiver's path.

class func clip(NSRect)

Intersects the specified rectangle with the clipping path of the current graphics context and makes the resulting shape the current clipping path

Hit Detection

func contains(NSPoint)

Returns a Boolean value indicating whether the receiver contains the specified point.

Querying Paths

var bounds: NSRect

The bounding box of the receiver’s path

var controlPointBounds: NSRect

The bounding box of the receiver’s path, including any control points

var currentPoint: NSPoint

The receiver’s current point (the trailing point or ending point in the most recently added segment).

var isEmpty: Bool

A Boolean value indicating whether the path is empty.

Applying Transformations

func transform(using: AffineTransform)

Transforms all points in the receiver using the specified transform.

Accessing Elements of a Path

var elementCount: Int

The total number of path elements in the receiver's path.

func element(at: Int)

Returns the type of path element at the specified index.

func element(at: Int, associatedPoints: NSPointArray?)

Gets the element type and (and optionally) the associated points for the path element at the specified index.

func removeAllPoints()

Removes all path elements from the receiver, effectively clearing the path.

func setAssociatedPoints(NSPointArray?, at: Int)

Changes the points associated with the specified path element.

Constants

enum NSBezierPath.ElementType

Basic path element commands.

enum NSBezierPath.LineJoinStyle

The shape of the joins between connected segments of a stroked path.

enum NSBezierPath.LineCapStyle

The shape of endpoints for an open path when it is stroked.

enum NSBezierPath.WindingRule

These constants are used to specify the winding rule a Bezier path should use.

Relationships

Inherits From