Bird
0
0

How can you apply a red color and underline style to a Text view in SwiftUI in a single chain of modifiers?

hard📝 Application Q9 of 15
iOS Swift - SwiftUI Basics
How can you apply a red color and underline style to a Text view in SwiftUI in a single chain of modifiers?
AText("Hello").underline(color: .red)
BText("Hello").underline().foregroundColor(.red)
CText("Hello").foregroundColor(.red).underline()
DText("Hello").underline(true).foregroundColor(.red)
Step-by-Step Solution
Solution:
  1. Step 1: Understand modifier chaining

    Both .underline() and .foregroundColor() can be chained, but color must be applied before .underline() to ensure the underline uses the red color.
  2. Step 2: Check syntax correctness

    .underline(color:) does not exist; .underline() takes a Bool parameter optionally. Reversing order in B and D results in default underline color.
  3. Final Answer:

    Text("Hello").foregroundColor(.red).underline() -> Option C
  4. Quick Check:

    Use .foregroundColor then .underline() for color and underline = A [OK]
Quick Trick: Chain .foregroundColor() before .underline() for clarity [OK]
Common Mistakes:
  • Using non-existent .underline(color:) method
  • Applying underline before color
  • Passing wrong parameters to underline

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes