SwiftUI .tracking()

Christine_Yang's avatar
Published Feb 26, 2023Updated Mar 2, 2023
Contribute to Docs

The .tracking() modifier method adjusts the spacing of the characters in a Text view.

  • Learn how to build iOS applications with Swift and SwiftUI and publish them to Apples' App Store.
    • Includes 7 Courses
    • With Certificate
    • Beginner Friendly.
      13 hours
  • A powerful programming language developed by Apple for iOS, macOS, and more.
    • Beginner Friendly.
      12 hours

Syntax

Text("Some text")
    .tracking(x)

The .tracking() modifier method takes one parameter, x, entered as an integer to define the space to be added to or deducted from each character’s default spacing in pt (points). The value of x can create the following trackings:

  • 0 or not specifying a value results in the system’s default spacing of each character,
  • A positive value applies additional spacing,
  • A negative value decreases spacing.

Note: .tracking() is not the same as .kerning(). Although both the .tracking() and .kerning() modifier methods adjust the spacing of the characters, they behave differently. .tracking() will pull apart the ligatures and does not leave trailing whitespace, while .kerning() will maintain ligatures and leaves some trailing whitespace.

Example

In the example below, the .tracking() modifier method is called on three Text views with decreased, default, and increased tracking values.

var body: some View {
Text("Tracking.")
.tracking(-5)
Text("Tracking.")
.tracking()
Text("Tracking.")
.tracking(5)
}

This will display the following:

SwiftUI ViewModifier Tracking

All contributors

Contribute to Docs

Learn SwiftUI on Codecademy

  • Learn how to build iOS applications with Swift and SwiftUI and publish them to Apples' App Store.
    • Includes 7 Courses
    • With Certificate
    • Beginner Friendly.
      13 hours
  • A powerful programming language developed by Apple for iOS, macOS, and more.
    • Beginner Friendly.
      12 hours