|
| 1 | +// |
| 2 | +// Copyright 2025 Square Inc. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +import UIKit |
| 20 | + |
| 21 | +class ElementFrameComparisonController: AccessibilityViewController { |
| 22 | + |
| 23 | + override func viewDidLoad() { |
| 24 | + super.viewDidLoad() |
| 25 | + |
| 26 | + view.backgroundColor = .white |
| 27 | + |
| 28 | + let frameHeader = MyLabel("Adjusting the accessibility frame vertically by > 8.0 alters the sort order") |
| 29 | + frameHeader.accessibilityTraits = [.staticText, .header] |
| 30 | + |
| 31 | + let pathHeader = MyLabel("Accessibility path is ignored entirely for sorting purposes") |
| 32 | + pathHeader.accessibilityTraits = [.staticText, .header] |
| 33 | + |
| 34 | + let spacing = 20.0 |
| 35 | + |
| 36 | + // 8 seems to be the magic number for VoiceOver to consider it |
| 37 | + // to be vertically "above" other views |
| 38 | + let voiceoverMagicNumber = 8.0 |
| 39 | + |
| 40 | + let frames = UIStackView(arrangedSubviews: [ |
| 41 | + // Label where the accessibilityFrame will be the view's frame |
| 42 | + // translated vertically by 8 pt down |
| 43 | + MyLabel( |
| 44 | + "frame down", |
| 45 | + accessibilityLabel: "Third", |
| 46 | + accessibilityFrame: .rect({ |
| 47 | + var rect = $0 |
| 48 | + rect.origin.y += voiceoverMagicNumber |
| 49 | + return rect |
| 50 | + }) |
| 51 | + ), |
| 52 | + |
| 53 | + // Label where the accessibilityFrame is unmodified |
| 54 | + MyLabel( "unchanged", accessibilityLabel: "Second"), |
| 55 | + |
| 56 | + // Label where the accessibilityFrame will be the view's frame |
| 57 | + // translated vertically by 8 pt up |
| 58 | + MyLabel( |
| 59 | + "frame up", |
| 60 | + accessibilityLabel: "First", |
| 61 | + accessibilityFrame: .rect({ |
| 62 | + var rect = $0 |
| 63 | + rect.origin.y -= voiceoverMagicNumber |
| 64 | + return rect |
| 65 | + }) |
| 66 | + ) |
| 67 | + ]) |
| 68 | + |
| 69 | + let paths = UIStackView(arrangedSubviews: [ |
| 70 | + |
| 71 | + // Label where the accessibilityPath will be the view's frame |
| 72 | + // translated vertically by 8 pt down |
| 73 | + MyLabel( |
| 74 | + "path down", |
| 75 | + accessibilityLabel: "Fourth", |
| 76 | + accessibilityFrame: .path({ |
| 77 | + var rect = $0 |
| 78 | + rect.origin.y += voiceoverMagicNumber |
| 79 | + return UIBezierPath(roundedRect: rect, cornerRadius: 5) |
| 80 | + }) |
| 81 | + ), |
| 82 | + |
| 83 | + // Label where the accessibilityPath is unmodified |
| 84 | + MyLabel( |
| 85 | + "path unchanged", |
| 86 | + accessibilityLabel: "Fifth", |
| 87 | + accessibilityFrame: .path({ |
| 88 | + return UIBezierPath(roundedRect: $0, cornerRadius: 5) |
| 89 | + }) |
| 90 | + ), |
| 91 | + |
| 92 | + |
| 93 | + // Label where the accessibilityPath will be the view's frame |
| 94 | + // translated vertically by 8 pt up |
| 95 | + MyLabel( |
| 96 | + "path up", |
| 97 | + accessibilityLabel: "Sixth", |
| 98 | + accessibilityFrame: .path({ |
| 99 | + var rect = $0 |
| 100 | + rect.origin.y -= voiceoverMagicNumber |
| 101 | + return UIBezierPath(roundedRect: rect, cornerRadius: 5) |
| 102 | + }) |
| 103 | + ) |
| 104 | + |
| 105 | + ]) |
| 106 | + |
| 107 | + frames.axis = .horizontal |
| 108 | + paths.axis = .horizontal |
| 109 | + |
| 110 | + frames.spacing = spacing |
| 111 | + paths.spacing = spacing |
| 112 | + |
| 113 | + view.addSubview(frameHeader) |
| 114 | + view.addSubview(frames) |
| 115 | + |
| 116 | + view.addSubview(pathHeader) |
| 117 | + view.addSubview(paths) |
| 118 | + |
| 119 | + frameHeader.translatesAutoresizingMaskIntoConstraints = false |
| 120 | + frames.translatesAutoresizingMaskIntoConstraints = false |
| 121 | + |
| 122 | + pathHeader.translatesAutoresizingMaskIntoConstraints = false |
| 123 | + paths.translatesAutoresizingMaskIntoConstraints = false |
| 124 | + |
| 125 | + NSLayoutConstraint.activate([ |
| 126 | + frameHeader.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: spacing), |
| 127 | + frameHeader.leadingAnchor.constraint(equalTo: view.leadingAnchor), |
| 128 | + frameHeader.trailingAnchor.constraint(equalTo: view.trailingAnchor), |
| 129 | + |
| 130 | + frames.topAnchor.constraint(equalTo: frameHeader.bottomAnchor, constant: spacing), |
| 131 | + frames.leadingAnchor.constraint(equalTo: view.leadingAnchor), |
| 132 | + frames.trailingAnchor.constraint(equalTo: view.trailingAnchor), |
| 133 | + |
| 134 | + pathHeader.topAnchor.constraint(equalTo: frames.bottomAnchor, constant: spacing), |
| 135 | + pathHeader.leadingAnchor.constraint(equalTo: view.leadingAnchor), |
| 136 | + pathHeader.trailingAnchor.constraint(equalTo: view.trailingAnchor), |
| 137 | + |
| 138 | + paths.topAnchor.constraint(equalTo: pathHeader.bottomAnchor, constant: spacing), |
| 139 | + paths.leadingAnchor.constraint(equalTo: view.leadingAnchor), |
| 140 | + paths.trailingAnchor.constraint(equalTo: view.trailingAnchor), |
| 141 | + |
| 142 | + ]) |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +enum AccessibilityFrame { |
| 147 | + case `default` |
| 148 | + case path((CGRect) -> UIBezierPath) |
| 149 | + case rect((CGRect) -> CGRect) |
| 150 | +} |
| 151 | + |
| 152 | +class MyLabel: UILabel { |
| 153 | + |
| 154 | + private let _accessibilityFrame: AccessibilityFrame |
| 155 | + |
| 156 | + init(_ text: String, align: NSTextAlignment = .left, accessibilityLabel: String? = nil, accessibilityFrame: AccessibilityFrame = .default) { |
| 157 | + self._accessibilityFrame = accessibilityFrame |
| 158 | + super.init(frame: .zero) |
| 159 | + self.text = text |
| 160 | + self.textAlignment = align |
| 161 | + self.backgroundColor = .gray.withAlphaComponent(0.2) |
| 162 | + if let accessibilityLabel { |
| 163 | + self.accessibilityLabel = accessibilityLabel |
| 164 | + } |
| 165 | + |
| 166 | + self.numberOfLines = 0 |
| 167 | + self.lineBreakMode = .byWordWrapping |
| 168 | + } |
| 169 | + |
| 170 | + required init?(coder: NSCoder) { nil } |
| 171 | + |
| 172 | + override var accessibilityPath: UIBezierPath? { |
| 173 | + set { _ = newValue } |
| 174 | + get { |
| 175 | + switch _accessibilityFrame { |
| 176 | + |
| 177 | + case .default, .rect: |
| 178 | + return nil |
| 179 | + |
| 180 | + case .path(let transform): |
| 181 | + guard let superview else { return nil } |
| 182 | + return UIAccessibility.convertToScreenCoordinates(transform(frame), in: superview) |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + override var accessibilityFrame: CGRect { |
| 188 | + set { _ = newValue } |
| 189 | + get { |
| 190 | + switch _accessibilityFrame { |
| 191 | + case .default, .path: |
| 192 | + return super.accessibilityFrame |
| 193 | + |
| 194 | + case .rect(let transform): |
| 195 | + guard let superview else { return super.accessibilityFrame } |
| 196 | + return UIAccessibility.convertToScreenCoordinates(transform(frame), in: superview) |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | +} |
0 commit comments