NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

How to fill and stroke shapes at the same time

Paul Hudson    @twostraws   

Updated for Xcode 14.2

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, and I’m going to show you both here.

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

Circle()
    .strokeBorder(.black, lineWidth: 20)
    .background(Circle().fill(.blue))
    .frame(width: 150, height: 150)

Download this as an Xcode project

A blue circle with a thick black outline.

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

The second option is to layer the two circles manually using ZStack:

ZStack {
    Circle()
        .fill(.red)

    Circle()
        .strokeBorder(.black, lineWidth: 20)
}
.frame(width: 150, height: 150)

Download this as an Xcode project

A red circle with a thick black outline.

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().

Here’s how that looks in code:

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))
    }
}
Hacking with Swift is sponsored by Essential Developer

SPONSORED From March 20th to 26th, you can join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer!

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Similar solutions…

BUY OUR BOOKS
Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 4.5/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.