格式化结果当前页展示并移除结果页
This commit is contained in:
+60
-12
@@ -9,8 +9,9 @@ import SwiftUI
|
|||||||
|
|
||||||
struct FormatterView: View {
|
struct FormatterView: View {
|
||||||
@State private var inputText: String = ""
|
@State private var inputText: String = ""
|
||||||
@State private var resultText: String?
|
@State private var outputText: String = ""
|
||||||
@State private var showEmptyAlert: Bool = false
|
@State private var showEmptyAlert: Bool = false
|
||||||
|
@State private var showCopied: Bool = false
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
NavigationStack {
|
NavigationStack {
|
||||||
@@ -18,14 +19,14 @@ struct FormatterView: View {
|
|||||||
TextEditor(text: $inputText)
|
TextEditor(text: $inputText)
|
||||||
.frame(minHeight: 160)
|
.frame(minHeight: 160)
|
||||||
.padding(8)
|
.padding(8)
|
||||||
.background(Color(.systemGray6))
|
.background(Color(.systemBackground))
|
||||||
.cornerRadius(8)
|
.cornerRadius(8)
|
||||||
.overlay(
|
.overlay(
|
||||||
RoundedRectangle(cornerRadius: 8)
|
RoundedRectangle(cornerRadius: 8)
|
||||||
.stroke(Color(.systemGray4), lineWidth: 1)
|
.stroke(Color(.systemGray4), lineWidth: 1)
|
||||||
)
|
)
|
||||||
|
|
||||||
Button(action: formatAndNavigate) {
|
Button(action: formatText) {
|
||||||
Label("格式化", systemImage: "wand.and.stars")
|
Label("格式化", systemImage: "wand.and.stars")
|
||||||
.font(.headline)
|
.font(.headline)
|
||||||
.frame(maxWidth: .infinity)
|
.frame(maxWidth: .infinity)
|
||||||
@@ -35,6 +36,30 @@ struct FormatterView: View {
|
|||||||
.cornerRadius(10)
|
.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: 160)
|
||||||
|
.padding(8)
|
||||||
|
.background(Color(.systemBackground))
|
||||||
|
.cornerRadius(8)
|
||||||
|
.overlay(
|
||||||
|
RoundedRectangle(cornerRadius: 8)
|
||||||
|
.stroke(Color(.systemGray4), lineWidth: 1)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Spacer()
|
Spacer()
|
||||||
}
|
}
|
||||||
.padding()
|
.padding()
|
||||||
@@ -43,27 +68,38 @@ struct FormatterView: View {
|
|||||||
.onTapGesture {
|
.onTapGesture {
|
||||||
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
|
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
|
||||||
}
|
}
|
||||||
.navigationDestination(item: $resultText) { text in
|
|
||||||
ResultView(formattedText: text)
|
|
||||||
}
|
|
||||||
.alert("提示", isPresented: $showEmptyAlert) {
|
.alert("提示", isPresented: $showEmptyAlert) {
|
||||||
Button("确定", role: .cancel) {}
|
Button("确定", role: .cancel) {}
|
||||||
} message: {
|
} message: {
|
||||||
Text("请输入需要格式化的文案")
|
Text("请输入需要格式化的文案")
|
||||||
}
|
}
|
||||||
|
.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 formatAndNavigate() {
|
private func formatText() {
|
||||||
if inputText.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
|
if inputText.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
|
||||||
showEmptyAlert = true
|
showEmptyAlert = true
|
||||||
|
outputText = ""
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
resultText = format(inputText)
|
|
||||||
}
|
|
||||||
|
|
||||||
private func format(_ text: String) -> String {
|
var content = inputText
|
||||||
var content = text
|
|
||||||
|
|
||||||
// 去 #
|
// 去 #
|
||||||
content = content.replacingOccurrences(of: "# ", with: "")
|
content = content.replacingOccurrences(of: "# ", with: "")
|
||||||
@@ -93,7 +129,19 @@ struct FormatterView: View {
|
|||||||
content = content.replacingOccurrences(of: "二、交易方面", with: "## 二、交易方面")
|
content = content.replacingOccurrences(of: "二、交易方面", with: "## 二、交易方面")
|
||||||
content = content.replacingOccurrences(of: "三、工作方面", with: "## 三、工作方面")
|
content = content.replacingOccurrences(of: "三、工作方面", with: "## 三、工作方面")
|
||||||
|
|
||||||
return content
|
outputText = content
|
||||||
|
}
|
||||||
|
|
||||||
|
private func copyOutput() {
|
||||||
|
UIPasteboard.general.string = outputText
|
||||||
|
withAnimation {
|
||||||
|
showCopied = true
|
||||||
|
}
|
||||||
|
DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
|
||||||
|
withAnimation {
|
||||||
|
showCopied = false
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,83 +0,0 @@
|
|||||||
//
|
|
||||||
// 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)
|
|
||||||
.background(Color(hex: "f2f2f7").ignoresSafeArea())
|
|
||||||
.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: "# 示例文本")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user