Created
May 10, 2023 16:12
-
-
Save minsOne/5154c597bb4b546dbeb49c7f68c8c7b0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import AXSnapshot | |
class ViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view. | |
do { | |
let label = UILabel(frame: .init(x: 100, y: 100, width: 100, height: 100)) | |
label.text = "Hello" | |
view.addSubview(label) | |
} | |
do { | |
let label = UILabel(frame: .init(x: 100, y: 200, width: 100, height: 100)) | |
label.text = "World" | |
view.addSubview(label) | |
} | |
} | |
} | |
struct ViewController_Preview: PreviewProvider { | |
static var previews: some View { | |
ViewController() | |
.printAccessiblity() | |
.showPreview(.iPhone8) | |
} | |
} | |
#if canImport(SwiftUI) && DEBUG | |
import SwiftUI | |
struct UIViewPreview<View: UIView>: UIViewRepresentable { | |
let view: View | |
init(_ builder: @escaping () -> View) { | |
view = builder() | |
} | |
// MARK: - UIViewRepresentable | |
func makeUIView(context: Context) -> UIView { | |
return view | |
} | |
func updateUIView(_ view: UIView, context: Context) { | |
view.setContentHuggingPriority(.defaultHigh, for: .horizontal) | |
view.setContentHuggingPriority(.defaultHigh, for: .vertical) | |
} | |
} | |
extension UIViewController { | |
private struct Preview: UIViewControllerRepresentable { | |
let viewController: UIViewController | |
func makeUIViewController(context: Context) -> UIViewController { | |
return viewController | |
} | |
func updateUIViewController(_ uiViewController: UIViewController, context: Context) { | |
} | |
} | |
func printAccessiblity() -> Self { | |
DispatchQueue.main.asyncAfter(deadline: .now() + 1, execute: { [weak self] in | |
guard let self else { return } | |
print(self.view.axSnapshot()) | |
self.view.exposedAccessibleViews().enumerated().forEach { (index, view) in | |
let label = UILabel() | |
label.backgroundColor = .darkGray.withAlphaComponent(0.5) | |
label.text = "\(index)" | |
label.sizeToFit() | |
label.isUserInteractionEnabled = false | |
label.accessibilityTraits = .none | |
view.addSubview(label) | |
// view.backgroundColor = .lightGray | |
// view.layer.borderWidth = 1 | |
} | |
}) | |
return self | |
} | |
func showPreview(_ deviceType: DeviceType = .iPhone12Pro) -> some View { | |
Preview(viewController: self).previewDevice(PreviewDevice(rawValue: deviceType.name())) | |
} | |
} | |
#endif | |
enum DeviceType { | |
case iPhoneSE2 | |
case iPhone8 | |
case iPhone12Pro | |
case iPhone12ProMax | |
func name() -> String { | |
switch self { | |
case .iPhoneSE2: | |
return "iPhone SE" | |
case .iPhone8: | |
return "iPhone 8" | |
case .iPhone12Pro: | |
return "iPhone 12 Pro" | |
case .iPhone12ProMax: | |
return "iPhone 12 Pro Max" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment