Dynamic Fonts in iOS 18 - #30DaysOfSwift

thevediwho

Vaibhav Dwivedi

Posted on October 27, 2024

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.

πŸ’– πŸ’ͺ πŸ™… 🚩
thevediwho
Vaibhav Dwivedi

Posted on October 27, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related