'Remove padding on TextEditor

My custom text editor below once you click on the pen to edit, a new space appears so the text from before is not on the same line as the new one. How can I fix this? Here's a simple reproducible example:

struct SwiftUIView: View {
    
    @State var name: String = "test"
    @State var showEdit: Bool = true
    
    var body: some View {
        HStack {
            HStack {
                if(showEdit) {
                    CustomTextEditor.init(placeholder: "My unique name", text: $name)
                        .font(.headline)
                } else {
                    Text(name)
                        .font(.headline)
                }
            }
            
            Spacer()
            
            Button(action: {
                showEdit.toggle()
            }) {
                Image(systemName: "pencil")
                    .foregroundColor(.secondary)
            }
        }
    }
}

struct CustomTextEditor: View {
    let placeholder: String
    
    @Binding var text: String
    
    var body: some View {
        ZStack {
            if text.isEmpty  {
                Text(placeholder)
                    .foregroundColor(Color.primary.opacity(0.25))
            }
            TextEditor(text: $text)
        }.onAppear() {
            UITextView.appearance().backgroundColor = .clear
        }.onDisappear() {
            UITextView.appearance().backgroundColor = nil
        }
    }
}

I want it to have the same padding properies as inserting a simple Text("") so when I switch between Text("xyz") and TextEditor(text: $xyz) it has the same padding alignment. Right now TextEditor has a weird padding.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source