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-12-26 15:29:01 +01:00
|
|
|
let textView = {
|
|
|
|
let textView: UITextView
|
|
|
|
|
2022-12-23 18:08:46 +01:00
|
|
|
if #available(iOS 16, *) {
|
|
|
|
// TODO: update code below to use TextKit 2 when dropping iOS 15 support
|
2022-12-26 15:29:01 +01:00
|
|
|
textView = UITextView(usingTextLayoutManager: false)
|
2022-12-23 18:08:46 +01:00
|
|
|
} else {
|
2022-12-26 15:29:01 +01:00
|
|
|
textView = UITextView()
|
2022-12-23 18:08:46 +01:00
|
|
|
}
|
2022-12-26 15:29:01 +01:00
|
|
|
|
|
|
|
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
|
2022-12-23 18:08:46 +01:00
|
|
|
}()
|
2022-11-26 15:39:24 +01:00
|
|
|
|
2022-11-26 21:11:13 +01:00
|
|
|
init(alt: String, sourceView: UIView?) {
|
2022-12-26 15:29:01 +01:00
|
|
|
textView.text = 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()
|
|
|
|
|
2022-12-26 15:29:01 +01:00
|
|
|
textView.translatesAutoresizingMaskIntoConstraints = false
|
2022-12-23 17:14:04 +01:00
|
|
|
view.backgroundColor = .systemBackground
|
2022-12-26 15:29:01 +01:00
|
|
|
view.addSubview(textView)
|
2022-11-26 15:39:24 +01:00
|
|
|
|
2022-12-26 15:29:01 +01:00
|
|
|
textView.pinToParent()
|
2022-11-26 15:39:24 +01:00
|
|
|
NSLayoutConstraint.activate([
|
2022-12-26 15:29:01 +01:00
|
|
|
textView.widthAnchor.constraint(lessThanOrEqualToConstant: 400),
|
2022-11-26 15:39:24 +01:00
|
|
|
])
|
|
|
|
}
|
|
|
|
|
|
|
|
override func viewDidLayoutSubviews() {
|
|
|
|
super.viewDidLayoutSubviews()
|
|
|
|
UIView.performWithoutAnimation {
|
2022-12-26 15:29:01 +01:00
|
|
|
|
|
|
|
let size = textView.layoutManager.boundingRect(forGlyphRange: NSMakeRange(0, (textView.textStorage.string as NSString).length), in: textView.textContainer).size
|
|
|
|
|
2022-11-26 15:39:24 +01:00
|
|
|
preferredContentSize = CGSize(
|
2022-12-26 15:29:01 +01:00
|
|
|
width: size.width + (8 + textView.textContainer.lineFragmentPadding) * 2,
|
|
|
|
height: size.height + 12 + (textView.textContainer.lineFragmentPadding) * 2
|
2022-11-26 15:39:24 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2022-12-23 20:16:41 +01:00
|
|
|
|
|
|
|
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
|
|
|
|
super.traitCollectionDidChange(previousTraitCollection)
|
2022-12-26 15:29:01 +01:00
|
|
|
textView.font = .preferredFont(forTextStyle: .callout)
|
2022-12-23 20:16:41 +01:00
|
|
|
}
|
2022-11-26 15:39:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: UIPopoverPresentationControllerDelegate
|
|
|
|
extension AltViewController: UIPopoverPresentationControllerDelegate {
|
|
|
|
func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
|
|
|
|
.none
|
|
|
|
}
|
|
|
|
}
|