diff --git a/BlueprintUICommonControls/Sources/AttributedLabel.swift b/BlueprintUICommonControls/Sources/AttributedLabel.swift index 8349a9098..a48fc5656 100644 --- a/BlueprintUICommonControls/Sources/AttributedLabel.swift +++ b/BlueprintUICommonControls/Sources/AttributedLabel.swift @@ -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. @@ -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] = []) { @@ -849,3 +855,8 @@ extension NSTextCheckingResult.CheckingType { } } +extension String { + var removingNewlines: String { + components(separatedBy: .newlines).filter { !$0.isEmpty }.joined(separator: " ") + } +} diff --git a/BlueprintUICommonControls/Tests/Sources/AttributedLabelTests.swift b/BlueprintUICommonControls/Tests/Sources/AttributedLabelTests.swift index ed58e802a..43c3985ba 100644 --- a/BlueprintUICommonControls/Tests/Sources/AttributedLabelTests.swift +++ b/BlueprintUICommonControls/Tests/Sources/AttributedLabelTests.swift @@ -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() diff --git a/CHANGELOG.md b/CHANGELOG.md index baea9efaa..9f5469a81 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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