Build: StyledTextScreen
This screen shows a text label styled using a custom ViewModifier to add padding, background color, and rounded corners.
Target UI
------------------------- | | | Hello, SwiftUI! | | | -------------------------
------------------------- | | | Hello, SwiftUI! | | | -------------------------
import SwiftUI struct StyledTextScreen: View { var body: some View { VStack { // TODO: Add Text with custom modifier here } .frame(maxWidth: .infinity, maxHeight: .infinity) .background(Color.white) } } // TODO: Define custom ViewModifier here struct StyledTextScreen_Previews: PreviewProvider { static var previews: some View { StyledTextScreen() } }
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.
------------------------- | | | Hello, SwiftUI! | | (blue rounded box) | | | -------------------------
ViewModifier in SwiftUI?ViewModifier named Highlight to a Text view?.modifier()..modifier(Highlight()) with parentheses to create the modifier instance.struct RedTitle: ViewModifier {
func body(content: Content) -> some View {
content
.font(.title)
.foregroundColor(.red)
}
}
What will this code display?
Text("Welcome").modifier(RedTitle())struct BlueBackground: ViewModifier {
func body(content: Content) -> some View {
content.background(Color.blue)
}
func body(content: Content) -> some View {
content.padding()
}
}body(content:) methods defined, which is not allowed.highlight is true. Which of these implementations correctly applies the border conditionally?Bool to width (CGFloat expected, type error).Color.clear with fixed width 2: invisible but expands frame (layout shift). struct ConditionalBorder: ViewModifier {
var highlight: Bool
func body(content: Content) -> some View {
content
.border(Color.green, width: highlight ? 2 : 0)
}
} uses conditional width 2/0: border only when true, no layout shift.Pass the test and this pattern moves unknown