mastodon-ios/Mastodon/Scene/MediaPreview/AltViewController.swift

81 lines
2.5 KiB
Swift
Raw Normal View History

//
// AltViewController.swift
// Mastodon
//
// Created by Jed Fox on 2022-11-26.
//
import SwiftUI
class AltViewController: UIViewController {
private var alt: String
2022-11-26 21:53:09 +01:00
let label = UITextView()
init(alt: String, sourceView: UIView?) {
self.alt = alt
super.init(nibName: nil, bundle: nil)
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
}
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
)
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
label.text = alt
2022-12-23 17:14:04 +01:00
label.textContainerInset = UIEdgeInsets(horizontal: 8, vertical: 16)
label.verticalScrollIndicatorInsets.bottom = 4
2022-12-23 17:14:04 +01:00
view.backgroundColor = .systemBackground
view.addSubview(label)
2022-12-23 17:14:04 +01:00
label.pinToParent()
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
)
}
}
}
// MARK: UIPopoverPresentationControllerDelegate
extension AltViewController: UIPopoverPresentationControllerDelegate {
func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
.none
}
}