Skip to content

Instantly share code, notes, and snippets.

@mac-cain13
Created July 9, 2020 07:19
Show Gist options
  • Save mac-cain13/d495b998cba4fda5109a6bf32b4e4606 to your computer and use it in GitHub Desktop.
Save mac-cain13/d495b998cba4fda5109a6bf32b4e4606 to your computer and use it in GitHub Desktop.
SwiftUI - AccessibleHStack

AccessibleHStack

The more accessible HStack for SwiftUI.

What is it?

HStack that will switch to a VStack when the horizontal size class is compact and the content size category is set to an accessibility size option.

Why do I need it?

Often when presenting a few items in an HStack you will run out of size when the users starts to scale the font really big and the screen is quite compact. Causing the Text elements to wrap or worse truncate text.

This stack will automatically swap to a VStack under those conditions. That will affect your design, but at those accessibility font sizes it's more important to be readable than to be pixel perfect on design.

Am I free to use this?

Yes, I've put this in the public domain!

//
// AccessibleHStack.swift
//
// Created by Mathijs Kadijk on 08/07/2020.
//
import SwiftUI
/// HStack that will switch to a VStack when the horizontal size class is compact and the content size category is set to an accessibility size option
struct AccessibleHStack<Content>: View where Content: View {
@Environment(\.sizeCategory) private var sizeCategory
@Environment(\.horizontalSizeClass) private var horizontalSizeClass
let verticalAlignment: VerticalAlignment
let horizontalAlignment: HorizontalAlignment
let spacing: CGFloat?
let content: Content
var body: some View {
if case .compact? = horizontalSizeClass, sizeCategory.isAccessibilityCategory {
VStack(alignment: horizontalAlignment, spacing: spacing) { content }
} else {
HStack(alignment: verticalAlignment, spacing: spacing) { content }
}
}
@inlinable
init(verticalAlignment: VerticalAlignment = .center, horizontalAlignment: HorizontalAlignment = .center, spacing: CGFloat? = nil, @ViewBuilder content: () -> Content) {
self.verticalAlignment = verticalAlignment
self.horizontalAlignment = horizontalAlignment
self.spacing = spacing
self.content = content()
}
}
struct AccessibleHStack_Previews: PreviewProvider {
static var previews: some View {
VStack {
ForEach(ContentSizeCategory.allCases, id: \.self) { sizeCategory in
AccessibleHStack {
Text("A")
Text("B")
Text("C")
}
.environment(\.sizeCategory, sizeCategory)
Divider()
}
}
.previewLayout(.sizeThatFits)
}
}
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <https://unlicense.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment