How can I add a Button() inside of TextEditor() in SwiftUI?

36 Views Asked by At

I'd like try be able to add buttons inside of TextEditor() or a custom text editor. Is this possible with SwiftUI?

1

There are 1 best solutions below

0
miltenkot On

The easiest way is by using a ZStack e.g.

struct ContentView: View {
    @State private var text: String = "Type your text here..."

    var body: some View {
        ZStack(alignment: Alignment(horizontal: .trailing, vertical: .top)) {
            TextEditor(text: $text)
                .padding()
                .border(Color.gray, width: 1)

            Button(action: {
                // Action for the button
                print("Button was tapped")
            }) {
                Image(systemName: "plus.circle.fill")
                    .font(.title)
                    .padding([.top, .trailing], 20)
            }
        }
    }
}