2022-11-26 15:39:24 +01:00
|
|
|
//
|
|
|
|
// AltViewController.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by Jed Fox on 2022-11-26.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
class AltViewController: UIViewController {
|
2022-11-26 21:11:13 +01:00
|
|
|
private var alt: String
|
2022-11-26 21:53:09 +01:00
|
|
|
let label = UITextView()
|
2022-11-26 15:39:24 +01:00
|
|
|
|
2022-11-26 21:11:13 +01:00
|
|
|
init(alt: String, sourceView: UIView?) {
|
2022-11-26 15:39:24 +01:00
|
|
|
self.alt = alt
|
2022-11-26 21:11:13 +01:00
|
|
|
super.init(nibName: nil, bundle: nil)
|
2022-11-26 15:39:24 +01:00
|
|
|
self.modalPresentationStyle = .popover
|
|
|
|
self.popoverPresentationController?.delegate = self
|
|
|
|
self.popoverPresentationController?.permittedArrowDirections = .up
|
|
|
|
self.popoverPresentationController?.sourceView = sourceView
|
|
|
|
self.overrideUserInterfaceStyle = .dark
|
|
|
|
}
|
|
|
|
|
|
|
|
@MainActor required dynamic init?(coder aDecoder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
2022-12-23 16:27:07 +01:00
|
|
|
|
|
|
|
override func loadView() {
|
|
|
|
super.loadView()
|
|
|
|
view.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
}
|
2022-11-26 15:39:24 +01:00
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
|
|
|
label.translatesAutoresizingMaskIntoConstraints = false
|
2022-11-26 21:53:09 +01:00
|
|
|
label.textContainer.maximumNumberOfLines = 0
|
|
|
|
label.textContainer.lineBreakMode = .byWordWrapping
|
|
|
|
label.textContainerInset = UIEdgeInsets(
|
|
|
|
top: 8,
|
|
|
|
left: 0,
|
|
|
|
bottom: -label.textContainer.lineFragmentPadding,
|
|
|
|
right: 0
|
|
|
|
)
|
2022-11-26 15:39:24 +01:00
|
|
|
label.font = .preferredFont(forTextStyle: .callout)
|
2022-12-23 16:27:07 +01:00
|
|
|
label.isScrollEnabled = true
|
2022-11-26 21:53:09 +01:00
|
|
|
label.backgroundColor = .clear
|
|
|
|
label.isOpaque = false
|
|
|
|
label.isEditable = false
|
|
|
|
label.tintColor = .white
|
2022-11-26 21:11:13 +01:00
|
|
|
label.text = alt
|
2022-12-23 17:14:04 +01:00
|
|
|
label.textContainerInset = UIEdgeInsets(horizontal: 8, vertical: 16)
|
|
|
|
label.verticalScrollIndicatorInsets.bottom = 4
|
2022-11-26 15:39:24 +01:00
|
|
|
|
2022-12-23 17:14:04 +01:00
|
|
|
view.backgroundColor = .systemBackground
|
2022-11-26 15:39:24 +01:00
|
|
|
view.addSubview(label)
|
|
|
|
|
2022-12-23 17:14:04 +01:00
|
|
|
label.pinToParent()
|
2022-11-26 15:39:24 +01:00
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
label.widthAnchor.constraint(lessThanOrEqualToConstant: 400),
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
|
|
|
override func viewDidLayoutSubviews() {
|
|
|
|
super.viewDidLayoutSubviews()
|
|
|
|
UIView.performWithoutAnimation {
|
|
|
|
preferredContentSize = CGSize(
|
2022-12-23 16:27:07 +01:00
|
|
|
width: label.contentSize.width + 16,
|
|
|
|
height: label.contentSize.height + view.layoutMargins.top + view.layoutMargins.bottom
|
2022-11-26 15:39:24 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: UIPopoverPresentationControllerDelegate
|
|
|
|
extension AltViewController: UIPopoverPresentationControllerDelegate {
|
|
|
|
func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
|
|
|
|
.none
|
|
|
|
}
|
|
|
|
}
|