Files
note/Note/PreviewViewController.swift

100 lines
4.2 KiB
Swift

//
// PreviewViewController.swift
// Note
//
// Created by fish on 2026/7/13.
//
import UIKit
class PreviewViewController: UIViewController {
private var textView: UITextView!
private var logButton: UIButton!
private var noteButton: UIButton!
var initialText: String?
var fileName: String?
override func viewDidLoad() {
super.viewDidLoad()
title = "预览"
navigationItem.largeTitleDisplayMode = .never
view.backgroundColor = .systemGroupedBackground
setupUI()
}
private func setupUI() {
textView = UITextView()
textView.translatesAutoresizingMaskIntoConstraints = false
textView.backgroundColor = UIColor { traitCollection in
traitCollection.userInterfaceStyle == .dark ? .secondarySystemBackground : .white
}
textView.layer.cornerRadius = 12
textView.layer.borderWidth = 0.5
textView.layer.borderColor = UIColor.separator.cgColor
textView.layer.masksToBounds = true
textView.font = UIFont.preferredFont(forTextStyle: .body)
textView.textContainerInset = UIEdgeInsets(top: 12, left: 12, bottom: 12, right: 12)
textView.isEditable = false
textView.isSelectable = true
textView.text = initialText
view.addSubview(textView)
logButton = createBottomButton(title: "处理日志", color: .systemBlue)
logButton.addTarget(self, action: #selector(didTapLogButton), for: .touchUpInside)
view.addSubview(logButton)
noteButton = createBottomButton(title: "处理 Note", color: .systemGreen)
noteButton.addTarget(self, action: #selector(didTapNoteButton), for: .touchUpInside)
view.addSubview(noteButton)
let safeArea = view.safeAreaLayoutGuide
let buttonHeight: CGFloat = 52
let spacing: CGFloat = 16
NSLayoutConstraint.activate([
textView.topAnchor.constraint(equalTo: safeArea.topAnchor, constant: spacing),
textView.leadingAnchor.constraint(equalTo: safeArea.leadingAnchor, constant: spacing),
textView.trailingAnchor.constraint(equalTo: safeArea.trailingAnchor, constant: -spacing),
logButton.topAnchor.constraint(equalTo: textView.bottomAnchor, constant: spacing),
logButton.leadingAnchor.constraint(equalTo: safeArea.leadingAnchor, constant: spacing),
logButton.trailingAnchor.constraint(equalTo: safeArea.centerXAnchor, constant: -spacing / 2),
logButton.heightAnchor.constraint(equalToConstant: buttonHeight),
logButton.bottomAnchor.constraint(equalTo: safeArea.bottomAnchor, constant: -spacing),
noteButton.topAnchor.constraint(equalTo: textView.bottomAnchor, constant: spacing),
noteButton.leadingAnchor.constraint(equalTo: safeArea.centerXAnchor, constant: spacing / 2),
noteButton.trailingAnchor.constraint(equalTo: safeArea.trailingAnchor, constant: -spacing),
noteButton.heightAnchor.constraint(equalToConstant: buttonHeight),
noteButton.bottomAnchor.constraint(equalTo: safeArea.bottomAnchor, constant: -spacing)
])
}
private func createBottomButton(title: String, color: UIColor) -> UIButton {
var config = UIButton.Configuration.filled()
config.title = title
config.baseBackgroundColor = color
config.baseForegroundColor = .white
config.cornerStyle = .medium
config.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 16, bottom: 0, trailing: 16)
let button = UIButton(configuration: config)
button.translatesAutoresizingMaskIntoConstraints = false
return button
}
@objc private func didTapLogButton() {
let viewController = FormatLogViewController()
viewController.initialText = TextFormatter.formatLog(initialText ?? "")
navigationController?.pushViewController(viewController, animated: true)
}
@objc private func didTapNoteButton() {
let viewController = FormatNoteViewController()
viewController.initialText = TextFormatter.formatForNote(initialText ?? "")
navigationController?.pushViewController(viewController, animated: true)
}
}