优化键盘收起:支持点击标题栏和空白区域

This commit is contained in:
2026-07-12 16:07:14 +08:00
parent bfc43964d8
commit aa45062bd7
+76 -39
View File
@@ -15,40 +15,13 @@ struct FormatterView: View {
var body: some View {
NavigationStack {
VStack(spacing: 16) {
TextEditor(text: $inputText)
.frame(minHeight: 160)
.padding(8)
.background(Color(.systemBackground))
.cornerRadius(8)
.overlay(
RoundedRectangle(cornerRadius: 8)
.stroke(Color(.systemGray4), lineWidth: 1)
)
VStack(spacing: 0) {
headerView
.background(Color(hex: "f2f2f7"))
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))
ScrollView {
VStack(spacing: 16) {
TextEditor(text: $inputText)
.frame(minHeight: 160)
.padding(8)
.background(Color(.systemBackground))
@@ -57,16 +30,58 @@ struct FormatterView: View {
RoundedRectangle(cornerRadius: 8)
.stroke(Color(.systemGray4), lineWidth: 1)
)
}
}
.toolbar {
ToolbarItemGroup(placement: .keyboard) {
Spacer()
Button("完成") {
dismissKeyboard()
}
}
}
Spacer()
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: 160)
.padding(8)
.background(Color(.systemBackground))
.cornerRadius(8)
.overlay(
RoundedRectangle(cornerRadius: 8)
.stroke(Color(.systemGray4), lineWidth: 1)
)
}
}
Spacer(minLength: 16)
}
.padding()
}
.background(Color(hex: "f2f2f7"))
}
.padding()
.navigationTitle("文案格式化")
.background(Color(hex: "f2f2f7").ignoresSafeArea())
.onTapGesture {
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
dismissKeyboard()
}
.alert("提示", isPresented: $showEmptyAlert) {
Button("确定", role: .cancel) {}
@@ -89,9 +104,31 @@ struct FormatterView: View {
.transition(.opacity)
}
}
.navigationBarHidden(true)
}
}
private var headerView: some View {
HStack {
Text("文案格式化")
.font(.largeTitle.bold())
.foregroundStyle(.primary)
Spacer()
}
.frame(maxWidth: .infinity, minHeight: 44)
.padding(.horizontal)
.padding(.top, 8)
.padding(.bottom, 8)
.contentShape(Rectangle())
.onTapGesture {
dismissKeyboard()
}
}
private func dismissKeyboard() {
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
}
private func formatText() {
if inputText.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
showEmptyAlert = true