From 15fc0f917115f5ab0ad64ebc440f3dfc6b5bb4a8 Mon Sep 17 00:00:00 2001 From: fish Date: Sun, 12 Jul 2026 21:31:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96=E7=AC=94=E8=AE=B0?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=20iCloud=20=E5=92=8C=20Note=20=E4=B8=A4?= =?UTF-8?q?=E7=A7=8D=E6=A0=BC=E5=BC=8F=E5=8C=96=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Note/FormatNoteViewController.swift | 85 +++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/Note/FormatNoteViewController.swift b/Note/FormatNoteViewController.swift index 78bdc80..dfa2168 100644 --- a/Note/FormatNoteViewController.swift +++ b/Note/FormatNoteViewController.swift @@ -121,14 +121,99 @@ class FormatNoteViewController: UIViewController { } @objc private func didTapiCloudButton() { + textView.text = formatForCloud(textView.text) rightBarButtonMode = .reset updateRightBarButtonItem() } @objc private func didTapNoteButton() { + textView.text = formatForNote(textView.text) rightBarButtonMode = .reset updateRightBarButtonItem() } + + private func formatForCloud(_ text: String) -> String { + var content = text + content = content.replacingOccurrences(of: " ", with: "") + content = content.replacingOccurrences(of: " ", with: "") + content = content.replacingOccurrences(of: "\u{00A0}", 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.replacingOccurrences(of: ",", with: ",") + content = content.replacingOccurrences(of: ":", with: ":") + + let numberReplacements: [(String, String)] = [ + ("20、", "二十、"), ("19、", "十九、"), ("18、", "十八、"), ("17、", "十七、"), + ("16、", "十六、"), ("15、", "十五、"), ("14、", "十四、"), ("13、", "十三、"), + ("12、", "十二、"), ("11、", "十一、"), ("10、", "十、"), + ("9、", "九、"), ("8、", "八、"), ("7、", "七、"), ("6、", "六、"), + ("5、", "五、"), ("4、", "四、"), ("3、", "三、"), ("2、", "二、"), ("1、", "一、") + ] + for (old, new) in numberReplacements { + content = content.replacingOccurrences(of: old, with: new) + } + + content = content.replacingOccurrences(of: "。", with: "。\n") + content = content.replacingOccurrences(of: "?", with: "?\n") + content = removeEmptyLines(content) + content = content.replacingOccurrences(of: ":\\s*", with: ":\n\n", options: .regularExpression) + content = content.replacingOccurrences(of: "。", with: "。\n") + content = content.replacingOccurrences(of: "?", with: "?\n") + content = content.replacingOccurrences(of: ":\n\n", with: ":") + content = content.replacingOccurrences(of: ":一、", with: ":\n\n一、") + content = content.replacingOccurrences(of: " ", with: "") + content = content.replacingOccurrences(of: "---", with: "---\n") + content = content.replacingOccurrences(of: "日星期", with: "日 星期") + content = "# " + content + content = insertEmptyLineAfterFirstLine(content) + return content + } + + private func formatForNote(_ 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.replacingOccurrences(of: ",", with: ",") + content = content.replacingOccurrences(of: ":", with: ":") + content = content.replacingOccurrences(of: "。", with: "。\n") + content = content.replacingOccurrences(of: "?", with: "?\n") + content = removeEmptyLines(content) + content = content.replacingOccurrences(of: "---", with: "") + content = content.replacingOccurrences(of: "日星期", with: "日 星期") + content = insertEmptyLineAfterFirstLine(content) + return content + } + + private func removeEmptyLines(_ text: String) -> String { + let lines = text.components(separatedBy: .newlines) + let nonEmptyLines = lines.filter { !$0.trimmingCharacters(in: .whitespaces).isEmpty } + return nonEmptyLines.joined(separator: "\n") + } + + private func insertEmptyLineAfterFirstLine(_ text: String) -> String { + var lines = text.components(separatedBy: .newlines) + if !lines.isEmpty { + lines.insert("", at: 1) + } + return lines.joined(separator: "\n") + } } extension FormatNoteViewController: UITextViewDelegate {