Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put padding in a text editor so that text isn't directly on the edges(swiftUI)?

I am using a TextEditor and do not want the text to be directly touching the left side. I want it to look like most notes or document apps where the text is not crunched up on the top or sides. enter image description here

I tried to use a padding (as seen in the photo), but it shifted the actual TextEditor itself and not the text inside the editor. How can I pad the text but not the editor?

ZStack (alignment: .leading){
            
            if test.isEmpty{
                VStack{
                   
                    Text("Write something....")
                        .padding(.top)
                        .padding(.leading, 20)
                        .opacity(0.6)
                        .font(.system(size: 20))
                        .foregroundColor(.black)
                    Spacer()
                }
            } else{
                VStack{
                    Text("")
                }
            }
            
            VStack{
                TextEditor(text: $note.text)
                    .padding()
                    .opacity(note.text.isEmpty ? 0.85 : 1)
                    .font(.custom("SanFrancisco", fixedSize: 20))
                    .onReceive(note.publisher(for: \.text), perform: setName)
                    .onReceive(
                        note.publisher(for: \.text)
                            .debounce(for: 0.5, scheduler: RunLoop.main)
                            .removeDuplicates()
                    ){ _ in
                        try? PersistenceController.shared.saveContext()
                    }
                    .navigationTitle(note.name)
            }
        }
      
    }
like image 937
jari85 Avatar asked Oct 17 '25 12:10

jari85


1 Answers

Can you try this (iOS 16+)

 ZStack {
    TextEditor(text: $text)
    .padding()
  }
  .scrollContentBackground(.hidden)
  .background(Color.gray.opacity(0.1))
  .cornerRadius(15)

like image 189
Zim Avatar answered Oct 21 '25 04:10

Zim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!