Merge pull request #814 from j-f1/alt-scroll

Allow AltViewController text to scroll + increase HUDButton contrast
This commit is contained in:
Nathan Mattes 2022-12-24 00:25:02 +01:00 committed by GitHub
commit 80c145111a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 15 deletions

View File

@ -9,7 +9,14 @@ import SwiftUI
class AltViewController: UIViewController { class AltViewController: UIViewController {
private var alt: String private var alt: String
let label = UITextView() let label = {
if #available(iOS 16, *) {
// TODO: update code below to use TextKit 2 when dropping iOS 15 support
return UITextView(usingTextLayoutManager: false)
} else {
return UITextView()
}
}()
init(alt: String, sourceView: UIView?) { init(alt: String, sourceView: UIView?) {
self.alt = alt self.alt = alt
@ -24,6 +31,11 @@ class AltViewController: UIViewController {
@MainActor required dynamic init?(coder aDecoder: NSCoder) { @MainActor required dynamic init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented") fatalError("init(coder:) has not been implemented")
} }
override func loadView() {
super.loadView()
view.translatesAutoresizingMaskIntoConstraints = false
}
override func viewDidLoad() { override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
@ -38,22 +50,20 @@ class AltViewController: UIViewController {
right: 0 right: 0
) )
label.font = .preferredFont(forTextStyle: .callout) label.font = .preferredFont(forTextStyle: .callout)
label.isScrollEnabled = false label.isScrollEnabled = true
label.backgroundColor = .clear label.backgroundColor = .clear
label.isOpaque = false label.isOpaque = false
label.isEditable = false label.isEditable = false
label.tintColor = .white label.tintColor = .white
label.text = alt label.text = alt
label.textContainerInset = UIEdgeInsets(top: 12, left: 8, bottom: 8, right: 8)
label.contentInsetAdjustmentBehavior = .always
label.verticalScrollIndicatorInsets.bottom = 4
view.translatesAutoresizingMaskIntoConstraints = false view.backgroundColor = .systemBackground
view.addSubview(label) view.addSubview(label)
NSLayoutConstraint.activate( label.pinToParent()
NSLayoutConstraint.constraints(withVisualFormat: "V:|-[label]-|", metrics: nil, views: ["label": label])
)
NSLayoutConstraint.activate(
NSLayoutConstraint.constraints(withVisualFormat: "H:|-(8)-[label]-(8)-|", metrics: nil, views: ["label": label])
)
NSLayoutConstraint.activate([ NSLayoutConstraint.activate([
label.widthAnchor.constraint(lessThanOrEqualToConstant: 400), label.widthAnchor.constraint(lessThanOrEqualToConstant: 400),
]) ])
@ -62,12 +72,18 @@ class AltViewController: UIViewController {
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
preferredContentSize = CGSize( preferredContentSize = CGSize(
width: label.intrinsicContentSize.width + 16, width: size.width + (8 + label.textContainer.lineFragmentPadding) * 2,
height: label.intrinsicContentSize.height + view.layoutMargins.top + view.layoutMargins.bottom height: size.height + 12 + (label.textContainer.lineFragmentPadding * 2)
) )
} }
} }
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
label.font = .preferredFont(forTextStyle: .callout)
}
} }
// MARK: UIPopoverPresentationControllerDelegate // MARK: UIPopoverPresentationControllerDelegate

View File

@ -8,6 +8,9 @@
import UIKit import UIKit
extension UIEdgeInsets { extension UIEdgeInsets {
public init(horizontal: CGFloat, vertical: CGFloat) {
self.init(top: vertical, left: horizontal, bottom: vertical, right: horizontal)
}
public static func constant(_ offset: CGFloat) -> Self { public static func constant(_ offset: CGFloat) -> Self {
UIEdgeInsets(top: offset, left: offset, bottom: offset, right: offset) UIEdgeInsets(top: offset, left: offset, bottom: offset, right: offset)
} }

View File

@ -12,18 +12,17 @@ public class HUDButton: UIView {
public static let height: CGFloat = 30 public static let height: CGFloat = 30
let background: UIVisualEffectView = { let background: UIVisualEffectView = {
let backgroundView = UIVisualEffectView(effect: UIBlurEffect(style: .systemUltraThinMaterial)) let backgroundView = UIVisualEffectView(effect: UIBlurEffect(style: .systemMaterial))
backgroundView.alpha = 0.9
backgroundView.layer.masksToBounds = true backgroundView.layer.masksToBounds = true
backgroundView.layer.cornerRadius = HUDButton.height * 0.5 backgroundView.layer.cornerRadius = HUDButton.height * 0.5
return backgroundView return backgroundView
}() }()
let vibrancyView = UIVisualEffectView(effect: UIVibrancyEffect(blurEffect: UIBlurEffect(style: .systemUltraThinMaterial))) let vibrancyView = UIVisualEffectView(effect: UIVibrancyEffect(blurEffect: UIBlurEffect(style: .systemMaterial)))
public let button: UIButton = { public let button: UIButton = {
let button = HighlightDimmableButton() let button = HighlightDimmableButton()
button.expandEdgeInsets = UIEdgeInsets(top: -10, left: -10, bottom: -10, right: -10) button.expandEdgeInsets = .constant(-10)
button.contentEdgeInsets = .constant(7) button.contentEdgeInsets = .constant(7)
button.imageView?.tintColor = .label button.imageView?.tintColor = .label
button.titleLabel?.font = UIFontMetrics(forTextStyle: .body).scaledFont(for: .systemFont(ofSize: 15, weight: .bold)) button.titleLabel?.font = UIFontMetrics(forTextStyle: .body).scaledFont(for: .systemFont(ofSize: 15, weight: .bold))