Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace newlines in a11y labels with spaces #502

Merged
merged 2 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions BlueprintUICommonControls/Sources/AttributedLabel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,11 @@ extension AttributedLabel {
// Insert the word "link" after each link in the label. This mirrors the VoiceOver behavior when encountering a `.link` attribute.

guard let localizedLinkString = linkAccessibilityLabel,
!links.isEmpty else { return string }
!links.isEmpty
else {
// We need to replace all newlines with " "
return string.removingNewlines
}
var label = string
// Wrap the word in [brackets] to indicate that it is a tag distinct from the content string. This is transparent to voiceover but should be helpful when the accessibility label is printed e.g. in the accessibility inspector.

Expand Down Expand Up @@ -595,7 +599,9 @@ extension AttributedLabel {
}
label.insert(contentsOf: insertionString, at: insertionPoint)
}
return label

// We need to replace all newlines with " "
return label.removingNewlines
}

func applyLinkColors(activeLinks: [Link] = []) {
Expand Down Expand Up @@ -849,3 +855,8 @@ extension NSTextCheckingResult.CheckingType {
}
}

extension String {
var removingNewlines: String {
components(separatedBy: .newlines).filter { !$0.isEmpty }.joined(separator: " ")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,20 @@ class AttributedLabelTests: XCTestCase {
compareSnapshot(of: element)
}

func test_multilineAccessibility() {
let labelview = AttributedLabel.LabelView()

for (text, expected) in [
("Test Test", "Test Test"),
("Test\nTest", "Test Test"),
("Test\n\nTest", "Test Test"),
("\n\n\n\nTest\n\n\nTest\n\n\n", "Test Test"),
] {
let result = labelview.accessibilityLabel(with: [], in: text, linkAccessibilityLabel: nil)
XCTAssertEqual(expected, result)
}
}

func test_linkAccessibility() {
let labelview = AttributedLabel.LabelView()

Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- Fix a bug in which newlines were preserved in accessibility labels.

### Added

### Removed
Expand Down
Loading