mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2026-12-01 09:50:16 +00:00
Create and use a CopyableLabel for the version string that appears in settings and about view.
This commit is contained in:
1 parent
5728e0a7b6
commit
b808eaad89
3 files changed
+90
-35
No files matched your search
@@ -7,6 +7,7 @@
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
import RSCore
|
||||
|
||||
struct AboutView: View {
|
||||
var body: some View {
|
||||
@@ -20,8 +21,9 @@ struct AboutView: View {
|
||||
Text(verbatim: "NetNewsWire")
|
||||
.font(.largeTitle)
|
||||
|
||||
CopyableText(text: "Version \(Bundle.main.versionNumber) (build \(Bundle.main.buildNumber))")
|
||||
|
||||
Text(verbatim: "By Brent Simmons and the NetNewsWire team")
|
||||
.foregroundStyle(.secondary)
|
||||
Text("[netnewswire.com](https://netnewswire.com/)")
|
||||
|
||||
VStack(spacing: 6) {
|
||||
|
||||
@@ -150,14 +150,11 @@ final class SettingsViewController: UITableViewController {
|
||||
|
||||
openLinksInNetNewsWire.isOn = !AppDefaults.shared.useSystemBrowser
|
||||
|
||||
let buildLabel = NonIntrinsicLabel(frame: CGRect(x: 32.0, y: 0.0, width: 0.0, height: 0.0))
|
||||
let buildLabel = CopyableLabel(frame: CGRect(x: 32.0, y: 0.0, width: 0.0, height: 0.0))
|
||||
buildLabel.font = UIFont.systemFont(ofSize: 11.0)
|
||||
buildLabel.textColor = UIColor.gray
|
||||
buildLabel.text = versionString
|
||||
buildLabel.text = "\(Bundle.main.appName) \(Bundle.main.versionNumber) (build \(Bundle.main.buildNumber))"
|
||||
buildLabel.sizeToFit()
|
||||
buildLabel.isUserInteractionEnabled = true
|
||||
buildLabel.addInteraction(UIEditMenuInteraction(delegate: self))
|
||||
buildLabel.addGestureRecognizer(UILongPressGestureRecognizer(target: self, action: #selector(showVersionEditMenu(_:))))
|
||||
|
||||
let wrapperView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.width, height: buildLabel.frame.height + 10.0))
|
||||
wrapperView.addSubview(buildLabel)
|
||||
@@ -464,39 +461,10 @@ extension SettingsViewController: UIDocumentPickerDelegate {
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Version Edit Menu
|
||||
|
||||
extension SettingsViewController: @preconcurrency UIEditMenuInteractionDelegate {
|
||||
|
||||
func editMenuInteraction(_ interaction: UIEditMenuInteraction, menuFor configuration: UIEditMenuConfiguration, suggestedActions: [UIMenuElement]) -> UIMenu? {
|
||||
let versionString = versionString
|
||||
let title = NSLocalizedString("Copy", comment: "Command")
|
||||
let copyAction = UIAction(title: title) { _ in
|
||||
UIPasteboard.general.string = versionString
|
||||
}
|
||||
return UIMenu(children: [copyAction])
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Private
|
||||
|
||||
private extension SettingsViewController {
|
||||
|
||||
var versionString: String {
|
||||
"\(Bundle.main.appName) \(Bundle.main.versionNumber) (Build \(Bundle.main.buildNumber))"
|
||||
}
|
||||
|
||||
@objc func showVersionEditMenu(_ recognizer: UILongPressGestureRecognizer) {
|
||||
guard recognizer.state == .began, let label = recognizer.view else {
|
||||
return
|
||||
}
|
||||
guard let editMenuInteraction = label.interactions.first(where: { $0 is UIEditMenuInteraction }) as? UIEditMenuInteraction else {
|
||||
return
|
||||
}
|
||||
let sourcePoint = CGPoint(x: label.bounds.midX, y: label.bounds.minY)
|
||||
editMenuInteraction.presentEditMenu(with: UIEditMenuConfiguration(identifier: nil, sourcePoint: sourcePoint))
|
||||
}
|
||||
|
||||
static func makePopUpButton() -> UIButton {
|
||||
var configuration = UIButton.Configuration.plain()
|
||||
configuration.baseForegroundColor = .secondaryLabel
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
//
|
||||
// CopyableLabel.swift
|
||||
// NetNewsWire-iOS
|
||||
//
|
||||
// Created by Brent Simmons on 10/20/26.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
import SwiftUI
|
||||
|
||||
/// A label that shows a Copy edit menu on long press. Copy copies the entire text — no selection.
|
||||
final class CopyableLabel: UILabel {
|
||||
|
||||
override init(frame: CGRect) {
|
||||
super.init(frame: frame)
|
||||
commonInit()
|
||||
}
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
super.init(coder: coder)
|
||||
commonInit()
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - UIEditMenuInteractionDelegate
|
||||
|
||||
extension CopyableLabel: @preconcurrency UIEditMenuInteractionDelegate {
|
||||
|
||||
func editMenuInteraction(_ interaction: UIEditMenuInteraction, menuFor configuration: UIEditMenuConfiguration, suggestedActions: [UIMenuElement]) -> UIMenu? {
|
||||
let title = NSLocalizedString("Copy", comment: "Command")
|
||||
let copyAction = UIAction(title: title) { [weak self] _ in
|
||||
UIPasteboard.general.string = self?.text
|
||||
}
|
||||
return UIMenu(children: [copyAction])
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Private
|
||||
|
||||
private extension CopyableLabel {
|
||||
|
||||
func commonInit() {
|
||||
isUserInteractionEnabled = true
|
||||
addInteraction(UIEditMenuInteraction(delegate: self))
|
||||
addGestureRecognizer(UILongPressGestureRecognizer(target: self, action: #selector(showEditMenu(_:))))
|
||||
}
|
||||
|
||||
@objc func showEditMenu(_ recognizer: UILongPressGestureRecognizer) {
|
||||
guard recognizer.state == .began else {
|
||||
return
|
||||
}
|
||||
guard let editMenuInteraction = interactions.first(where: { $0 is UIEditMenuInteraction }) as? UIEditMenuInteraction else {
|
||||
return
|
||||
}
|
||||
let sourcePoint = CGPoint(x: bounds.midX, y: bounds.minY)
|
||||
editMenuInteraction.presentEditMenu(with: UIEditMenuConfiguration(identifier: nil, sourcePoint: sourcePoint))
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - SwiftUI
|
||||
|
||||
/// SwiftUI wrapper for `CopyableLabel`: centered, secondary-colored body text.
|
||||
struct CopyableText: UIViewRepresentable {
|
||||
|
||||
let text: String
|
||||
|
||||
func makeUIView(context: Context) -> CopyableLabel {
|
||||
let label = CopyableLabel()
|
||||
label.font = UIFont.preferredFont(forTextStyle: .body)
|
||||
label.adjustsFontForContentSizeCategory = true
|
||||
label.textColor = UIColor.secondaryLabel
|
||||
label.textAlignment = .center
|
||||
label.numberOfLines = 0
|
||||
return label
|
||||
}
|
||||
|
||||
func updateUIView(_ label: CopyableLabel, context: Context) {
|
||||
label.text = text
|
||||
}
|
||||
|
||||
func sizeThatFits(_ proposal: ProposedViewSize, uiView label: CopyableLabel, context: Context) -> CGSize? {
|
||||
let width = proposal.width ?? CGFloat.greatestFiniteMagnitude
|
||||
return label.sizeThatFits(CGSize(width: width, height: CGFloat.greatestFiniteMagnitude))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user