格式化笔记实现 iCloud 和 Note 两种格式化逻辑
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user