【问题标题】:How Can I Adjust TextField Placeholder Color : SwiftUI如何调整 TextField 占位符颜色:SwiftUI
【发布时间】:2020-08-03 20:59:00
【问题描述】:

我发现我可以像这样在swift中自定义TextField样式..

struct BottomLineTextFieldStyle: TextFieldStyle {
    func _body(configuration: TextField<Self._Label>) -> some View {
        VStack() {
            configuration

            Rectangle()
                .frame(height: 1, alignment: .bottom)
                .foregroundColor(Color.white)
        }
    }
}

这允许我使用“下划线形状样式”文本字段。

用法

TextField("placeholder", text: $value).textFieldStyle(BottomLineTextFieldStyle()).foregroundColor(.white)

但是我也想更改占位符颜色,但我不能。

那么,我的问题是如何自定义文本字段的占位符颜色? 请帮助我,感谢您的阅读。

【问题讨论】:

    标签: ios swift swiftui textfield


    【解决方案1】:

    您还不能更改占位符的默认颜色。但是,您可以通过使用UITextFieldUITextView 编写自定义TextField 来实现它。这是一个示例,我使用UITextView 创建了一个自定义TextArea 视图,我可以在其中为占位符设置自定义颜色。在makeUIView(_:)中查看我的评论

    struct TextArea: UIViewRepresentable {
        @State var placeholder: String
        @Binding var text: String
    
        func makeCoordinator() -> Coordinator {
            Coordinator(self, placeholder: placeholder)
        }
    
        func makeUIView(context: Context) -> UITextView {
            let textView = UITextView()
            textView.text = placeholder
    
            // Here you can set the color for placeholder text as per your choice. 
            textView.textColor = .lightGray
    
            textView.delegate = context.coordinator
            return textView
        }
    
        func updateUIView(_ textView: UITextView, context: Context) {
            if !text.isEmpty {
                textView.text = text
                textView.textColor = .black
            }
        }
    
        class Coordinator: NSObject, UITextViewDelegate {
            var textArea: TextArea
            var placeholder: String
    
            init(_ textArea: TextArea, placeholder: String) {
                self.textArea = textArea
                self.placeholder = placeholder
            }
    
            func textViewDidBeginEditing(_ textView: UITextView) {
                if textView.textColor == .lightGray {
                    textView.text = nil
                    textView.textColor = .black
                }
            }
    
            func textViewDidEndEditing(_ textView: UITextView) {
                if textView.text.isEmpty {
                    textView.text = placeholder
                    textView.textColor = UIColor.lightGray
                }
            }
        }
    }
    

    编辑:

    上面的文本区域可以像这样在视图中使用:

     TextArea(placeholder: textValue, text: $textValue)
    

    【讨论】:

    • 我明白了。谢谢。我怎样才能使用这个文本区域?你能给我举个例子吗?
    • 如果我想使用“下划线”形状文本字段而不是框形状,我必须在哪里编辑此代码?
    • 您可以编辑 TextArea 的 makeUIView 方法以根据您的特定需求返回 UITedtField。请注意,这里使用的是 UIKit 元素而不是 SwiftUI 元素。你可以在这里阅读更多信息:developer.apple.com/tutorials/swiftui/interfacing-with-uikit
    【解决方案2】:

    那么,我的问题是如何自定义文本字段的占位符颜色?

    很遗憾,目前无法更改标准 TextField 占位符的颜色。但是可以以某种方式对其进行修改,因此,我认为值得发布

    在下面的演示中,TextField 颜色已修改,密码是默认的。

    struct BottomLineTextFieldStyle: TextFieldStyle {
        func _body(configuration: TextField<Self._Label>) -> some View {
            VStack() {
                configuration
                    .colorMultiply(.red)
                    .foregroundColor(.red)
    
                Rectangle()
                    .frame(height: 1, alignment: .bottom)
                    .foregroundColor(.red)
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-06
      • 2022-11-08
      • 2012-11-08
      • 2020-02-16
      • 2020-04-20
      • 2015-06-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多