Text Views
Part of the Introduction to SwiftUI section of Coddy's Swift journey. Lesson 3 of 50.
Text shows a string. A string literal written inside Text can use Markdown: two asterisks for bold, one for italic:
Text("**Swift** is *fun*")This shows Swift in bold and fun in italic.
To put a value into the text, use string interpolation. Properties of the view are available inside body:
struct ContentView: View {
let name = "Ada"
let points = 42
var body: some View {
Text("\(name) has \(points) points")
}
}The screen shows Ada has 42 points. Markdown only applies to a literal: Text(name) shows the string stored in name exactly as it is.
Challenge
BeginnerThe view has two constants, city and days. Change the text so it shows Trip to Paris: 5 days, using the two constants with string interpolation.
Try it yourself
import SwiftUI
@main
struct CoddyApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Introduction to SwiftUI
Practice on your own: Swift playground