Skip to content

Commit

Permalink
Easy accessibility configuration for views was added
Browse files Browse the repository at this point in the history
AccessibilitySetting
  • Loading branch information
ivkosh committed Dec 16, 2022
1 parent f0d3266 commit 2d9d1ef
Show file tree
Hide file tree
Showing 9 changed files with 195 additions and 15 deletions.
44 changes: 44 additions & 0 deletions Sources/CoreUI/UI/UI+Accessibility.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import UIKit

public extension UI {
enum AccessibilitySetting {
case isElement(Bool)
case identifier(String)
case label(String)
case hint(String)
case value(String)
case traits(UIAccessibilityTraits)
case containerType(UIAccessibilityContainerType)
case elements([Any])
case elementsHidden(Bool)
case shouldGroupChildren(Bool)
}
}

internal extension UI.AccessibilitySetting {
func apply(to view: UIView) {
switch self {
case .isElement(let isElement):
view.isAccessibilityElement = isElement
case .identifier(let identifier):
view.accessibilityIdentifier = identifier
case .label(let label):
view.accessibilityLabel = label
case .hint(let hint):
view.accessibilityHint = hint
case .value(let value):
view.accessibilityValue = value
case .traits(let traits):
view.accessibilityTraits = traits
case .containerType(let type):
view.accessibilityContainerType = type
case .elementsHidden(let isHidden):
view.accessibilityElementsHidden = isHidden
case .elements(let elements):
view.accessibilityElements = elements
case .shouldGroupChildren(let shouldGroup):
view.shouldGroupAccessibilityChildren = shouldGroup
}
}
}

24 changes: 14 additions & 10 deletions Sources/CoreUI/UI/UI+Button.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public extension UI {
isSelected: Bool = false,
width: CGFloat? = nil,
height: CGFloat? = nil,
accessibility additionalAccessibilitySettings: [AccessibilitySetting] = [],
apply: ((UIButton) -> Void)? = nil
) -> UIButton {
let button = View(backgroundColor: backgroundColor, width: width, height: height) as UIButton
Expand All @@ -26,6 +27,7 @@ public extension UI {

if let title = title {
button.setTitle(title, for: .normal)
button.accessibility(.label(title))
}

if let image = image {
Expand All @@ -45,7 +47,10 @@ public extension UI {
}

button.isSelected = isSelected


button.accessibility(.isElement(true), .shouldGroupChildren(true), .traits(.button))
button.accessibility(additionalAccessibilitySettings)

apply?(button)

return button
Expand All @@ -59,12 +64,13 @@ public extension UI {
isSelected: Bool = false,
width: CGFloat? = nil,
height: CGFloat? = nil,
accessibility additionalAccessibilitySettings: [AccessibilitySetting] = [],
apply: ((UIButton) -> Void)? = nil
) -> UIButton {
let button = View(backgroundColor: backgroundColor, width: width, height: height) as UIButton

button.setAttributedTitle(attributedTitle, for: .normal)

if let image = image {
button.setImage(image, for: .normal)
}
Expand All @@ -79,16 +85,14 @@ public extension UI {

button.isSelected = isSelected

apply?(button)

button.isAccessibilityElement = true
button.shouldGroupAccessibilityChildren = true
button.accessibilityTraits = .button
button.accessibilityLabel = attributedTitle.string

button.titleLabel?.adjustsFontForContentSizeCategory = true
button.adjustsImageSizeForAccessibilityContentSizeCategory = true


button.accessibility(.isElement(true), .shouldGroupChildren(true), .traits(.button), .label(attributedTitle.string))
button.accessibility(additionalAccessibilitySettings)

apply?(button)

return button
}

Expand Down
3 changes: 3 additions & 0 deletions Sources/CoreUI/UI/UI+ImageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public extension UI {
cornerRadius: CGFloat? = nil,
contentMode: UIView.ContentMode = .scaleAspectFill,
clipsToBounds: Bool = true,
accessibility accessibilitySettings: [AccessibilitySetting] = [],
apply: ((UIImageView) -> Void)? = nil
) -> UIImageView {
let imageView = UIImageView()
Expand Down Expand Up @@ -53,6 +54,8 @@ public extension UI {
imageView.layer.cornerRadius = cornerRadius
}

imageView.accessibility(accessibilitySettings)

var constraints: [NSLayoutConstraint] = []

if let width = width {
Expand Down
6 changes: 6 additions & 0 deletions Sources/CoreUI/UI/UI+Label.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public extension UI {
lineBreakMode: NSLineBreakMode = .byTruncatingTail,
minWidth: CGFloat? = nil,
minHeight: CGFloat? = nil,
accessibility accessibilitySettings: [AccessibilitySetting] = [],
apply: ((UILabel) -> Void)? = nil
) -> UILabel {
let label = UILabel()
Expand All @@ -26,6 +27,8 @@ public extension UI {
label.text = text
}

label.accessibility(accessibilitySettings)

var constraints: [NSLayoutConstraint] = []

if let width = minWidth {
Expand All @@ -50,6 +53,7 @@ public extension UI {
lineBreakMode: NSLineBreakMode = .byTruncatingTail,
minWidth: CGFloat? = nil,
minHeight: CGFloat? = nil,
accessibility accessibilitySettings: [AccessibilitySetting] = [],
apply: ((UILabel) -> Void)? = nil
) -> UILabel {
let label = UILabel()
Expand All @@ -60,6 +64,8 @@ public extension UI {
label.adjustsFontForContentSizeCategory = true

label.attributedText = attributedText

label.accessibility(accessibilitySettings)

var constraints: [NSLayoutConstraint] = []

Expand Down
13 changes: 12 additions & 1 deletion Sources/CoreUI/UI/UI+Stack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import UIKit
// swiftlint:disable identifier_name type_body_length
// swiftlint:disable:next type_name
public extension UI {

private static func Stack(axis: NSLayoutConstraint.Axis,
spacing: CGFloat,
distribution: UIStackView.Distribution,
alignment: UIStackView.Alignment,
backgroundColor: UIColor?,
isInteractionEnabled: Bool?,
accessibility accessibilitySettings: [AccessibilitySetting],
_ subviews: [UIView?] = [],
apply: ((UIStackView) -> Void)? = nil
) -> UIStackView {
Expand All @@ -25,6 +26,8 @@ public extension UI {
stack.isUserInteractionEnabled = isInteractionEnabled
}

stack.accessibility(accessibilitySettings)

if #available(iOS 14.0, *) {
if let backgroundColor = backgroundColor {
stack.backgroundColor = backgroundColor
Expand All @@ -42,6 +45,7 @@ public extension UI {
alignment: UIStackView.Alignment = .fill,
backgroundColor: UIColor? = nil,
isInteractionEnabled: Bool? = nil,
accessibility: [AccessibilitySetting] = [],
_ subviews: [UIView?] = [],
apply: ((UIStackView) -> Void)? = nil
) -> UIStackView {
Expand All @@ -51,6 +55,7 @@ public extension UI {
alignment: alignment,
backgroundColor: backgroundColor,
isInteractionEnabled: isInteractionEnabled,
accessibility: accessibility,
subviews,
apply: apply
)
Expand All @@ -62,6 +67,7 @@ public extension UI {
alignment: UIStackView.Alignment = .fill,
backgroundColor: UIColor? = nil,
isInteractionEnabled: Bool? = nil,
accessibility: [AccessibilitySetting] = [],
_ subviews: UIView?...,
apply: ((UIStackView) -> Void)? = nil
) -> UIStackView {
Expand All @@ -70,6 +76,7 @@ public extension UI {
alignment: alignment,
backgroundColor: backgroundColor,
isInteractionEnabled: isInteractionEnabled,
accessibility: accessibility,
subviews,
apply: apply
)
Expand All @@ -81,6 +88,7 @@ public extension UI {
alignment: UIStackView.Alignment = .fill,
backgroundColor: UIColor? = nil,
isInteractionEnabled: Bool? = nil,
accessibility: [AccessibilitySetting] = [],
_ subviews: [UIView?] = [],
apply: ((UIStackView) -> Void)? = nil
) -> UIStackView {
Expand All @@ -90,6 +98,7 @@ public extension UI {
alignment: alignment,
backgroundColor: backgroundColor,
isInteractionEnabled: isInteractionEnabled,
accessibility: accessibility,
subviews,
apply: apply
)
Expand All @@ -101,6 +110,7 @@ public extension UI {
alignment: UIStackView.Alignment = .fill,
backgroundColor: UIColor? = nil,
isInteractionEnabled: Bool? = nil,
accessibility: [AccessibilitySetting] = [],
_ subviews: UIView?...,
apply: ((UIStackView) -> Void)? = nil
) -> UIStackView {
Expand All @@ -109,6 +119,7 @@ public extension UI {
alignment: alignment,
backgroundColor: backgroundColor,
isInteractionEnabled: isInteractionEnabled,
accessibility: accessibility,
subviews,
apply: apply
)
Expand Down
3 changes: 3 additions & 0 deletions Sources/CoreUI/UI/UI+TextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public extension UI {
font: UIFont? = nil,
textColor: UIColor? = nil,
linkTextAttributes: [NSAttributedString.Key: Any]? = nil,
accessibility accessibilitySettings: [AccessibilitySetting] = [],
apply: ((UITextView) -> Void)? = nil
) -> UITextView {
let textView = UITextView()
Expand Down Expand Up @@ -42,6 +43,8 @@ public extension UI {
if let textColor = textColor {
textView.textColor = textColor
}

textView.accessibility(accessibilitySettings)

apply?(textView)

Expand Down
40 changes: 36 additions & 4 deletions Sources/CoreUI/UI/UI+View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public extension UI {
minWidth: CGFloat? = nil,
minHeight: CGFloat? = nil,
cornerRadius: CGFloat? = nil,
accessibility accessibilitySettings: [AccessibilitySetting] = [],
_ subviews: [UIView?],
apply: ((ViewType) -> Void)? = nil
) -> ViewType {
Expand All @@ -29,6 +30,8 @@ public extension UI {
view.addSubview(subview)
}

view.accessibility(accessibilitySettings)

var constraints: [NSLayoutConstraint] = []

if let width = width {
Expand Down Expand Up @@ -61,6 +64,7 @@ public extension UI {
minWidth: CGFloat? = nil,
minHeight: CGFloat? = nil,
cornerRadius: CGFloat? = nil,
accessibility accessibilitySettings: [AccessibilitySetting] = [],
_ subviews: UIView?...,
apply: ((ViewType) -> Void)? = nil
) -> ViewType {
Expand All @@ -70,6 +74,7 @@ public extension UI {
minWidth: minWidth,
minHeight: minHeight,
cornerRadius: cornerRadius,
accessibility: accessibilitySettings,
subviews,
apply: apply)
}
Expand All @@ -82,23 +87,38 @@ public extension UI {
height: CGFloat? = nil,
minHeight: CGFloat? = nil,
cornerRadius: CGFloat? = nil,
accessibility accessibilitySettings: [AccessibilitySetting] = [],
_ subview: UIView,
_ anchors: LayoutAnchor...,
apply: ((ViewType) -> Void)? = nil
) -> ViewType {
View(backgroundColor: backgroundColor, width: width, height: height, minHeight: minHeight, cornerRadius: cornerRadius, subview, anchors, apply: apply)
View(backgroundColor: backgroundColor,
width: width,
height: height,
minHeight: minHeight,
cornerRadius: cornerRadius,
accessibility: accessibilitySettings,
subview,
anchors,
apply: apply)
}

static func View<ViewType: UIView>(backgroundColor: UIColor? = nil,
width: CGFloat? = nil,
height: CGFloat? = nil,
minHeight: CGFloat? = nil,
cornerRadius: CGFloat? = nil,
accessibility accessibilitySettings: [AccessibilitySetting] = [],
_ subview: UIView,
_ anchors: [LayoutAnchor],
apply: ((ViewType) -> Void)? = nil
) -> ViewType {
let view = View(backgroundColor: backgroundColor, width: width, height: height, minHeight: minHeight, cornerRadius: cornerRadius) as ViewType
let view = View(backgroundColor: backgroundColor,
width: width,
height: height,
minHeight: minHeight,
cornerRadius: cornerRadius,
accessibility: accessibilitySettings) as ViewType
view.addSubview(subview, anchors)
apply?(view)
return view
Expand All @@ -115,19 +135,31 @@ public extension UI {
static func Control(backgroundColor: UIColor? = nil,
width: CGFloat? = nil,
height: CGFloat? = nil,
accessibility accessibilitySettings: [AccessibilitySetting] = [],
_ subviews: [UIView],
apply: ((UIControl) -> Void)? = nil
) -> UIControl {
View(backgroundColor: backgroundColor, width: width, height: height, subviews, apply: apply)
View(backgroundColor: backgroundColor,
width: width,
height: height,
accessibility: accessibilitySettings,
subviews,
apply: apply)
}

static func Control(backgroundColor: UIColor? = nil,
width: CGFloat? = nil,
height: CGFloat? = nil,
accessibility accessibilitySettings: [AccessibilitySetting] = [],
_ subviews: UIView...,
apply: ((UIControl) -> Void)? = nil
) -> UIControl {
View(backgroundColor: backgroundColor, width: width, height: height, subviews, apply: apply)
View(backgroundColor: backgroundColor,
width: width,
height: height,
accessibility: accessibilitySettings,
subviews,
apply: apply)
}

}
Expand Down
13 changes: 13 additions & 0 deletions Sources/CoreUI/UI/UIView+accessibility.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import UIKit

public extension UIView {
func accessibility(_ settings: [UI.AccessibilitySetting]) {
for setting in settings {
setting.apply(to: self)
}
}

func accessibility(_ settings: UI.AccessibilitySetting...) {
accessibility(settings)
}
}
Loading

0 comments on commit 2d9d1ef

Please sign in to comment.