Is there a way to add rounded corners to a CAShapeLayer
? In my case I needed the shape layer to create a dashed border via lineDashPattern
.
The answer is simple. Create a bezier path with rounded corners.
self.clipsToBounds = YES;
self.layer.cornerRadius = 10.0;
self.border = [CAShapeLayer layer];
self.border.fillColor = nil;
self.border.path = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:10.0];
self.border.frame = self.bounds;
self.border.strokeColor = [UIColor purpleColor].CGColor;
self.border.lineWidth = borderWidth * 2; // double desired width as half will be clipped
self.border.lineDashPattern = @[@15];
[self.layer addSublayer:self.border];