0
0
iOS Swiftmobile~5 mins

Text view and modifiers in iOS Swift

Choose your learning style9 modes available
Introduction

Text views show words on the screen. Modifiers change how the text looks or behaves.

To display a title or label in your app.
To show instructions or messages to the user.
To style text with colors, fonts, or alignment.
To make text bold, italic, or underlined.
To add padding or background behind text.
Syntax
iOS Swift
Text("Your text here")
  .modifier1()
  .modifier2()

Use Text to create a text view.

Chain modifiers after Text to change its style or layout.

Examples
Simple text showing "Hello, world!" on screen.
iOS Swift
Text("Hello, world!")
Text with a large title font and blue color.
iOS Swift
Text("Welcome")
  .font(.title)
  .foregroundColor(.blue)
Text that is bold and italic.
iOS Swift
Text("SwiftUI")
  .bold()
  .italic()
Text with space around it and a yellow background.
iOS Swift
Text("Padded Text")
  .padding()
  .background(Color.yellow)
Sample App

This app shows three text views stacked vertically. Each text uses different modifiers to change font, color, style, and background.

iOS Swift
import SwiftUI

struct ContentView: View {
  var body: some View {
    VStack(spacing: 20) {
      Text("Hello, SwiftUI!")
        .font(.largeTitle)
        .foregroundColor(.purple)
      Text("This is a simple text view.")
        .font(.body)
        .italic()
      Text("Styled Text")
        .bold()
        .padding()
        .background(Color.green.opacity(0.3))
        .cornerRadius(8)
    }
    .padding()
  }
}

@main
struct MyApp: App {
  var body: some Scene {
    WindowGroup {
      ContentView()
    }
  }
}
OutputSuccess
Important Notes

Modifiers do not change the original text but create a new styled view.

Order of modifiers can affect the final look.

Use .padding() to add space around text for better touch targets.

Summary

Text shows words on screen.

Modifiers like .font(), .foregroundColor(), and .padding() change text style and layout.

Chain modifiers to combine effects.