How to fill and stroke basic shapes in swiftUI?

By | October 9, 2024

In latest iOS versions ( ios 17 onwards) we can directly put stroke and fill shapes just by adding modifiers one after another as shown below.

struct ContentView: View {
    
    @State private var showAlert = false
    
    var body: some View {
        VStack {
            Circle()
                .stroke(.blue, lineWidth: 20)
                .fill(.green)
                .frame(width: 150, height: 150)
        }
    }
}

It works with multiple strokes of various sizes as shown below.

            Circle()
                .stroke(.orange, lineWidth: 50)
                .stroke(.green, lineWidth: 40)
                .stroke(.blue, lineWidth: 30)
                .stroke(.yellow, lineWidth: 20)
                .stroke(.red, lineWidth: 10)
                .frame(width: 300, height: 300)

In older versions of iOS ( iOS 16 ) SwiftUI provides the fill()stroke(), and strokeBorder() modifiers for adjusting the way we draw shapes, but it does not provide a built-in way to fill and stroke at the same time. However, we can get the same effect in two different ways as shown below.

            Circle()
                .strokeBorder(.red, lineWidth: 20)
                .background(Circle().fill(.yellow))
                .frame(width: 150, height: 150)

The first option is to use strokeBorder() to add a border around your shape, then place a filled shape in the background using background()as shown in above example, this creates a circle with a black stroke and blue fill as shown below.

Using background() ensures the yellow circle always matches the size of the red circle.

The second option is to layer the two circles manually using ZStack as shown below.

    var body: some View {
        VStack {
            ZStack {
                Circle()
                    .fill(.orange)
                Circle()
                    .strokeBorder(.red, lineWidth: 20)
            }
            .frame(width: 150, height: 150)
        }
    }

If you want to fill and stroke lots of shapes, you should consider wrapping up this functionality in an extension. Only InsettableShapes get the strokeBorder() method, so you should probably write two extension methods – one to handle regular shapes using stroke(), and one to handle insettable shapes using strokeBorder()

extension Shape {
    func fill<Fill: ShapeStyle, Stroke: ShapeStyle>(_ fillStyle: Fill, strokeBorder strokeStyle: Stroke, lineWidth: Double = 1) -> some View {
        self
            .stroke(strokeStyle, lineWidth: lineWidth)
            .background(self.fill(fillStyle))
    }
}

extension InsettableShape {
    func fill<Fill: ShapeStyle, Stroke: ShapeStyle>(_ fillStyle: Fill, strokeBorder strokeStyle: Stroke, lineWidth: Double = 1) -> some View {
        self
            .strokeBorder(strokeStyle, lineWidth: lineWidth)
            .background(self.fill(fillStyle))
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *