DEV Community

Cover image for Dynamic Fonts in iOS 18 - #30DaysOfSwift
Vaibhav Dwivedi
Vaibhav Dwivedi

Posted on

Dynamic Fonts in iOS 18 - #30DaysOfSwift

Day 17: Dynamic Font Scaling for Accessibility πŸ”€

Today, let me share a simple tip on how to implement Dynamic Font Scaling in your SwiftUI app to enhance accessibility.

Image description

Why Use Dynamic Font Scaling?

  • Accessibility: Supports users with visual impairments by allowing them to adjust text sizes according to their preferences.
  • User Experience: Creates a more comfortable reading experience, improving usability for all users.
  • Consistency: Maintains a cohesive design as text scales across different devices and settings.

Code Example: Implementing Dynamic Font Scaling

import SwiftUI

struct ContentView: View {
    var body: some View {
        VStack(spacing: 20) {
            Text("Welcome to My App!")
                .font(.largeTitle) // Large title using dynamic type
                .padding()

            Text("This text will scale based on user settings.")
                .font(.body) // Body text using dynamic type
                .padding()

            Text("Adjust the text size in Settings > Accessibility > Display & Text Size.")
                .font(.caption) // Caption text using dynamic type
                .padding()
        }
        .padding()
        .multilineTextAlignment(.center) // Center align the text for a better layout
    }
}

struct DynamicFontScalingApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView() // Main content view
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

By supporting users’ preferences for text size, you create a more inclusive environment for all.

Happy Coding!

P.S. The full series is available on my profile and the components can also be found at shipios.app/components.

Top comments (0)