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.

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

πŸ‘‹ Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay