From 193a44f47271c2fa9a8112cc1acd6e1779ddf82a Mon Sep 17 00:00:00 2001 From: fish Date: Sun, 12 Jul 2026 15:57:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96=E5=90=8E=E8=B7=B3?= =?UTF-8?q?=E8=BD=AC=E7=BB=93=E6=9E=9C=E9=A1=B5=E6=9F=A5=E7=9C=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Note/FormatterView.swift | 70 ++++++---------------------------- Note/ResultView.swift | 82 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 58 deletions(-) create mode 100644 Note/ResultView.swift diff --git a/Note/FormatterView.swift b/Note/FormatterView.swift index 4548921..c1fc0c7 100644 --- a/Note/FormatterView.swift +++ b/Note/FormatterView.swift @@ -9,14 +9,13 @@ import SwiftUI struct FormatterView: View { @State private var inputText: String = "" - @State private var outputText: String = "" - @State private var showCopied: Bool = false + @State private var resultText: String? var body: some View { NavigationStack { VStack(spacing: 16) { TextEditor(text: $inputText) - .frame(minHeight: 120) + .frame(minHeight: 160) .padding(8) .background(Color(.systemGray6)) .cornerRadius(8) @@ -25,7 +24,7 @@ struct FormatterView: View { .stroke(Color(.systemGray4), lineWidth: 1) ) - Button(action: formatText) { + Button(action: formatAndNavigate) { Label("格式化", systemImage: "wand.and.stars") .font(.headline) .frame(maxWidth: .infinity) @@ -35,55 +34,22 @@ struct FormatterView: View { .cornerRadius(10) } - if !outputText.isEmpty { - VStack(alignment: .leading, spacing: 8) { - HStack { - Text("格式化结果") - .font(.subheadline) - .foregroundStyle(.secondary) - Spacer() - Button(action: copyOutput) { - Image(systemName: "doc.on.doc") - } - } - - TextEditor(text: .constant(outputText)) - .frame(minHeight: 120) - .padding(8) - .background(Color(.systemGray6)) - .cornerRadius(8) - .overlay( - RoundedRectangle(cornerRadius: 8) - .stroke(Color(.systemGray4), lineWidth: 1) - ) - } - } - Spacer() } .padding() .navigationTitle("文案格式化") - .overlay { - if showCopied { - VStack { - Spacer() - Text("已复制") - .font(.subheadline) - .padding(.horizontal, 16) - .padding(.vertical, 8) - .background(Color.black.opacity(0.75)) - .foregroundStyle(.white) - .cornerRadius(20) - .padding(.bottom, 32) - } - .transition(.opacity) - } + .navigationDestination(item: $resultText) { text in + ResultView(formattedText: text) } } } - private func formatText() { - var content = inputText + private func formatAndNavigate() { + resultText = format(inputText) + } + + private func format(_ text: String) -> String { + var content = text // 去 # content = content.replacingOccurrences(of: "# ", with: "") @@ -113,19 +79,7 @@ struct FormatterView: View { content = content.replacingOccurrences(of: "二、交易方面", with: "## 二、交易方面") content = content.replacingOccurrences(of: "三、工作方面", with: "## 三、工作方面") - outputText = content - } - - private func copyOutput() { - UIPasteboard.general.string = outputText - withAnimation { - showCopied = true - } - DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) { - withAnimation { - showCopied = false - } - } + return content } } diff --git a/Note/ResultView.swift b/Note/ResultView.swift new file mode 100644 index 0000000..064b868 --- /dev/null +++ b/Note/ResultView.swift @@ -0,0 +1,82 @@ +// +// ResultView.swift +// Note +// +// Created by fish on 2026/7/12. +// + +import SwiftUI + +struct ResultView: View { + let formattedText: String + @Environment(\.dismiss) private var dismiss + @State private var showCopied: Bool = false + + var body: some View { + ScrollView { + VStack(alignment: .leading, spacing: 16) { + Text(formattedText) + .font(.body) + .lineSpacing(4) + .frame(maxWidth: .infinity, alignment: .leading) + .padding() + .background(Color(.systemGray6)) + .cornerRadius(12) + + Button(action: copyText) { + Label("复制结果", systemImage: "doc.on.doc") + .font(.headline) + .frame(maxWidth: .infinity) + .padding() + .background(Color.accentColor) + .foregroundStyle(.white) + .cornerRadius(10) + } + } + .padding() + } + .navigationTitle("格式化结果") + .navigationBarTitleDisplayMode(.inline) + .toolbar { + ToolbarItem(placement: .topBarTrailing) { + Button("完成") { + dismiss() + } + } + } + .overlay { + if showCopied { + VStack { + Spacer() + Text("已复制") + .font(.subheadline) + .padding(.horizontal, 16) + .padding(.vertical, 8) + .background(Color.black.opacity(0.75)) + .foregroundStyle(.white) + .cornerRadius(20) + .padding(.bottom, 32) + } + .transition(.opacity) + } + } + } + + private func copyText() { + UIPasteboard.general.string = formattedText + withAnimation { + showCopied = true + } + DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) { + withAnimation { + showCopied = false + } + } + } +} + +#Preview { + NavigationStack { + ResultView(formattedText: "# 示例文本") + } +}