输入框占满屏幕,格式化结果直接回填

This commit is contained in:
2026-07-12 16:59:11 +08:00
parent 09a202324e
commit 252f8a294f
+69 -51
View File
@@ -9,21 +9,19 @@ import SwiftUI
struct FormatterView: View { struct FormatterView: View {
@State private var inputText: String = "" @State private var inputText: String = ""
@State private var outputText: String = ""
@State private var showEmptyAlert: Bool = false @State private var showEmptyAlert: Bool = false
@State private var showCopied: Bool = false @State private var showCopied: Bool = false
var body: some View { var body: some View {
NavigationStack { NavigationStack {
List { VStack(spacing: 0) {
inputSection headerView
actionSection
if !outputText.isEmpty { inputArea
resultSection .frame(maxHeight: .infinity)
}
actionArea
} }
.listStyle(.insetGrouped)
.navigationTitle("文案格式化")
.background(Color(.systemGroupedBackground).ignoresSafeArea()) .background(Color(.systemGroupedBackground).ignoresSafeArea())
.alert("提示", isPresented: $showEmptyAlert) { .alert("提示", isPresented: $showEmptyAlert) {
Button("确定", role: .cancel) {} Button("确定", role: .cancel) {}
@@ -46,15 +44,51 @@ struct FormatterView: View {
.transition(.opacity) .transition(.opacity)
} }
} }
.navigationBarHidden(true)
} }
} }
private var inputSection: some View { private var headerView: some View {
Section { HStack {
Text("文案格式化")
.font(.largeTitle.bold())
.foregroundStyle(.primary)
Spacer()
if !inputText.isEmpty {
Button(action: copyText) {
Image(systemName: "doc.on.doc")
}
.font(.title3)
}
}
.frame(maxWidth: .infinity, minHeight: 44)
.padding(.horizontal)
.padding(.top, 8)
.padding(.bottom, 8)
.background(Color(.systemGroupedBackground))
.contentShape(Rectangle())
.onTapGesture {
dismissKeyboard()
}
}
private var inputArea: some View {
VStack(alignment: .leading, spacing: 8) {
Text("输入")
.font(.subheadline)
.foregroundStyle(.secondary)
.padding(.horizontal)
TextEditor(text: $inputText) TextEditor(text: $inputText)
.frame(minHeight: 160) .frame(maxWidth: .infinity, maxHeight: .infinity)
.scrollContentBackground(.hidden) .padding(12)
.background(Color(.secondarySystemGroupedBackground)) .background(Color(.systemBackground))
.cornerRadius(12)
.overlay(
RoundedRectangle(cornerRadius: 12)
.stroke(Color(.systemGray4), lineWidth: 1)
)
.padding(.horizontal)
.toolbar { .toolbar {
ToolbarItemGroup(placement: .keyboard) { ToolbarItemGroup(placement: .keyboard) {
Spacer() Spacer()
@@ -63,42 +97,27 @@ struct FormatterView: View {
} }
} }
} }
} header: {
Text("输入")
} footer: {
Text("粘贴需要格式化的文案") Text("粘贴需要格式化的文案")
}
}
private var actionSection: some View {
Section {
Button(action: formatText) {
HStack {
Spacer()
Label("格式化", systemImage: "wand.and.stars")
.font(.headline)
Spacer()
}
}
}
}
private var resultSection: some View {
Section {
TextEditor(text: .constant(outputText))
.frame(minHeight: 160)
.scrollContentBackground(.hidden)
.background(Color(.secondarySystemGroupedBackground))
} header: {
HStack {
Text("格式化结果")
Spacer()
Button(action: copyOutput) {
Image(systemName: "doc.on.doc")
}
.font(.subheadline) .font(.subheadline)
} .foregroundStyle(.secondary)
.padding(.horizontal)
} }
.padding(.vertical, 8)
}
private var actionArea: some View {
Button(action: formatText) {
Label("格式化", systemImage: "wand.and.stars")
.font(.headline)
.frame(maxWidth: .infinity)
.padding()
.background(Color(.systemBackground))
.foregroundStyle(Color.accentColor)
.cornerRadius(12)
}
.padding()
.background(Color(.systemGroupedBackground))
} }
private func dismissKeyboard() { private func dismissKeyboard() {
@@ -108,7 +127,6 @@ struct FormatterView: View {
private func formatText() { private func formatText() {
if inputText.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty { if inputText.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
showEmptyAlert = true showEmptyAlert = true
outputText = ""
return return
} }
@@ -142,12 +160,12 @@ struct FormatterView: View {
content = content.replacingOccurrences(of: "二、交易方面", with: "## 二、交易方面") content = content.replacingOccurrences(of: "二、交易方面", with: "## 二、交易方面")
content = content.replacingOccurrences(of: "三、工作方面", with: "## 三、工作方面") content = content.replacingOccurrences(of: "三、工作方面", with: "## 三、工作方面")
outputText = content inputText = content
dismissKeyboard() dismissKeyboard()
} }
private func copyOutput() { private func copyText() {
UIPasteboard.general.string = outputText UIPasteboard.general.string = inputText
withAnimation { withAnimation {
showCopied = true showCopied = true
} }