Placeholder is not on the same line with text SwiftUI

325 Views Asked by At

I want to have a placeholder when there is no text in the texteditor, is working well, but the placeholder is not on the same line with the cursor of the TextEditor. I tried to replace the TextEditor with a textfield and it worked just fine, but I need a TextEditor. How can I put the TextEditor text and placeholder on the same line ? Thanks.

 ZStack(alignment: .topLeading) {
                VStack {
                    TextEditor(text: $currentText)
                        .rchFont(.rchBody, .primaryLighter)
                        .frame(maxWidth: .infinity, alignment: .leading)
                        .onChange(of: currentText) { value in
                            if value.count > 1000 {
                                currentText = oldText
                                return
                            }
                            oldText = currentText
                            currentCharCount = "\(value.count)"
                        }
                    
                    Spacer()
                    
                    Text("\(currentCharCount)/1000")
                        .rchFont(.rchBody, .primaryLighter)
                        .frame(maxWidth: .infinity, alignment: .trailing)
                    
                    RCHButtonPrimary(title: messageButtonTitle) {
                        text = currentText
                        presentationMode.wrappedValue.dismiss()
                    }
                }
                if currentText.isEmpty {
                    Text(placeholder)
                        .rchFont(.rchBody, .textSupport)
                        .frame(maxWidth: .infinity, alignment: .leading)
                }
            }
1

There are 1 best solutions below

0
Hongtron On

Hard to try and understand what you are trying to achieve as both views you present have .infinity but you seem to want to replace the TextEditor with the Text(placeholder). Ideally would need to see what is in the TextEditor view to understand what you say about the cursor not being on the same line.

Here is an example of what I referred to in my comment by swapping the field lower down in the view hierarchy which swaps just the base component, rather than a large view you are effectively covering up and hoping the positioning is the same.

import SwiftUI

struct TestView: View {
@State var isEditing: Bool = true

var body: some View {
    ZStack(alignment: .top) {
        VStack{
            Text("Text in Main View")
            EditorView(isEditing: $isEditing)
            Button("Change Editing") {
                isEditing.toggle()
            }
            HStack(alignment: .center) {
            Text("Some More Text in the View")
        }
        }
    }
}
}

struct EditorView: View {
@Binding var isEditing: Bool
var body: some View {
    ZStack {
        VStack{
            if isEditing {
                Text("Editing Here..............")
            } else {
                Text("Place Holder").foregroundColor(.red)
            }
        }
    }
}
}