You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SwiftUI中如何在TextEditor()内添加Button()?

在SwiftUI的TextEditor中添加按钮的实现方案

完全可行,以下是几种常见的实现方式,根据你的需求选择即可:

1. ZStack叠加按钮(快速实现)

通过ZStack将按钮直接叠加在TextEditor上方,适合简单的悬浮按钮场景,注意调整按钮位置避免遮挡输入区域:

struct EditorWithButtonView: View {
    @State private var text = ""
    
    var body: some View {
        ZStack(alignment: .bottomTrailing) {
            TextEditor(text: $text)
                .padding()
                .background(Color(.systemGray6))
                .cornerRadius(8)
            
            Button(action: {
                // 自定义按钮逻辑,比如插入预设文本
                text += "[快速插入内容]"
            }) {
                Image(systemName: "plus.circle.fill")
                    .foregroundColor(.blue)
                    .font(.title)
            }
            .padding(16)
        }
        .padding()
    }
}

2. 自定义编辑器(深度集成)

如果需要更灵活的布局(比如按钮嵌入编辑器内部),可以用UIViewRepresentable包装UITextView,直接添加子视图按钮:

struct CustomEditorView: UIViewRepresentable {
    @Binding var text: String
    
    func makeUIView(context: Context) -> UITextView {
        let textView = UITextView()
        textView.backgroundColor = UIColor.systemGray6
        textView.textContainerInset = UIEdgeInsets(top: 16, left: 16, bottom: 16, right: 16)
        
        // 创建并添加按钮
        let insertButton = UIButton(type: .system)
        insertButton.setImage(UIImage(systemName: "plus.circle.fill"), for: .normal)
        insertButton.tintColor = .blue
        insertButton.addTarget(context.coordinator, action: #selector(Coordinator.insertText), for: .touchUpInside)
        
        textView.addSubview(insertButton)
        insertButton.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            insertButton.trailingAnchor.constraint(equalTo: textView.trailingAnchor, constant: -16),
            insertButton.bottomAnchor.constraint(equalTo: textView.bottomAnchor, constant: -16),
            insertButton.widthAnchor.constraint(equalToConstant: 44),
            insertButton.heightAnchor.constraint(equalToConstant: 44)
        ])
        
        textView.delegate = context.coordinator
        return textView
    }
    
    func updateUIView(_ uiView: UITextView, context: Context) {
        if uiView.text != text {
            uiView.text = text
        }
    }
    
    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }
    
    class Coordinator: NSObject, UITextViewDelegate {
        var parent: CustomEditorView
        
        init(_ parent: CustomEditorView) {
            self.parent = parent
        }
        
        @objc func insertText() {
            parent.text += "[自定义插入内容]"
        }
        
        func textViewDidChange(_ textView: UITextView) {
            parent.text = textView.text
        }
    }
}

// 使用示例
struct ContentView: View {
    @State private var text = ""
    
    var body: some View {
        CustomEditorView(text: $text)
            .cornerRadius(8)
            .padding()
            .frame(height: 200)
    }
}

3. 键盘工具栏按钮

如果希望按钮出现在键盘顶部,直接用toolbar修饰符即可:

struct EditorWithToolbarButton: View {
    @State private var text = ""
    
    var body: some View {
        TextEditor(text: $text)
            .padding()
            .background(Color(.systemGray6))
            .cornerRadius(8)
            .toolbar {
                ToolbarItem(placement: .keyboard) {
                    Button(action: {
                        text += "[键盘插入内容]"
                    }) {
                        Label("插入", systemImage: "plus")
                    }
                }
            }
            .padding()
    }
}

内容的提问来源于stack exchange,提问作者Justikun

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 09:04:56