Build: Simple Text Styling
This screen shows a text label with different styles applied using modifiers.
Target UI
------------------------- | | | Hello, SwiftUI! | | | | (Styled Text Label) | | | -------------------------
------------------------- | | | Hello, SwiftUI! | | | | (Styled Text Label) | | | -------------------------
import SwiftUI struct ContentView: View { var body: some View { // TODO: Add your Text view with modifiers here Text("") } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } }
import SwiftUI struct ContentView: View { var body: some View { Text("Hello, SwiftUI!") .font(.largeTitle) .foregroundColor(.blue) .padding() .frame(maxWidth: .infinity, maxHeight: .infinity) .background(Color.white) .multilineTextAlignment(.center) } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } }
We created a Text view with the string "Hello, SwiftUI!". Then, we applied the .font(.largeTitle) modifier to make the text large. The .foregroundColor(.blue) modifier changes the text color to blue. We added .padding() to give space around the text so it doesn't touch the edges. To center the text both vertically and horizontally, we used .frame(maxWidth: .infinity, maxHeight: .infinity) which expands the view to fill the screen, and the text is centered by default inside this frame. The background is set to white for clarity. This creates a simple, styled text label centered on the screen.
------------------------- | | | | | Hello, SwiftUI! | | | | | -------------------------
Text view do in SwiftUI?Text view is designed to show words or sentences on the screen in SwiftUI.Text view to 24 points?.font(.system(size: value)).Text("Welcome")
.foregroundColor(.red)
.foregroundColor(.blue).foregroundColor overrides previous ones..blue, so the text color will be blue.Text("Hello")
.font(.title)
.padding(10, 20).padding() modifier accepts either no arguments, one argument for uniform padding, or a specific edge set with length..padding(10, 20) is invalid syntax in SwiftUI.Text view that shows "SwiftUI Rocks!" in large bold font, colored green, with padding around it. Which code snippet achieves this correctly?.font(.largeTitle.bold()) chains the bold style properly with the font size..foregroundColor(.green) sets the text color, and .padding() adds default padding around the text..bold() separately which is invalid on Font. Text("SwiftUI Rocks!")
.font(.largeTitle)
.foregroundColor(.green.bold())
.padding() tries to bold the color, which is incorrect. Text("SwiftUI Rocks!")
.font(.bold.largeTitle)
.foregroundColor(.green)
.padding() uses invalid font syntax.Pass the test and this pattern moves unknown