import SwiftUI
struct RoundedBackground: ViewModifier {
func body(content: Content) -> some View {
content
.padding(16)
.background(Color.blue)
.cornerRadius(10)
.foregroundColor(.white)
}
}
extension View {
func roundedBackground() -> some View {
self.modifier(RoundedBackground())
}
}
struct StyledTextScreen: View {
var body: some View {
VStack {
Text("Hello, SwiftUI!")
.roundedBackground()
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.white)
.ignoresSafeArea()
}
}
struct StyledTextScreen_Previews: PreviewProvider {
static var previews: some View {
StyledTextScreen()
}
}We created a custom ViewModifier called RoundedBackground. It adds padding around the content, sets a blue background color, rounds the corners with a radius of 10, and changes the text color to white for contrast.
We also added a convenient extension on View to apply this modifier easily with .roundedBackground().
Inside the StyledTextScreen, we put a Text view with the string "Hello, SwiftUI!" and applied our custom modifier. The VStack centers the text vertically and horizontally by expanding to fill the screen.
This approach keeps styling reusable and clean, just like putting on a favorite jacket to look nice.