Swift - 从视图扩展中以编程方式更新绑定<String> 存储值

Swift - Updating Binding<String> stored value programmatically from View extension

所以我的目标是有一种更方便的方法来在 SwiftUI 的 TextEditor 上添加占位符文本值,因为似乎没有。我正在尝试的方法发现了一些我在 Binding<> 包装类型周围真正不理解的东西。 (也许这是一个危险信号,表明我正在做一些不推荐的事情?)

无论如何,关于我的问题:我们是否能够以编程方式更新 Binding 上的基础值?如果我接受一些 Binding<String> 值,我可以从我的方法中更新它吗?如果是这样,@State 发起者会引用更新后的值吗?下面的示例将我的占位符值作为文本放置在您单击它时我尝试输入的位置,如果我清除它甚至不会再次尝试。

从我前段时间找到的其他帖子导入此代码,使其在正文为空时显示占位符。

import Foundation
import SwiftUI

struct TextEditorViewThing: View {
  @State private var noteText = ""
  var body: some View {
    VStack{
      TextEditor(text: $noteText)
        .textPlaceholder(placeholder: "PLACEHOLDER", text: $noteText)
        .padding()
    }
  }
}

extension TextEditor {
  @ViewBuilder func textPlaceholder(placeholder: String, text: Binding<String>) -> some View {
    self.onAppear {
      // remove the placeholder text when keyboard appears
      NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillShowNotification, object: nil, queue: .main) { (noti) in
        withAnimation {
          if text.wrappedValue == placeholder {
            text.wrappedValue = placeholder
          }
        }
      }
      
      // 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 text.wrappedValue == "" {
            text.wrappedValue = placeholder
          }
        }
      }
    }
  }
}

根据您的要求自定义此设置:

struct ContentView: View {
    @State private var text: String = ""
    
    var body: some View {
        VStack {
            ZStack(alignment: .leading) {
                if self.text.isEmpty {
                    VStack {
                        Text("Placeholder Text")
                            .multilineTextAlignment(.leading)
                            .padding(.leading, 25)
                            .padding(.top, 8)
                            .opacity(0.5)
                        Spacer()
                    }
                }
                TextEditor(text: $text)
                    .padding(.leading, 20)
                    .opacity(self.text.isEmpty ? 0.5 : 1)
            }
            .frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height/2)
            .overlay(
                Rectangle().stroke()
                    .foregroundColor(Color.black)
                    .padding(.horizontal, 15)
            )
        }
    }
}