Shapes
Most built-in shapes in SwiftUI accept their proposed size and fill the proposed size. Circle
is the exception; it'll always report a square size. We can see the reported sizes in the sample below by enabling the border, which visualizes the shape's bounding box.
By default, a shape gets filled using the current foreground style. We can either use fill
(as we did in the example above) or add a stroke
to the shape. A stroke traces the shape, drawing half of the line width outside of the bounds (we can visualize the bounds by enabling the border).
Insettable Shapes
Coming soon.
Custom Shapes
We can also define our own custom shapes. In essence, a shape is a wrapper around a Path
— but it takes into account the proposed size. For example, we can write a shape that displays a checkmark sign. By using properties such as maxX
instead of hard coded values, the path always fits exactly inside the proposed rectangle:
We can now use our shape as any other shape, for example by applying a stroke:
When we adjust the size of the frame above, the path of the shape fits exactly inside the rectangle (even when the stroke might draw outside). However, our custom shape does look distorted when it's not drawing within a square. We can enforce a square aspect ratio by returning a square size in the sizeThatFits
method:
Now our shape always renders with a square aspect ratio, regardless of the size we propose:
When we want more control over how the end points are drawn within a stroked shape, we can specify the line cap parameter using a custom stroke style. Likewise, we can specify the line join parameter to control the appearance of the line connections. The example below lets us tweak both parameters:
Checkmark() .stroke(Color.accentColor, style: StrokeStyle(lineWidth: , lineCap: .round, lineJoin: .round)) /* .border(Color.red) */ .padding(30)