Giter Site home page Giter Site logo

veil's Introduction

Veil

Swift codecov

Easy and flexible string masking utility.

Features

  • Platform independent (this is a pure swift package)
  • Provides masked and unmasked input (process method).
  • Easy integration to (UIKit, SwiftUI)
  • Input blocking comes free.

Installation

Veil comes with support for SwiftPM so just copy this repos url into your Xcode project

Usage

Veil enables string masking by providing a pattern of special characters

To create for instance a date formatter for mm / yy this is as simple as:

let dateMask = Veil(pattern: "## / ##")
let result = dateMask.mask(input: "234")
// result = "23 / 4"

Veil uses a configuration object to specify how to parse the mask pattern. The default configuration uses # to represent any digit and * to represent other characters.

Also notice that Veil will pickup any other character such as / and spaces as symbols in the pattern and will insert them automatically in the resulting string.

UITextField integration

UITextField integration is simple and doesn't require usage of its delegate.

This how you can accomplish live masking of textfields.

let dateMask = Veil(pattern: "## / ##")

@objc func textDidChange(_ sender: UITextField) {
    guard let currentText = sender.text else  {
        return
    }

    sender.text = dateMask.mask(input: currentText, exhaustive: false)
}

When performing masking of live input just make sure to use the exhaustive option set to false

veil's People

Contributors

danielcardonarojas avatar onsissond avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

veil's Issues

SwiftUI

Is there a sample for SwiftUI?
I write like this, but clearing characters requires clearing mask characters too

@Published var myText = "" {
        didSet {
            if myText != myMask.mask(input: myText, exhaustive: false) {
                myText = myMask.mask(input: myText, exhaustive: false)
            }
        }
    }

Cursor move to the trailing of the masked text.

Hello ๐Ÿ‘‹๐Ÿผ

I tried to use Veil to format credit card numbers of 16 digits within a text field. I'm using the pattern of "#### #### #### ####".

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
     
      let numberMask = Veil(pattern: "#### #### #### ####")
           
      textField.text = numberMask.mask(input: cardNumber, exhaustive: false)

}

The formatting works fine, but when I tried to move the cursor in the middle of the text and delete a number, the cursor move automatically to the last number. (see the video attached)

In my opinion, the cursor should stay at the same place where the deletion happened.

Thank you.

mask_issue.mov

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.