How to add placeholder text to TextEditor in SwiftUI?

后端 未结 9 2151
逝去的感伤
逝去的感伤 2021-02-14 01:35

When using SwiftUI\'s new TextEditor, you can modify its content directly using a @State. However, I haven\'t see a way to add a placeholder text to it. Is it doable right now?<

相关标签:
9条回答
  • 2021-02-14 01:55

    You can use a ZStack with a disabled TextEditor containing your placeholder text behind. For example:

    ZStack {
        if self.content.isEmpty {
                TextEditor(text:$placeholderText)
                    .font(.body)
                    .foregroundColor(.gray)
                    .disabled(true)
                    .padding()
        }
        TextEditor(text: $content)
            .font(.body)
            .opacity(self.content.isEmpty ? 0.25 : 1)
            .padding()
    }
    
    0 讨论(0)
  • 2021-02-14 01:57

    Until we have some API support, an option would be to use the binding string as placeholder and onTapGesture to remove it

    TextEditor(text: self.$note)
                    .padding(.top, 20)
                    .foregroundColor(self.note == placeholderString ? .gray : .primary)
                    .onTapGesture {
                        if self.note == placeholderString {
                            self.note = ""
                        }
                    }
    
    0 讨论(0)
  • 2021-02-14 01:58

    I like Umayanga's approach but his code wasn't reusable. Here's the code as a reusable view:

    struct TextEditorPH: View {
        
        private var placeholder: String
        @Binding var text: String
        
        init(placeholder: String, text: Binding<String>) {
            self.placeholder = placeholder
            self._text = text
        }
        
        var body: some View {
            TextEditor(text: self.$text)
                // make the color of the placeholder gray
                .foregroundColor(self.text == placeholder ? .gray : .primary)
                
                .onAppear {
                    // create placeholder
                    self.text = placeholder
    
                    // remove the placeholder text when keyboard appears
                    NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillShowNotification, object: nil, queue: .main) { (noti) in
                        withAnimation {
                            if self.text == placeholder {
                                self.text = ""
                            }
                        }
                    }
                    
                    // put back the placeholder text if the user dismisses the keyboard without adding any text
                    NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillHideNotification, object: nil, queue: .main) { (noti) in
                        withAnimation {
                            if self.text == "" {
                                self.text = placeholder
                            }
                        }
                    }
                }
        }
    }
    
    0 讨论(0)
提交回复
热议问题