UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

RoundedRectangle vs Capsule ?

Forums > SwiftUI

@00jim  

What is the difference between a RoundedRectangle and a Capsule Shape in SwiftUI ?

   

A Capsule shape has a semi circle at the horizontal end, where a RoundedRectangle has corners rounded off depending on the radius of corner and which corners.

You can make a Capsule shape with RoundedRectangle by making the corner radius half the height of the frame!

   

@00jim  

I understand that, but to get a capsule one need only modify a rectangle, so what was the point ?

   

The point is that you do not need to know the height of the frame for it to have semi circle edges

Did this to show that

struct ContentView: View {
    let texts = [" World!", "Goodbye"]
    @State private var count = 0
    @State private var text = "Hello"
    var body: some View {
        VStack {
            Text(text)
                .frame(width: 170)
                .background(.purple)
                .clipShape(.capsule)

            Text(text)
                .frame(width: 170)
                .background(.cyan)
                .clipShape(.rect(cornerRadius: 40))

            Button("Tap Me") {
                if count < texts.count {
                    text += texts[count]
                    count += 1
                }
            }
            .buttonStyle(.borderedProminent)

        }
        .font(.largeTitle)
        .foregroundColor(.white)
        .padding()
    }
}

when you tap the button you will see that the first one has round corner the same however the second one has vertical straight edges.

You can also make a Circle with a Rectangle

RoundedRectangle(cornerRadius: 25.0)
    .fill(.blue)
    .frame(width: 50, height: 50)

   

@00jim  

That's a pretty weak excuse on Apple's behalf.

Why didn't Apple do what Pixelmator did and allow for shapes to be created based on a number of points e.g. three for a triangle and five for a pentagon etc..

Anyway, thanks for the demo !

   

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Click to save your free spot now

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

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.