diff --git a/Note/FormatLogViewController.swift b/Note/FormatLogViewController.swift index deea6b1..9e6448e 100644 --- a/Note/FormatLogViewController.swift +++ b/Note/FormatLogViewController.swift @@ -40,6 +40,7 @@ class FormatLogViewController: UIViewController { formatButton.backgroundColor = .systemBlue formatButton.setTitleColor(.white, for: .normal) formatButton.layer.cornerRadius = 12 + formatButton.addTarget(self, action: #selector(didTapFormatButton), for: .touchUpInside) view.addSubview(formatButton) NSLayoutConstraint.activate([ @@ -58,6 +59,32 @@ class FormatLogViewController: UIViewController { @objc private func didTapDoneButton() { textView.resignFirstResponder() } + + @objc private func didTapFormatButton() { + textView.text = formatLog(textView.text) + } + + private func formatLog(_ text: String) -> String { + var content = text + + content = content.replacingOccurrences(of: "# ", with: "") + content = content.replacingOccurrences(of: "#", with: "") + content = content.replacingOccurrences(of: ";", with: "。") + content = content.replacingOccurrences(of: ";", with: "。") + content = content.replacingOccurrences(of: ".", with: "。") + content = content.replacingOccurrences(of: "(", with: "(") + content = content.replacingOccurrences(of: ")", with: ")") + content = content.replacingOccurrences(of: "?", with: "?") + content = content.replacingOccurrences(of: ",", with: ",") + content = content.replacingOccurrences(of: ":", with: ":") + content = content.replacingOccurrences(of: "日星期", with: "日 星期") + content = "# " + content + content = content.replacingOccurrences(of: "一、生活方面", with: "## 一、生活方面") + content = content.replacingOccurrences(of: "二、交易方面", with: "## 二、交易方面") + content = content.replacingOccurrences(of: "三、工作方面", with: "## 三、工作方面") + + return content + } } extension FormatLogViewController: UITextViewDelegate {