TextField

public struct TextField : Element

Displays a text field.

  • Declaration

    Swift

    public var text: String
  • Declaration

    Swift

    public var placeholder: String
  • Declaration

    Swift

    public var onChange: ((String) -> Void)?
  • Declaration

    Swift

    public var secure: Bool
  • Declaration

    Swift

    public var isEnabled: Bool
  • Declaration

    Swift

    public var textAlignment: NSTextAlignment
  • Declaration

    Swift

    public var font: UIFont
  • Declaration

    Swift

    public var textColor: UIColor
  • Declaration

    Swift

    public var clearButtonMode: UITextField.ViewMode
  • Declaration

    Swift

    public var keyboardType: UIKeyboardType
  • Declaration

    Swift

    public var keyboardAppearance: UIKeyboardAppearance
  • Declaration

    Swift

    public var autocapitalizationType: UITextAutocapitalizationType
  • Declaration

    Swift

    public var autocorrectionType: UITextAutocorrectionType
  • Declaration

    Swift

    public var spellCheckingType: UITextSpellCheckingType
  • Declaration

    Swift

    public var textContentType: UITextContentType?
  • Declaration

    Swift

    public var onReturn: (() -> Void)?
  • Declaration

    Swift

    public var returnKeyType: UIReturnKeyType
  • Declaration

    Swift

    public var enablesReturnKeyAutomatically: Bool
  • Declaration

    Swift

    public var focusBinding: FocusBinding?
  • A set of accessibility traits that should be applied to the field, these will be merged with any existing traits. These traits should relate to the content of the text, for example .header, .link, or .updatesFrequently.

    Declaration

    Swift

    public var accessibilityTraits: Set<AccessibilityElement.Trait>?
  • Declaration

    Swift

    public init(text: String, configure: (inout TextField) -> Void = { _ in })
  • Declaration

    Swift

    public func backingViewDescription(with context: ViewDescriptionContext) -> ViewDescription?
  • Declaration

    Swift

    public var content: ElementContent { get }
  • Modifies this text field by binding its focus state to the given state value.

    Use this modifier to cause the text field to receive focus whenever the state equals the value. Typically, you create an enumeration of fields that may receive focus, bind an instance of this enumeration, and assign its cases to focusable views.

    The following example uses the cases of a LoginForm enumeration to bind the focus state of two TextField elements. A sign-in button validates the fields and sets the bound focusedField value to any field that requires the user to correct a problem.

    struct LoginForm: ProxyElement {
        enum Field: Hashable {
            case username
            case password
        }
    
        var username: String
        var password: String
        var handleLogin: () -> Void
    
        @FocusState private var focusedField: Field?
    
        var elementRepresentation: Element {
            Column { column in
                column.add(
                    child: TextField(text: "")
                        .focused(when: $focusedField, equals: .username)
                )
    
                column.add(
                    child: TextField(text: "")
                        .focused(when: $focusedField, equals: .password)
                )
    
                column.add(
                    child: Button(
                        onTap: {
                            if username.isEmpty {
                                focusedField = .username
                            } else if password.isEmpty {
                                focusedField = .password
                            } else {
                                handleLogin()
                            }
                        },
                        wrapping: Label(text: "Sign In")
                    )
                )
            }
        }
    }
    

    To control focus using a Boolean, use the focused(when:) method instead.

    Declaration

    Swift

    public func focused<Value>(
        when state: FocusState<Value?>,
        equals value: Value
    ) -> Self

    Parameters

    state

    The state to bind to.

    value

    The value to match against when determining whether the binding should change.

    Return Value

    A modified text field.

  • Modifies this text field by binding its focus state to the given Boolean state value.

    Use this modifier to cause the text field to receive focus whenever the the condition is true. You can use this modifier to observe the focus state of a text field, or programmatically focus or blur the field.

    In the following example, a single text field accepts a user’s desired username. The text field binds its focus state to the Boolean value isUsernameFocused. A “Submit” button’s action checks if the username is empty, and sets isUsernameFocused to true, which causes focus to return to the text field.

    struct SignupForm: ProxyElement {
        var username: String
        var onSignUpTapped: () -> Void
    
        @FocusState var isUsernameFocused: Bool
    
        var elementRepresentation: Element {
            Column { column in
                column.add(
                    child: TextField(text: username)
                        .focused(when: $isUsernameFocused)
                )
    
                column.add(
                    child: Button(
                        onTap: {
                            if username.isEmpty {
                                isUsernameFocused = true
                            } else {
                                onSignUpTapped()
                            }
                        },
                        wrapping: Label(text: "Submit")
                    )
                )
            }
        }
    }
    

    To control focus by matching a value, use the focused(when:equals:) method instead.

    Declaration

    Swift

    public func focused(when condition: FocusState<Bool>) -> TextField

    Parameters

    condition

    The state to bind to.

    Return Value

    A modified text field.