Giter Site home page Giter Site logo

jmmasktextfield-swift's Introduction

JMMaskTextField

CI Status Version License Platform

Back in my JavaScript days (oh what dark ages) it was quite simple to apply a mask to an input with some jQuery plugin, you just had to set the mask to something like (999) 999-9999 and you had a great masked input for a phone number. I never found anything that simple on iOS. Some libs required you to write a regex pattern and other crazy stuff. Sure, that can be useful for more complex cases, but I just wanted something simple and straightforward. From that need came JMMaskTextField.

JMMaskTextField allows you to mask your UITextField by simply setting a mask pattern string. Mask characters are:

  • A for letters
  • 0 for numbers
  • * for numbers and letters

So for a Brazilian license plate, which has 3 letters, a dash and then 4 numbers, the mask would be: AAA-0000.

JMMaskTextField also handles pasting, inserting and deleting from the middle.

There's also an Objective-C version

Installation

CocoaPods

You can install JMMaskTextField with CocoaPods

  • Add this line to your Podfile pod "JMMaskTextField-Swift"
  • Run pod install

Manual

Just drop the JMMaskTextField folder in your project and you're all set!

Usage

Funcionality is broken down into 2 classes:

  • JMStringMask - all the string masking functionality. You can use this yourself outside of the JMMaskTextField.
  • JMMaskTextField - the UITextField subclass, handling all editing events. Uses an instance of JMStringMask.

Interface Builder

Set the class of your text field to JMMaskTextField in the Identity Inspector. And then on the Attributes Inspector set your mask. And that's it!

Code

Initialize an instance of JMMaskTextField and set the mask property:

let maskTextField = JMMaskTextField(frame: CGRect.zero)
maskTextField.maskString = "(00) 0 0000-0000"

And that's it!

JMStringMask

You can use the string masking functionality outside of the text field. Usage is also simple:

let mask = JMStringMask(mask: "00000-000")
let maskedString = mask.mask(string: "30310360") // returns "30310-360"
let unmaskedString = mask.unmask(string: maskedString) // returns "30310360"

Changing the mask while editing

In Brazil we a problem: cell phone numbers are 9 digits (and until recently not all of them were 9 digits), and landlines are 8 digits. That creates a problem with masking. JMMaskTextField supports changing the mask while editing. So when we identify the user typed a 9 digit number, we switch the mask to the longer format. If they delete a digit, we go back to the original mask. An example:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    
    guard let text = textField.text as NSString? else { return true }
    let newText = text.replacingCharacters(in: range, with: string)
    
    let maskTextField = textField as! JMMaskTextField
    guard let unmaskedText = maskTextField.stringMask?.unmask(string: newText) else { return true }
    
    if unmaskedText.count >= 11 {
        maskTextField.maskString = "(00) 0 0000-0000"
    } else {
        maskTextField.maskString = "(00) 0000-0000"
    }
    
    return true
}

Author

Jota Melo, [email protected]

License

JMMaskTextField is available under the MIT license. See the LICENSE file for more info.

jmmasktextfield-swift's People

Contributors

jotamelo avatar leovalentim avatar reprotector avatar netizen01 avatar

Watchers

James Cloos avatar

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.