SwiftUI。如何更改TextField的占位符颜色?

时间:2019-08-28 08:38:37

标签: swift swiftui

我想更改TextField的占位符颜色,但是找不到用于它的方法。

我尝试设置foregroundColoraccentColor,但不会更改占位符颜色。

代码如下:

TextField("Placeholder", $text)
    .foregroundColor(Color.red)
    .accentColor(Color.green)

也许还没有API?

5 个答案:

答案 0 :(得分:15)

还没有api。 但可以

使用import pygetwindow as gw Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/opt/rh/rh-python36/root/usr/lib/python3.6/site-packages/pygetwindow/__init__.py", line 42, in <module> raise NotImplementedError('PyGetWindow currently does not support Linux. If you have Xlib knowledge, please contribute! https://github.com/asweigart/pygetwindow') NotImplementedError: PyGetWindow currently does not support Linux. If you have Xlib knowledge, please contribute! https://github.com/asweigart/pygetwindow 并自己实现一个占位符:

ZStack

现在您可以像老板一样在其中进行任何编辑。

-自定义文本字段

您始终可以创建自己的自定义var body: some View { ZStack(alignment: .leading) { if text.isEmpty { Text("Placeholder").foregroundColor(.red) } TextField("", text: $text) } } 以便在任何地方使用:

View

使用struct CustomTextField: View { var placeholder: Text @Binding var text: String var editingChanged: (Bool)->() = { _ in } var commit: ()->() = { } var body: some View { ZStack(alignment: .leading) { if text.isEmpty { placeholder } TextField("", text: $text, onEditingChanged: editingChanged, onCommit: commit) } } } 和占位符)

TextField

答案 1 :(得分:15)

最终,将内容嵌入ZStack的ViewModifier更加优雅,代码更少:

public struct PlaceholderStyle: ViewModifier {
    var showPlaceHolder: Bool
    var placeholder: String

    public func body(content: Content) -> some View {
        ZStack(alignment: .leading) {
            if showPlaceHolder {
                Text(placeholder)
                .padding(.horizontal, 15)
            }
            content
            .foregroundColor(Color.white)
            .padding(5.0)            
        }
    }
}

用法:

TextField("", text: $data)
.modifier(PlaceholderStyle(showPlaceHolder: data.isEmpty,
                           placeholder: "My Placeholder"))

答案 2 :(得分:7)

这是对@jfk's answer的修改,我们可以为view创建一个扩展名,以简化主视图中的修改器代码,也可以将其用于Text和{{ 1}}。

Image

在TextField中的用法:

将此行代码struct PlaceHolder<T: View>: ViewModifier { var placeHolder: T var show: Bool func body(content: Content) -> some View { ZStack(alignment: .leading) { if show { placeHolder } content } } } extension View { func placeHolder<T:View>(_ holder: T, show: Bool) -> some View { self.modifier(PlaceHolder(placeHolder:holder, show: show)) } } 作为.placeHolder(Text("Your placeholder"), show: text.isEmpty)添加到viewModifier

TextField

图像用途:

此外,如@EmilioPelaez所建议,我修改了代码以支持占位符以显示ex的任何视图。 TextField("", text: $text, onEditingChanged: { (changing) in print("Changing: \(changing)") }, onCommit: { print("Committed!") }) .placeHolder(Text("Your placeholder"), show: text.isEmpty) ,如下所示。

Image

答案 3 :(得分:0)

如果您想保留原始的TextField,并且不介意将Introspect添加到项目(https://github.com/siteline/SwiftUI-Introspect),则可以通过访问UIKit attributedPlaceholder来做到这一点:

TextField("Email", text: $email)
.introspectTextField { uiTextField in
    uiTextField.attributedPlaceholder = NSAttributedString(string: "placeholder text", 
    attributes: [NSAttributedString.Key.foregroundColor: UIColor.red])
}

答案 4 :(得分:0)

选项 1:TL;DR 简答:

myUiTextField.attributedPlaceholder = NSAttributedString(string: placeHolderText, attributes: [NSAttributedString.Key.foregroundColor: placeHolderTextColor])
<块引用>

以上答案的解释: 使用其他社区成员在此处也建议的属性字符串。

选项 2:答案冗长但简洁且可扩展: 我们在项目中创建了一个扩展类,使用以下方法为一些参数设置默认值,以保持代码干净、可扩展和可重用:

    import UIKit

    extension UITextField {

        /// Set the placeholder text properties of a UITextField
        /// - Parameters:
        ///   - placeHolder: text message for placeholder
        ///   - foregroundColor: text color (optional)
        ///   - fontSize: text font size (optional)
        func placeHolderSetup(placeHolder: String, foregroundColor: UIColor = UIColor.AppColors.zGreyColor, fontSize: CGFloat = 16)  {
            let str = NSAttributedString(string: placeHolder,
                                     attributes:[NSAttributedString.Key.foregroundColor:foregroundColor,NSAttributedString.Key.font: UIFont(name: AppFonts.zFontRegular, size: fontSize)!])
            
             self.attributedPlaceholder = str;
         }
     }

上述方法的示例用法:

searchTextField?.placeHolderSetup(placeHolder: "Search", foregroundColor: UIColor.white, fontSize: 14)