Slightly adjust code (#806)

Renaming, remove iOS-checks.
This commit is contained in:
Nathan Mattes 2022-12-26 15:29:01 +01:00
parent 80c145111a
commit f26f36a60b
4 changed files with 43 additions and 60 deletions

View File

@ -8,18 +8,33 @@
import SwiftUI import SwiftUI
class AltViewController: UIViewController { class AltViewController: UIViewController {
private var alt: String let textView = {
let label = { let textView: UITextView
if #available(iOS 16, *) { if #available(iOS 16, *) {
// TODO: update code below to use TextKit 2 when dropping iOS 15 support // TODO: update code below to use TextKit 2 when dropping iOS 15 support
return UITextView(usingTextLayoutManager: false) textView = UITextView(usingTextLayoutManager: false)
} else { } else {
return UITextView() textView = UITextView()
} }
textView.textContainer.maximumNumberOfLines = 0
textView.textContainer.lineBreakMode = .byWordWrapping
textView.font = .preferredFont(forTextStyle: .callout)
textView.isScrollEnabled = true
textView.backgroundColor = .clear
textView.isOpaque = false
textView.isEditable = false
textView.tintColor = .white
textView.textContainerInset = UIEdgeInsets(top: 12, left: 8, bottom: 8, right: 8)
textView.contentInsetAdjustmentBehavior = .always
textView.verticalScrollIndicatorInsets.bottom = 4
return textView
}() }()
init(alt: String, sourceView: UIView?) { init(alt: String, sourceView: UIView?) {
self.alt = alt textView.text = alt
super.init(nibName: nil, bundle: nil) super.init(nibName: nil, bundle: nil)
self.modalPresentationStyle = .popover self.modalPresentationStyle = .popover
self.popoverPresentationController?.delegate = self self.popoverPresentationController?.delegate = self
@ -40,49 +55,32 @@ class AltViewController: UIViewController {
override func viewDidLoad() { override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
label.translatesAutoresizingMaskIntoConstraints = false textView.translatesAutoresizingMaskIntoConstraints = false
label.textContainer.maximumNumberOfLines = 0
label.textContainer.lineBreakMode = .byWordWrapping
label.textContainerInset = UIEdgeInsets(
top: 8,
left: 0,
bottom: -label.textContainer.lineFragmentPadding,
right: 0
)
label.font = .preferredFont(forTextStyle: .callout)
label.isScrollEnabled = true
label.backgroundColor = .clear
label.isOpaque = false
label.isEditable = false
label.tintColor = .white
label.text = alt
label.textContainerInset = UIEdgeInsets(top: 12, left: 8, bottom: 8, right: 8)
label.contentInsetAdjustmentBehavior = .always
label.verticalScrollIndicatorInsets.bottom = 4
view.backgroundColor = .systemBackground view.backgroundColor = .systemBackground
view.addSubview(label) view.addSubview(textView)
label.pinToParent() textView.pinToParent()
NSLayoutConstraint.activate([ NSLayoutConstraint.activate([
label.widthAnchor.constraint(lessThanOrEqualToConstant: 400), textView.widthAnchor.constraint(lessThanOrEqualToConstant: 400),
]) ])
} }
override func viewDidLayoutSubviews() { override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews() super.viewDidLayoutSubviews()
UIView.performWithoutAnimation { UIView.performWithoutAnimation {
let size = label.layoutManager.boundingRect(forGlyphRange: NSMakeRange(0, (label.textStorage.string as NSString).length), in: label.textContainer).size
let size = textView.layoutManager.boundingRect(forGlyphRange: NSMakeRange(0, (textView.textStorage.string as NSString).length), in: textView.textContainer).size
preferredContentSize = CGSize( preferredContentSize = CGSize(
width: size.width + (8 + label.textContainer.lineFragmentPadding) * 2, width: size.width + (8 + textView.textContainer.lineFragmentPadding) * 2,
height: size.height + 12 + (label.textContainer.lineFragmentPadding * 2) height: size.height + 12 + (textView.textContainer.lineFragmentPadding) * 2
) )
} }
} }
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) { override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection) super.traitCollectionDidChange(previousTraitCollection)
label.font = .preferredFont(forTextStyle: .callout) textView.font = .preferredFont(forTextStyle: .callout)
} }
} }

View File

@ -181,9 +181,9 @@ extension MediaPreviewViewController {
@objc private func altButtonPressed(_ sender: UIButton) { @objc private func altButtonPressed(_ sender: UIButton) {
guard let alt = viewModel.altText else { return } guard let alt = viewModel.altText else { return }
present(AltViewController(alt: alt, sourceView: sender), animated: true) present(AltViewController(alt: alt, sourceView: sender), animated: true)
} }
} }
// MARK: - MediaPreviewingViewController // MARK: - MediaPreviewingViewController

View File

@ -7,7 +7,6 @@
import SwiftUI import SwiftUI
@available(iOS 15.0, *)
struct MediaAltTextOverlay: View { struct MediaAltTextOverlay: View {
var altDescription: String? var altDescription: String?
@ -69,7 +68,6 @@ struct MediaAltTextOverlay: View {
} }
} }
@available(iOS 15.0, *)
struct MediaAltTextOverlay_Previews: PreviewProvider { struct MediaAltTextOverlay_Previews: PreviewProvider {
static var previews: some View { static var previews: some View {
MediaAltTextOverlay(altDescription: "Hello, world!") MediaAltTextOverlay(altDescription: "Hello, world!")

View File

@ -73,19 +73,11 @@ public final class MediaView: UIView {
return label return label
}() }()
let _altViewController: UIViewController! = { let altViewController: UIHostingController<MediaAltTextOverlay> = {
if #available(iOS 15.0, *) { let vc = UIHostingController(rootView: MediaAltTextOverlay())
let vc = UIHostingController(rootView: MediaAltTextOverlay()) vc.view.backgroundColor = .clear
vc.view.backgroundColor = .clear return vc
return vc
} else {
return nil
}
}() }()
@available(iOS 15.0, *)
var altViewController: UIHostingController<MediaAltTextOverlay> {
_altViewController as! UIHostingController<MediaAltTextOverlay>
}
public override init(frame: CGRect) { public override init(frame: CGRect) {
super.init(frame: frame) super.init(frame: frame)
@ -228,9 +220,8 @@ extension MediaView {
} else { } else {
accessibilityLabel = altDescription accessibilityLabel = altDescription
} }
if #available(iOS 15.0, *) {
altViewController.rootView.altDescription = altDescription altViewController.rootView.altDescription = altDescription
}
} }
private func layoutBlurhash() { private func layoutBlurhash() {
@ -262,11 +253,9 @@ extension MediaView {
} }
private func layoutAlt() { private func layoutAlt() {
if #available(iOS 15.0, *) { altViewController.view.translatesAutoresizingMaskIntoConstraints = false
altViewController.view.translatesAutoresizingMaskIntoConstraints = false container.addSubview(altViewController.view)
container.addSubview(altViewController.view) altViewController.view.pinToParent()
altViewController.view.pinToParent()
}
} }
public func prepareForReuse() { public func prepareForReuse() {
@ -304,9 +293,7 @@ extension MediaView {
container.removeFromSuperview() container.removeFromSuperview()
container.removeConstraints(container.constraints) container.removeConstraints(container.constraints)
if #available(iOS 15.0, *) { altViewController.rootView.altDescription = nil
altViewController.rootView.altDescription = nil
}
// reset configuration // reset configuration
configuration = nil configuration = nil