UITextField

extension UITextField
  • A convenience method wrapping a typical use for UITextField’s UIControlEvents.editingChanged event. This will send the new value text after it has been modified.


    An example of calling this method:

    myTextField.onChange { newText in
        <#text changed code#>
    }
    

    Note

    This callback provides a non-optional String, even though UITextField.text can be nil. For that reason, it is technically not exactly the text in the text property, but rather a convenience so you don’t have to unwrap it. If nil is an important distinction for you to have, please use the text field’s text property directly or use on(_:handler:) and obtain the text property from the sender parameter.

    Declaration

    Swift

    @discardableResult
    public func onChange(handler: @escaping (_ text: String) -> Void) -> Self

    Parameters

    handler

    The closure to be called on the text changed event

    Return Value

    itself so you can daisy chain the other event handler calls

  • A convenience method to respond to UIControlEvents.editingDidEnd for a UITextField. This occurs when the UITextField resigns as first responder.

    Declaration

    Swift

    @discardableResult
    public func onEditingEnded(handler: @escaping () -> Void) -> Self

    Parameters

    handler

    The closure to be called when the focus leaves the text field

    Return Value

    itself so you can daisy chain the other event handler calls

  • A convenience method to respond to UIControlEvents.editingDidBegin for a UITextField. This occurs when the UITextField becomes the first responder.

    Declaration

    Swift

    @discardableResult
    public func onEditingBegan(handler: @escaping () -> Void) -> Self

    Parameters

    handler

    The closure to be called when the focus enters the text field

    Return Value

    itself so you can daisy chain the other event handler calls

  • A convenience method to respond to UIControlEvents.editingDidEndOnExit for a UITextField. This occurs when the user taps the return key on the keyboard.

    Declaration

    Swift

    @discardableResult
    public func onReturn(handler: @escaping () -> Void) -> Self

    Parameters

    handler

    The closure to be called when the return key is tapped

    Return Value

    itself so you can daisy chain the other event handler calls

  • This method determines if the text field should begin editing. This is equivalent to implementing the textFieldShouldBeginEditing(_:) method in UITextFieldDelegate.

    Declaration

    Swift

    @discardableResult
    public func shouldBeginEditing(handler: @escaping () -> Bool) -> Self

    Parameters

    handler

    The closure that determines whether the text field should begin editing.

    Return Value

    Returns itself so you can daisy chain the other delegate calls

  • This method determines if the text field did begin editing. This is equivalent to implementing the textFieldDidBeginEditing(_:) method in UITextFieldDelegate.

    Declaration

    Swift

    @discardableResult
    public func didBeginEditing(handler: @escaping () -> Void) -> Self

    Parameters

    handler

    The closure that determines whether the text field did begin editing.

    Return Value

    Returns itself so you can daisy chain the other delegate calls

  • This method determines if the text field should end editing. This is equivalent to implementing the textFieldShouldEndEditing(_:) method in UITextFieldDelegate.

    Declaration

    Swift

    @discardableResult
    public func shouldEndEditing(handler: @escaping () -> Bool) -> Self

    Parameters

    handler

    The closure that determines whether the text field should end editing.

    Return Value

    Returns itself so you can daisy chain the other delegate calls

  • This method determines if the text field did end editing. This is equivalent to implementing the textFieldDidEndEditing(_:) method in UITextFieldDelegate.

    Declaration

    Swift

    @discardableResult
    public func didEndEditing(handler: @escaping () -> Void) -> Self

    Parameters

    handler

    The closure that determines whether the text field did end editing.

    Return Value

    Returns itself so you can daisy chain the other delegate calls

  • This method determines if the text field should change its characters based on user input. This is equivalent to implementing the textField(_:shouldChangeCharactersIn:replacementString:) method in UITextFieldDelegate. This closure passed here will conflict with any closure passed to shouldChangeString(handler:) and the last closure passed will be the one that’s called.

    Declaration

    Swift

    @discardableResult
    public func shouldChangeCharacters(handler: @escaping (_ range: NSRange, _ replacementString: String) -> Bool) -> Self

    Parameters

    handler

    The closure that determines whether the text field should change its characters.

    Return Value

    Returns itself so you can daisy chain the other delegate calls

  • This method determines if the text field should change its text based on user input. This is a convenience method around equivalent to implementing the textField(:shouldChangeCharactersIn:replacementString:) method in UITextFieldDelegate. This closure passed here will conflict with any closure passed to `shouldChangeCharacters(:)` and the last closure passed will be the one that’s called.

    Declaration

    Swift

    @discardableResult
    public func shouldChangeString(handler: @escaping (_ from: String, _ to: String) -> Bool) -> Self

    Parameters

    handler

    The closure that determines whether the text field should change its text from the first parameter string to the second parameter string.

    Return Value

    Returns itself so you can daisy chain the other delegate calls

  • This method determines if the text field should remove its text. This is equivalent to implementing the textFieldShouldClear(_:) method in UITextFieldDelegate.

    Declaration

    Swift

    @discardableResult
    public func shouldClear(handler: @escaping () -> Bool) -> Self

    Parameters

    handler

    The closure that determines whether the text field should clear its text.

    Return Value

    Returns itself so you can daisy chain the other delegate calls

  • This method determines if the text field should process the return button. This is equivalent to implementing the textFieldShouldReturn(_:) method in UITextFieldDelegate.

    Declaration

    Swift

    @discardableResult
    public func shouldReturn(handler: @escaping () -> Bool) -> Self

    Parameters

    handler

    The closure that determines whether the text field should process the return button.

    Return Value

    Returns itself so you can daisy chain the other delegate calls

  • Clears any delegate closures that were assigned to this UITextField. This cleans up memory as well as sets the delegate property to nil. This is typically only used for explicit cleanup. You are not required to call this method.

    Declaration

    Swift

    @objc
    public func clearClosureDelegates()