Skip to content

CAShapeLayer ​

CAShapeLayer represents a shape layer: it draws a path (UIBezierPath). It first fills the area enclosed by the path, then strokes along the path. Line charts, pie charts, and progress rings are all drawn with it.

It is a layer, and like any other sublayer you set its frame yourself and add it under some view's layer. It does not resize itself to fit the path: coordinates in the path are points in the layer's own coordinate system, with the origin at the layer's top-left corner.

Note: fillColor defaults to opaque black, and strokeColor defaults to null. When you want just a line, set fillColor to null and give strokeColor a color.

CAShapeLayer fires no events. Its properties are changed only by the app's code, so there are no change notifications.

Inheritance: EventTarget → NSObject → CALayer → CAShapeLayer

For drawing polylines, pie slices, progress rings, and dashed lines, see the guide Drawing rounded corners, shadows, gradients, and shapes with layers.

Constructor ​

CAShapeLayer()

Creates a shape layer. Its size is 0 × 0 pt and it has no path.

Instance properties ​

Also inherits the properties of CALayer; commonly used ones are frame and masksToBounds.

PropertyDescription
pathThe path to draw

Fill:

PropertyDescription
fillColorFill color
fillRuleWhich areas count as inside the path

Stroke:

PropertyDescription
strokeColorLine color
lineWidthLine width
lineCapShape of line ends
lineJoinShape of corners
miterLimitLength limit for sharp corners
strokeStartWhere along the path the stroke starts
strokeEndWhere along the path the stroke ends
lineDashPatternHow dashes are drawn
lineDashPhaseWhere in the dash pattern drawing starts

Instance methods ​

CAShapeLayer has no methods of its own. It also inherits the methods of CALayer.

Examples ​

A polyline ​

js
const card = new UIView();
card.frame = new CGRect(20, 100, 300, 160);

const path = new UIBezierPath();
path.moveToPoint(new CGPoint(24, 120));
path.addLineToPoint(new CGPoint(96, 60));
path.addLineToPoint(new CGPoint(168, 96));

const line = new CAShapeLayer();
line.path = path;
line.fillColor = null;
line.strokeColor = 'systemBlue';
line.lineWidth = 3;
line.frame = card.bounds;
card.layer.addSublayer(line);

Effect: a 3 pt blue polyline with two segments appears on the card.

Corresponding iOS API ​

ObjCJS
[CAShapeLayer layer]new CAShapeLayer()
shape.path = path.CGPathshape.path = path
shape.fillColor = UIColor.clearColor.CGColorshape.fillColor = null
shape.fillRule = kCAFillRuleEvenOddshape.fillRule = 'evenOdd'
shape.lineCap = kCALineCapRoundshape.lineCap = 'round'
shape.lineJoin = kCALineJoinBevelshape.lineJoin = 'bevel'
shape.lineDashPattern = @[@4, @2]shape.lineDashPattern = [4, 2]

See also ​