Skip to content

Instantly share code, notes, and snippets.

@robinkanatzar
Last active August 4, 2024 07:37

Revisions

  1. robinkanatzar revised this gist Aug 4, 2024. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions AStack.swift
    Original file line number Diff line number Diff line change
    @@ -18,6 +18,8 @@ import SwiftUI
    /// }
    /// }
    /// ```
    ///
    /// Article about this gist: https://robinkanatzar.blog/accessibile-stack-in-swiftui-af8630a1cd1d
    struct AStack<Content>: View where Content: View {
    @Environment(\.dynamicTypeSize) var dynamicTypeSize
    let content: Content
  2. robinkanatzar revised this gist Aug 4, 2024. 1 changed file with 19 additions and 5 deletions.
    24 changes: 19 additions & 5 deletions AStack.swift
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,13 @@
    import SwiftUI

    /// An accessible stack view that starts as an HStack
    /// then becomes a VStack once the dynamic type size
    /// is an accessibility size.
    ///
    /// Usage in a View:
    /// ```
    /// var body: some View {
    /// AStack {
    /// AStack(hStackAlignment: .top, hStackSpacing: 100, vStackAlignment: .leading, vStackSpacing: 5) {
    /// Text("Hello, world!")
    /// Text("Hello, world!")
    /// Text("Hello, world!")
    @@ -19,16 +21,28 @@
    struct AStack<Content>: View where Content: View {
    @Environment(\.dynamicTypeSize) var dynamicTypeSize
    let content: Content
    let hStackAlignment: VerticalAlignment
    let hStackSpacing: CGFloat?
    let vStackAlignment: HorizontalAlignment
    let vStackSpacing: CGFloat?

    init(@ViewBuilder content: () -> Content) {
    self.content = content()
    init(hStackAlignment: VerticalAlignment = .center,
    hStackSpacing: CGFloat? = nil,
    vStackAlignment: HorizontalAlignment = .center,
    vStackSpacing: CGFloat? = nil,
    @ViewBuilder content: () -> Content) {
    self.hStackAlignment = hStackAlignment
    self.hStackSpacing = hStackSpacing
    self.vStackAlignment = vStackAlignment
    self.vStackSpacing = vStackSpacing
    self.content = content()
    }

    var body: some View {
    if dynamicTypeSize.isAccessibilitySize {
    VStack { content }
    VStack(alignment: vStackAlignment, spacing: vStackSpacing) { content }
    } else {
    HStack { content }
    HStack(alignment: hStackAlignment, spacing: hStackSpacing) { content }
    }
    }
    }
  3. robinkanatzar revised this gist Aug 4, 2024. 1 changed file with 15 additions and 0 deletions.
    15 changes: 15 additions & 0 deletions AStack.swift
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,21 @@
    /// An accessible stack view that starts as an HStack
    /// then becomes a VStack once the dynamic type size
    /// is an accessibility size.
    ///
    /// Usage in a View:
    /// ```
    /// var body: some View {
    /// AStack {
    /// Text("Hello, world!")
    /// Text("Hello, world!")
    /// Text("Hello, world!")
    /// Text("Hello, world!")
    /// Text("Hello, world!")
    /// Text("Hello, world!")
    /// Text("Hello, world!")
    /// }
    /// }
    /// ```
    struct AStack<Content>: View where Content: View {
    @Environment(\.dynamicTypeSize) var dynamicTypeSize
    let content: Content
  4. robinkanatzar created this gist Aug 4, 2024.
    19 changes: 19 additions & 0 deletions AStack.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    /// An accessible stack view that starts as an HStack
    /// then becomes a VStack once the dynamic type size
    /// is an accessibility size.
    struct AStack<Content>: View where Content: View {
    @Environment(\.dynamicTypeSize) var dynamicTypeSize
    let content: Content

    init(@ViewBuilder content: () -> Content) {
    self.content = content()
    }

    var body: some View {
    if dynamicTypeSize.isAccessibilitySize {
    VStack { content }
    } else {
    HStack { content }
    }
    }
    }