You can do the above for the Button
as @vtabmow
suggested or just add .padding()
to the label:
Button {
// do something
} label: {
Text("Add")
.padding()
}
.foregroundColor(.white)
.background(Color.blue)
.cornerRadius(6.0)
For the TextField
you need to recreate the .textFieldStyle(RoundedBorderTextFieldStyle())
. Think this is quite close
TextField("Input text here", text: $text)
.padding()
.overlay(
RoundedRectangle(cornerRadius: 6)
.stroke(Color.secondary.opacity(0.5))
)
If you are re using this button style in lots of place you might want to do a new file called "PaddedButtonStyle" then put this in it
struct PaddedButtonStyle: ButtonStyle {
let foregroundColor: Color
let backgroundColor: Color
let cornerRadius: Double
func makeBody(configuration: Configuration) -> some View {
configuration.label
.padding()
.foregroundColor(foregroundColor)
.background(backgroundColor)
.cornerRadius(cornerRadius)
}
}
extension View {
func paddedButtonStyle(foregroundColor: Color = .white,
backgroundColor: Color = .blue,
cornerRadius: Double = 6
) -> some View {
self.buttonStyle(PaddedButtonStyle(foregroundColor: foregroundColor,
backgroundColor: backgroundColor,
cornerRadius: cornerRadius)
)
}
}
Now you can add it to your button like this and everywhere you want the same button style
Button("Add") {
// do something
}
.paddedButtonStyle()
I have give it parameter so you can change colors and radius but have the default same as you gave.