DSTextField

@IBDesignable
public final class DSTextField : UIView
extension DSTextField: UITextViewDelegate

An Anatomy-styled text entry element. Includes properties for providing placeholder text (which shrinks and moves upwards when text is entered), helper text that sits below that educates the user on what text to enter, and DSValidationRule rules which will fire when the entered text isn’t determined to be valid.

The field defaults to single-line, but if isMultiline is set to true the field will grow vertically as text is entered and space is required.

For storyboards, add a UIView to your view and change the class name to DSTextField.

Since this component can shrink and grow vertically to accomodate its contents, it’s recommended that the DSTextField be embedded in a UITableView that makes it easy for its contents to resize smoothly vertically. The easiest way to do this is to use the DSTextFieldDelegate method textFieldDidLayout which gets called when the size changes. Implement it somewhat as follows:

textFieldDidLayout(_ textField: DSTextField) {
    tableView.beginUpdates()
    tableView.endUpdates()
}

Initialize and set properties as needed.

let bioField = DSTextField()

bioField.placeholderText = "Bio"
bioField.helperText = "XXX-XXX-XXXX"
bioField.addValidationRule(RequiredValidationRule())
bioField.clearButtonMode = .whileEditing
  • Events from DSTextField that can be listened for via Combine.

    Declaration

    Swift

    public enum EventType

Accessors

  • The delegate which receives updates on the DSTextField‘s state.

    Declaration

    Swift

    @IBOutlet
    public weak var textFieldDelegate: DSTextFieldDelegate
  • When true, the field will grow vertically as more text is entered. This is similar to standard UITextView behavior except the vertical size is the minimum required to display all text.

    Declaration

    Swift

    @IBInspectable
    public var isMultiline: Bool { get set }
  • Returns true if the component is enabled and allows text editing.

    Declaration

    Swift

    @IBInspectable
    public var isEnabled: Bool { get set }
  • The text to be displayed in the first position of the DSTextField.

    Declaration

    Swift

    @IBInspectable
    public var prefixText: String? { get set }
  • The text to be displayed in the placeholder which doubles as the DSTextField‘s label.

    Declaration

    Swift

    @IBInspectable
    public var placeholderText: String? { get set }
  • The text to be displayed as helper (below the field) to the user on what to enter into the field.

    Declaration

    Swift

    @IBInspectable
    public var helperText: String? { get set }
  • The text entered into the DSTextField.

    Declaration

    Swift

    @IBInspectable
    public var text: String? { get set }
  • An input mask that formats entered text and/or prevents certain characters from being entered at all

    Declaration

    Swift

    public var inputMask: DSInputMask? { get set }
  • Returns the raw text entered into the DSTextField if a DSInputMask is defined, otherwise returns the text as displayed.

    Declaration

    Swift

    public var rawText: String { get }
  • Returns the text as it appears in DSTextField minus prefixing.

    Declaration

    Swift

    public var unprefixedText: String { get }
  • The array of ValidationRule objects to be used for validating the current state of the DSTextField contents.

    Declaration

    Swift

    public var validationRules: [DSValidationRule]
  • Returns true if there are no validations or any validations that exist pass.

    Declaration

    Swift

    public var isValid: Bool { get }
  • The embedded UITextView in case additional customization is needed. (Note that some properties might be overridden by DSTextField, so try to avoid this if at all possible.)

    Declaration

    Swift

    public private(set) var textView: AccessibilityTextView! { get }
  • Determined if, and when, the clear button displays on the component that allows the input text to be reset.

    Declaration

    Swift

    public var clearButtonMode: UITextField.ViewMode { get set }
  • Declaration

    Swift

    public private(set) lazy var eventPublisher: AnyPublisher<EventType, Never> { get set }
  • Declaration

    Swift

    public private(set) lazy var textPublisher: AnyPublisher<String, Never> { get set }
  • Declaration

    Swift

    public private(set) lazy var validityPublisher: AnyPublisher<Bool, Never> { get set }
  • The clear button mode representable by a String.

    This property is only recommended for use in the Attributes inspector of a storyboard since enums are not @IBInspectable. Possible values are “always”, “never”, “whileEditing” and “unlessEditing”. In code, use the clearButtonMode property instead.

    Declaration

    Swift

    @IBInspectable
    public var clearButtonModeString: String { get set }
  • Declaration

    Swift

    public private(set) var validationState: DSTextFieldValidationState { get }

BaseTextField

  • Adds a validation rule to be used for validating the current DSTextField text.

    Declaration

    Swift

    func add(validationRule rule: DSValidationRule)

    Parameters

    validationRule

    A ValidationRule object to be used for checking a validation

  • Adds an array of validation rules to be used for validating the current DSTextField text.

    Declaration

    Swift

    func add(validationRules rules: [DSValidationRule])

    Parameters

    validationRules

    A ValidationRule array to be used for checking validations

  • Removes all validation rules.

    Declaration

    Swift

    func removeAllValidationRules()

Accessory View

  • Attaches a standard accessory view for this field.

    Declaration

    Swift

    func attachInputAccessoryView(done: Bool = true)

    Parameters

    done

    Adds a “Done” button if true

Validation

  • Returns the validation status of the TextField text based on the current validation rules.

    Declaration

    Swift

    @discardableResult
    func validate(animated: Bool = false) -> DSValidationRule?