Skip to content
Egor Taflanidi edited this page Jul 28, 2023 · 14 revisions

📑 Wiki Table of Contents

  • For a quick start, scroll down
  • Syntax: learn how to compose your masks
  • Text field listeners: learn more about available text field listener (delegate) classes
  • Phones and country codes: more info about the international phone number formats support
  • Tail placeholder: implement a placeholder that is always visible & guides user input
  • Multiple masks & affinity: you can have several formats associated with a single text field; this is a deeper look into how the library can decide between them

Advanced reading:

🚀 Quick Start

Install the library

Swift Package Manager is the preferred way.

  • Open project root settings → Package Dependencies
  • Click ➕Add Package Dependency
  • Click 🔍Search or Enter Package URL
  • Insert https://github.com/RedMadRobot/input-mask-ios
  • Click Add Package
  • Wait for the package resolution, choose target, boom, done

Programmatically, Swift

Import the library:

import InputMask

Configure the text field:

let inputListener = MaskedTextInputListener(primaryFormat: "+380 ([00]) [000]-[00]-[00]")
textField.delegate = inputListener // remember that delegates are weak references

Done. Build, Run & Enjoy.

Quick drop using a Storyboard

In order to attach our library to your UITextField you may simply wire up a listener directly within the layout.

  • Open the scene that contains the UITextField in question
  • Open Library ⌘⇧L → find NSObject → drop it onto the Scene (the left hierarchy view)
  • In the Identity Inspector select a custom class for this object: MaskedTextInputListener
  • In the Attributes Inspector fill in the Primary Mask Format: +380 ([00]) [000]-[00]-[00], hit enter
  • Context-click the UITextField, find the delegate outlet, drag it to the MaskedTextInputListener object
  • Done. Build, Run & Enjoy.

SwiftUI

Import the library:

import InputMask

Add into the view builder:

MaskedTextField(
    text: $text,                // text field contents
    value: $value,              // extracted value
    complete: $complete,        // value completeness, true/false
    placeholder: placeholder,   // UITextField::placeholder
    primaryMaskFormat: "+380 ([00]) [000]-[00]-[00]"
)

Done. Build, Run & Enjoy.