Bird
0
0

Which of the following is the correct syntax to add a blue background color to a Text view in SwiftUI?

easy📝 Syntax Q3 of 15
iOS Swift - SwiftUI Layout
Which of the following is the correct syntax to add a blue background color to a Text view in SwiftUI?
AText("Hello").background = Color.blue
BText("Hello").overlay(Color.blue)
CText("Hello").background(Color.blue)
DText("Hello").background(Color("blue"))
Step-by-Step Solution
Solution:
  1. Step 1: Check correct modifier usage

    The .background() modifier is called as a function with a view parameter, like Color.blue.
  2. Step 2: Identify syntax errors

    Text("Hello").background = Color.blue uses assignment which is invalid. Text("Hello").background(Color("blue")) uses string initializer incorrectly. Text("Hello").overlay(Color.blue) uses overlay, not background.
  3. Final Answer:

    Text("Hello").background(Color.blue) -> Option C
  4. Quick Check:

    Correct syntax for background = Text("Hello").background(Color.blue) [OK]
Quick Trick: Use .background(Color.colorName) with parentheses [OK]
Common Mistakes:
  • Using assignment instead of modifier call
  • Confusing overlay with background
  • Passing color name as string incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes