Skip to content

Commit

Permalink
Support block-based accessibility properties
Browse files Browse the repository at this point in the history
These block-based variants of the UIAccessibility properties were introduced in iOS 17. Resolves #192.
  • Loading branch information
NickEntin committed Jan 24, 2024
1 parent 1b2f58a commit 5c3071f
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//
// Copyright 2024 Block Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import Foundation

extension NSObject {

var effectiveAccessibilityLabel: String? {
#if swift(>=5.9)
if #available(iOS 17.0, *) {
return accessibilityLabelBlock?() ?? accessibilityLabel
} else {
return accessibilityLabel
}
#else
return accessibilityLabel
#endif
}

var effectiveAccessibilityValue: String? {
#if swift(>=5.9)
if #available(iOS 17.0, *) {
return accessibilityValueBlock?() ?? accessibilityValue
} else {
return accessibilityValue
}
#else
return accessibilityValue
#endif
}

var effectiveAccessibilityHint: String? {
#if swift(>=5.9)
if #available(iOS 17.0, *) {
return accessibilityHintBlock?() ?? accessibilityHint
} else {
return accessibilityHint
}
#else
return accessibilityHint
#endif
}

var effectiveAccessibilityTraits: UIAccessibilityTraits? {
#if swift(>=5.9)
if #available(iOS 17.0, *) {
return accessibilityTraitsBlock?() ?? accessibilityTraits
} else {
return accessibilityTraits
}
#else
return accessibilityTraits
#endif
}

var effectiveAccessibilityLanguage: String? {
#if swift(>=5.9)
if #available(iOS 17.0, *) {
return accessibilityLanguageBlock?() ?? accessibilityLanguage
} else {
return accessibilityLanguage
}
#else
return accessibilityLanguage
#endif
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ extension NSObject {

/// Returns a tuple consisting of the `description` and (optionally) a `hint` that VoiceOver will read for the object.
func accessibilityDescription(context: AccessibilityHierarchyParser.Context?) -> (description: String, hint: String?) {
var accessibilityDescription = accessibilityLabelOverride(for: context) ?? accessibilityLabel ?? ""
var accessibilityDescription = accessibilityLabelOverride(for: context) ?? effectiveAccessibilityLabel ?? ""

var hintDescription = accessibilityHint?.nonEmpty()
var hintDescription = effectiveAccessibilityHint?.nonEmpty()

let strings = Strings(locale: accessibilityLanguage)

Expand All @@ -36,7 +36,7 @@ extension NSObject {
switch context {
case let .dataTableCell(row: row, column: column, width: width, height: height, isFirstInRow: isFirstInRow, rowHeaders: rowHeaders, columnHeaders: columnHeaders):
let headersDescription = (rowHeaders + columnHeaders).map { header -> String in
switch (header.accessibilityLabel?.nonEmpty(), header.accessibilityValue?.nonEmpty()) {
switch (header.effectiveAccessibilityLabel?.nonEmpty(), header.effectiveAccessibilityValue?.nonEmpty()) {
case (nil, nil):
return ""
case let (.some(label), nil):
Expand Down Expand Up @@ -74,7 +74,7 @@ extension NSObject {
descriptionContainsContext = false
}

if let accessibilityValue = accessibilityValue?.nonEmpty(), !hidesAccessibilityValue(for: context) {
if let accessibilityValue = effectiveAccessibilityValue?.nonEmpty(), !hidesAccessibilityValue(for: context) {
if let existingDescription = accessibilityDescription.nonEmpty() {
if descriptionContainsContext {
accessibilityDescription += " \(accessibilityValue)"
Expand Down Expand Up @@ -114,7 +114,7 @@ extension NSObject {
traitSpecifiers.append(strings.switchButtonTraitName)
}

switch accessibilityValue {
switch effectiveAccessibilityValue {
case "1":
traitSpecifiers.append(strings.switchButtonOnStateName)
case "0":
Expand Down Expand Up @@ -229,7 +229,7 @@ extension NSObject {
}
}

let hasHintOnly = (accessibilityHint?.nonEmpty() != nil) && (accessibilityLabel?.nonEmpty() == nil) && (accessibilityValue?.nonEmpty() == nil)
let hasHintOnly = (effectiveAccessibilityHint?.nonEmpty() != nil) && (effectiveAccessibilityLabel?.nonEmpty() == nil) && (effectiveAccessibilityValue?.nonEmpty() == nil)
let hidesAdjustableHint = accessibilityTraits.contains(.notEnabled) || accessibilityTraits.contains(.switchButton) || hasHintOnly
if accessibilityTraits.contains(.adjustable) && !hidesAdjustableHint {
if let existingHintDescription = hintDescription?.nonEmpty()?.strippingTrailingPeriod() {
Expand Down Expand Up @@ -276,8 +276,6 @@ extension NSObject {
}
}

// MARK: - Private Static Properties

// MARK: - Private

private struct Strings {
Expand Down

0 comments on commit 5c3071f

Please sign in to comment.