diff --git a/Note/FormatLogViewController.swift b/Note/FormatLogViewController.swift index ca5e2cf..de8394a 100644 --- a/Note/FormatLogViewController.swift +++ b/Note/FormatLogViewController.swift @@ -74,6 +74,10 @@ class FormatLogViewController: UIViewController { } @objc private func didTapFormatButton() { + guard !trimmedText().isEmpty else { + showEmptyAlert() + return + } if isFormatted { saveToFiles() } else { @@ -91,6 +95,16 @@ class FormatLogViewController: UIViewController { } } + private func trimmedText() -> String { + return textView.text?.trimmingCharacters(in: .whitespacesAndNewlines) ?? "" + } + + private func showEmptyAlert() { + let alert = UIAlertController(title: "提示", message: "请输入内容后再进行格式化", preferredStyle: .alert) + alert.addAction(UIAlertAction(title: "确定", style: .default)) + present(alert, animated: true) + } + @objc private func didTapResetButton() { textView.text = "" isFormatted = false diff --git a/Note/FormatNoteViewController.swift b/Note/FormatNoteViewController.swift index 29509a0..9bd658f 100644 --- a/Note/FormatNoteViewController.swift +++ b/Note/FormatNoteViewController.swift @@ -102,6 +102,16 @@ class FormatNoteViewController: UIViewController { } } + private func trimmedText() -> String { + return textView.text?.trimmingCharacters(in: .whitespacesAndNewlines) ?? "" + } + + private func showEmptyAlert() { + let alert = UIAlertController(title: "提示", message: "请输入内容后再进行格式化", preferredStyle: .alert) + alert.addAction(UIAlertAction(title: "确定", style: .default)) + present(alert, animated: true) + } + @objc private func didTapResetButton() { textView.text = "" rightBarButtonMode = .paste @@ -124,6 +134,10 @@ class FormatNoteViewController: UIViewController { } @objc private func didTapiCloudButton() { + guard !trimmedText().isEmpty else { + showEmptyAlert() + return + } if isCloudFormatted { saveToFiles() } else { @@ -136,6 +150,10 @@ class FormatNoteViewController: UIViewController { } @objc private func didTapNoteButton() { + guard !trimmedText().isEmpty else { + showEmptyAlert() + return + } textView.text = formatForNote(textView.text) rightBarButtonMode = .reset updateRightBarButtonItem()