Search code examples
swiftuiios14

How to add character spacing in SwiftUI after applying fonts and color style?


I want to add character spacing in the text of SwiftUI in iOS version iOS 14 and plus.

When I use a new modifier called as kerning and Tracking after applying fonts styling then it won't work. So any pointer will be appreciated.


Solution

  • kerning and tracking are available in earlier versions of iOS, but only if they're applied directly to Text views.

    What's new in iOS 16 is the ability to apply the modifier to any view, and have it apply to any of its Text child views.

    Note the availability flags for Text.tracking vs View.tracking:

    So that means that this should work for you in iOS 14:

    Text("My Sample Text")
      .tracking(-0.1)
    

    but this would not:

    VStack {
      Text("My Sample View")
      Text("With two lines of text")
    }
    .tracking(-0.1)