Quick Tip Icon
Quick Tip

Animate Text style changes in SwiftUI

With the changes to SwiftUI text modifiers in iOS 16 and macOS 13, it's really easy to animate text style changes.

For example, if we want to toggle font weight between regular and bold, we can use bold() modifier that accepts a Boolean parameter. Then we just change the value from true to false and vice versa, depending on the state.

To animate the change, we can add animation(_:value:) modifier. For the value parameter, we should pass the value of the state variable that alters text font weight.

struct ContentView: View {
    @State private var isBold = false
    
    var body: some View {
        VStack {
            Text("Hello, world!")
                .bold(isBold)
                .animation(.default, value: isBold)
            
            Button("Toggle weight") {
                isBold.toggle()
            }
        }
    }
}
Integrating SwiftUI into UIKit Apps by Natalia Panferova book coverIntegrating SwiftUI into UIKit Apps by Natalia Panferova book cover

Check out our book!

Integrating SwiftUI into UIKit Apps

Integrating SwiftUI intoUIKit Apps

UPDATED FOR iOS 17!

A detailed guide on gradually adopting SwiftUI in UIKit projects.

  • Discover various ways to add SwiftUI views to existing UIKit projects
  • Use Xcode previews when designing and building UI
  • Update your UIKit apps with new features such as Swift Charts and Lock Screen widgets
  • Migrate larger parts of your apps to SwiftUI while reusing views and controllers built in UIKit