textField.style(_:)

// 🌀 TextField + .style()
extension TextField {
    public func style<S>(_ style: S) -> some View where S: TextFieldStyle {
        textFieldStyle(style)
    }
}

// content view
struct ContentView: View {
    @State private var text = "hello"
    var body: some View {
        VStack {
            TextField("hello", text: $text)
                // ⭐️ TextField 可使用 custom method,使語意更簡潔
                .style(.plain)
            TextField("hello", text: $text)
        }
        // ⭐️ parent view 使用 .textFieldStyle() 才不會失去 contextim
        .textFieldStyle(.roundedBorder)
    }
}

Last updated

Was this helpful?