Trikalabs
  • Home
  • Best online TDD videos
  • Book Suggestions
  • About Me
  • Contact
Trikalabs
No Result
View All Result

Accessibility – AutoResizable views

by fragi
February 18, 2022
in Accessibility
Share on FacebookShare on Twitter

Allow a user to select the text size is a must if your app wants to be accessible. IOS already make our life easier by handling most of cases nicely.
But there are cases where two design elements compete for the available space.
Let’s look on the following example:

Swift
1
2
3
4
5
6
7
8
9
10
11
struct ContentView: View {
    var body: some View {
        HStack {
            Text("Hello, world!")
            Spacer()
            Text("Today is a sunny day")
        }
        .padding()
 
    }
}

Which in default text size draws as follows:

Let’s now increase the text size:

Swift
1
2
3
4
5
6
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
            .dynamicTypeSize(.accessibility5)
    }
}

We notice that now the reading of the texts are not so clear.

 

Let’s try now to solve this issue by creating a view that can adapt to size category changes:

Swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct SizeCategoryAdaptiveStack: ViewModifier {
    @Environment(\.sizeCategory) var sizeCategory
    
    @ViewBuilder
    func body(content: Content) -> some View {
        if sizeCategory.isAccessibilityCategory {
            VStack {
                content
            }
        } else {
            HStack {
                content
            }
        }
    }
}

And update the view to use it:

Swift
1
2
3
4
5
6
7
8
9
10
11
struct ContentView: View {
    var body: some View {
         Group {
            Text("Hello, world!")
            Text("Today is a sunny day")
        }
        .padding()
        .modifier(SizeCategoryAdaptiveStack())
 
    }
}

Which produces the following desirable UI:

I used to use the Stack in UIKit to change the axis from horizontal to vertical to support this case, but for SwiftUI I got inspired by the following resourse.

Resources:
https://swiftwithmajid.com/2019/10/09/dynamic-type-in-swiftui/
https://www.fivestars.blog/articles/adaptive-swiftui-views/

fragi

fragi

Related Posts

Accessibility by Example
Accessibility

Accessibility by Example

January 28, 2022

This post contains notes and screenshots from WWDC accessibility videos. Accessibility in SwiftUI (WWDC 2019) - Add label to describe...

Large Content Viewer
Accessibility

SwiftUI – Accessibility for Images

May 13, 2021

Supporting dynamic type for text is essential to make the app accessible. Making images accessible sometimes is out of the...

Large Content Viewer
Accessibility

Large Content Viewer

May 13, 2021

Although scaling dynamic type is preferred whenever possible, there are cases as the navigation bars, the tab bars, the toolbars...

Next Post
Project Mess #2 – Magic numbers for UI spacing

Project Mess #2 - Magic numbers for UI spacing

Project Mess #3: Hardcoded UI colours

Project Mess #3: Hardcoded UI colours

Extract build settings – Xcode

Extract build settings - Xcode

  • Advertise
  • Privacy & Policy
  • Contact

© 2019 Trikalabs Ltd. All rights reserved.

No Result
View All Result
  • Home
  • About Me
  • A curated list with the best free online TDD videos
  • Book Suggestions
  • Pinner Code Club

© 2019 Trikalabs Ltd. All rights reserved.