Skip to content

Commit 6df94cd

Browse files
Back button and toggle button fixes (#237)
* Fixed a bug where an element's accessibility value could be omitted if it contained the .toggleButton trait but was not 0, 1 or 2. * Fixed a bug where elements with both a .backButton trait and an accessibilityLabel equal to "back" weren't being deduplicated properly.
1 parent 0702083 commit 6df94cd

File tree

9 files changed

+36
-8
lines changed

9 files changed

+36
-8
lines changed

Example/Podfile.lock

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
PODS:
2-
- AccessibilitySnapshot/Core (0.7.0)
3-
- AccessibilitySnapshot/iOSSnapshotTestCase (0.7.0):
2+
- AccessibilitySnapshot/Core (0.8.0)
3+
- AccessibilitySnapshot/iOSSnapshotTestCase (0.8.0):
44
- AccessibilitySnapshot/Core
55
- iOSSnapshotTestCase (~> 8.0)
6-
- AccessibilitySnapshot/SnapshotTesting (0.7.0):
6+
- AccessibilitySnapshot/SnapshotTesting (0.8.0):
77
- AccessibilitySnapshot/Core
88
- SnapshotTesting (~> 1.0)
99
- iOSSnapshotTestCase (8.0.0):
@@ -32,7 +32,7 @@ EXTERNAL SOURCES:
3232
:path: "../AccessibilitySnapshot.podspec"
3333

3434
SPEC CHECKSUMS:
35-
AccessibilitySnapshot: a29652b9054223b6c2a19ef1c83631e2a62ecbf9
35+
AccessibilitySnapshot: 9c1129f4d7653affc5f89f3086286bd50c11d5d4
3636
iOSSnapshotTestCase: a670511f9ee3829c2b9c23e6e68f315fd7b6790f
3737
Paralayout: 844978af530a061a31d849c7b554510190b9cddf
3838
SnapshotTesting: 8caa6661fea7c8019d5b46de77c16bab99c36c5c

Sources/AccessibilitySnapshot/Core/Swift/Assets/de.lproj/Localizable.strings

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
/* Description for the 'back button' accessibility trait */
1414
"trait.backbutton.description" = "Zurück Taste.";
1515

16+
/* Descriptor for the 'back' portion of the 'back button' accessibility trait */
17+
"back.descriptor" = "Zurück";
18+
1619
/* Description for the 'tab' accessibility trait */
1720
"trait.tab.description" = "Tabulator.";
1821

Sources/AccessibilitySnapshot/Core/Swift/Assets/en.lproj/Localizable.strings

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
/* Description for the 'back button' accessibility trait */
1414
"trait.backbutton.description" = "Back Button.";
1515

16+
/* Descriptor for the 'back' portion of the 'back button' accessibility trait */
17+
"back.descriptor" = "Back";
18+
1619
/* Description for the 'tab' accessibility trait */
1720
"trait.tab.description" = "Tab.";
1821

Sources/AccessibilitySnapshot/Core/Swift/Assets/ru.lproj/Localizable.strings

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
/* Description for the 'back button' accessibility trait */
1414
"trait.backbutton.description" = "Кнопка назад.";
1515

16+
/* Descriptor for the 'back' portion of the 'back button' accessibility trait */
17+
"back.descriptor" = "назад";
18+
1619
/* Description for the 'tab' accessibility trait */
1720
"trait.tab.description" = "Вкладка.";
1821

Sources/AccessibilitySnapshot/Core/Swift/Classes/UIAccessibility+SnapshotAdditions.swift

+23-4
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,15 @@ extension NSObject {
2020

2121
/// Returns a tuple consisting of the `description` and (optionally) a `hint` that VoiceOver will read for the object.
2222
func accessibilityDescription(context: AccessibilityHierarchyParser.Context?) -> (description: String, hint: String?) {
23-
var accessibilityDescription = accessibilityLabelOverride(for: context) ?? accessibilityLabel ?? ""
23+
let strings = Strings(locale: accessibilityLanguage)
24+
25+
var accessibilityDescription =
26+
accessibilityLabelOverride(for: context) ??
27+
(hidesAccessibilityLabel(backDescriptor: strings.backDescriptor) ? "" :
28+
accessibilityLabel ?? "" )
2429

2530
var hintDescription = accessibilityHint?.nonEmpty()
2631

27-
let strings = Strings(locale: accessibilityLanguage)
2832

2933
let numberFormatter = NumberFormatter()
3034
if let localeIdentifier = accessibilityLanguage {
@@ -126,8 +130,9 @@ extension NSObject {
126130
case "2":
127131
traitSpecifiers.append(strings.switchButtonMixedStateName)
128132
default:
129-
// When the switch button trait is set, unknown accessibility values are omitted from the description.
130-
break
133+
if let accessibilityValue {
134+
traitSpecifiers.append(accessibilityValue)
135+
}
131136
}
132137
}
133138

@@ -307,6 +312,13 @@ extension NSObject {
307312
return false
308313
}
309314
}
315+
316+
private func hidesAccessibilityLabel(backDescriptor: String) -> Bool {
317+
// To prevent duplication, Back Button elements omit their label if it matches the localized "Back" descriptor.
318+
guard accessibilityTraits.contains(.backButton),
319+
let label = accessibilityLabel else { return false }
320+
return label.lowercased() == backDescriptor.lowercased()
321+
}
310322

311323
// MARK: - Private Static Properties
312324

@@ -325,6 +337,8 @@ extension NSObject {
325337
let buttonTraitName: String
326338

327339
let backButtonTraitName: String
340+
341+
let backDescriptor: String
328342

329343
let tabTraitName: String
330344

@@ -410,6 +424,11 @@ extension NSObject {
410424
comment: "Description for the 'back button' accessibility trait",
411425
locale: locale
412426
)
427+
self.backDescriptor = "Back".localized(
428+
key: "back.descriptor",
429+
comment: "Descriptor for the 'back' portion of the 'back button' accessibility trait",
430+
locale: locale
431+
)
413432
self.tabTraitName = "Tab.".localized(
414433
key: "trait.tab.description",
415434
comment: "Description for the 'tab' accessibility trait",

0 commit comments

Comments
 (0)