DEV Community

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

Posted on

1

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.

Sentry blog image

The Visual Studio App Center’s retiring

But sadly….you’re not. See how to make the switch to Sentry for all your crash reporting needs.

Read more

Top comments (0)

đź‘‹ Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay