添加文案格式化页面

This commit is contained in:
2026-07-12 15:41:53 +08:00
parent 4b3499d73d
commit cb65163e5c
2 changed files with 135 additions and 1 deletions
+134
View File
@@ -0,0 +1,134 @@
//
// FormatterView.swift
// Note
//
// Created by fish on 2026/7/12.
//
import SwiftUI
struct FormatterView: View {
@State private var inputText: String = ""
@State private var outputText: String = ""
@State private var showCopied: Bool = false
var body: some View {
NavigationStack {
VStack(spacing: 16) {
TextEditor(text: $inputText)
.frame(minHeight: 120)
.padding(8)
.background(Color(.systemGray6))
.cornerRadius(8)
.overlay(
RoundedRectangle(cornerRadius: 8)
.stroke(Color(.systemGray4), lineWidth: 1)
)
Button(action: formatText) {
Label("格式化", systemImage: "wand.and.stars")
.font(.headline)
.frame(maxWidth: .infinity)
.padding()
.background(Color.accentColor)
.foregroundStyle(.white)
.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)
}
}
}
}
private func formatText() {
var content = inputText
// #
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
//
content = content.replacingOccurrences(of: "一、生活方面", with: "## 一、生活方面")
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
}
}
}
}
#Preview {
FormatterView()
}
+1 -1
View File
@@ -11,7 +11,7 @@ import SwiftUI
struct NoteApp: App {
var body: some Scene {
WindowGroup {
ContentView()
FormatterView()
}
}
}