diff --git a/Note/FormatLogViewController.swift b/Note/FormatLogViewController.swift index adaed68..ca5e2cf 100644 --- a/Note/FormatLogViewController.swift +++ b/Note/FormatLogViewController.swift @@ -12,7 +12,15 @@ class FormatLogViewController: UIViewController { private var textView: UITextView! private var formatButton: UIButton! private var doneButton: UIBarButtonItem! + private var pasteButton: UIBarButtonItem! + private var resetButton: UIBarButtonItem! private var isFormatted = false + private var rightBarButtonMode: RightBarButtonMode = .paste + + private enum RightBarButtonMode { + case paste + case reset + } override func viewDidLoad() { super.viewDidLoad() @@ -33,6 +41,10 @@ class FormatLogViewController: UIViewController { view.addSubview(textView) doneButton = UIBarButtonItem(title: "完成", style: .prominent, target: self, action: #selector(didTapDoneButton)) + pasteButton = UIBarButtonItem(title: "粘贴文案", style: .plain, target: self, action: #selector(didTapPasteButton)) + resetButton = UIBarButtonItem(title: "重置", style: .plain, target: self, action: #selector(didTapResetButton)) + + updateRightBarButtonItem() formatButton = UIButton(type: .system) formatButton.translatesAutoresizingMaskIntoConstraints = false @@ -68,6 +80,35 @@ class FormatLogViewController: UIViewController { textView.text = formatLog(textView.text) isFormatted = true formatButton.setTitle("保存到 iCloud", for: .normal) + rightBarButtonMode = .reset + updateRightBarButtonItem() + } + } + + @objc private func didTapPasteButton() { + if let string = UIPasteboard.general.string { + textView.text = string + } + } + + @objc private func didTapResetButton() { + textView.text = "" + isFormatted = false + formatButton.setTitle("格式化", for: .normal) + rightBarButtonMode = .paste + updateRightBarButtonItem() + } + + private func updateRightBarButtonItem() { + if textView.isFirstResponder { + navigationItem.rightBarButtonItem = doneButton + } else { + switch rightBarButtonMode { + case .paste: + navigationItem.rightBarButtonItem = pasteButton + case .reset: + navigationItem.rightBarButtonItem = resetButton + } } } @@ -124,11 +165,11 @@ class FormatLogViewController: UIViewController { extension FormatLogViewController: UITextViewDelegate { func textViewDidBeginEditing(_ textView: UITextView) { - navigationItem.rightBarButtonItem = doneButton + updateRightBarButtonItem() } func textViewDidEndEditing(_ textView: UITextView) { - navigationItem.rightBarButtonItem = nil + updateRightBarButtonItem() } }