DecoratingTextFieldDelegate

public final class DecoratingTextFieldDelegate : NSObject

Adds formatting (decoration) characters to text field’s content according to a variable pattern. Can be used for payment card number formatting, phone number formatting, etc.

  • DecoratingTextFieldDelegate will call this function passing current data string as a parameter every time the data string changes, the returned pattern will subsequently be used to format the data string passed.

    Declaration

    Swift

    public let patternForDataString: (String) -> String
  • A character that is not a formatting character.

    Declaration

    Swift

    public let patternPlaceholderForDataCharacter: Character
  • A predicate to filter non-data characters from user’s input. No matter what user tries to put into the textfield, only characters for which isDataCharacter returns true will appear in the text field.

    Declaration

    Swift

    public let isDataCharacter: (Character) -> Bool
  • Initializes a delegate with a fixed pattern

    Example:

    A 16-digit VISA payment card pattern might look like this ####-####-####-#### '#' is a patternPlaceholderForDataCharacter and ‘-’ is a formatting (decorating) character.

    Declaration

    Swift

    public convenience init(
    	pattern: String,
    	patternPlaceholderForDataCharacter: Character,
    	isDataCharacter: @escaping (Character) -> Bool)

    Parameters

    pattern

    A string containing data placeholder and formatting characters.

    patternPlaceholderForDataCharacter

    A character that is not a formatting character.

    isDataCharacter

    A predicate to filter non-data characters from user’s input. No matter what user tries to put into the textfield, only characters for which isDataCharacter returns true will appear in the text field.

  • Intializes a delegate with a fixed pattern

    Example:

    A 16-digit VISA payment card pattern might look like this ####-####-####-#### '#' is a patternPlaceholderForDataCharacter and ‘-’ is a formatting (decorating) character. Furthermore, to support various kinds of payment cards a more complex behaviour may need to be implemented where the first 6 digits of a payment card number will define total length and formatting pattern for any valid card number starting with those 6 digits. This behaviour can be implemented by using patternForDataString.

    Declaration

    Swift

    public init(
    	patternForDataString: @escaping (String) -> String,
    	patternPlaceholderForDataCharacter: Character,
    	isDataCharacter: @escaping (Character) -> Bool)

    Parameters

    patternForDataString

    DecoratingTextFieldDelegate will call this function passing current data string as a parameter every time the data string changes, the returned pattern will subsequently be used to format the data string passed.

    patternPlaceholderForDataCharacter

    A character that is not a formatting character.

    isDataCharacter

    A predicate to filter non-data characters from user’s input. No matter what user tries to put into the textfield, only characters for which isDataCharacter returns true will appear in the text field.

  • Decorate a string consisting of data characters (see isDataCharacter) into a string converted using patternForDataString

    Declaration

    Swift

    public func decorateString(_ dataString: String) -> String

    Parameters

    dataString

    A string containing only data characters (see isDataCharacter).

    Return Value

    The converted string from the input.

  • Strips formatting (decoration) characters from the input string, checking each character using isDataCharacter and removing it from the input.

    Declaration

    Swift

    public func undecorateString(_ decoratedString: String) -> String

    Parameters

    dataString

    A string contained any kind of characters

    Return Value

    The undecorated string from the input.

  • Please refer to the documentation for UITextFieldDelegate.textField(_:, shouldChangeCharactersIn:, replacementString:).

    Declaration

    Swift

    public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool