Code Monkey home page Code Monkey logo

chrtextfieldformatter's Introduction

CHRTextFieldFormatter

CHRTextFieldFormatter allows you to apply an input mask to a UITextField instance.

Features

  • Does not mute Copy/Cut/Paste events.
  • Correctly calculates cursor position.
  • Does not require UITextField subclassing.
  • Provides CHRTextMask interface to allow custom text masking algorithm implementation.
  • Subclasses NSFormatter.

Implemented Masks

There are two kind of masks available:

  • CHRPhoneNumberMask to apply cellular phone number mask.
  • CHRCardNumberMask to apply credit card number mask.

Phone Number Mask

Phone number mask can be configured to have non-deletable prefix. For example:

    CHRPhoneNumberMask *mask = [CHRPhoneNumberMask new];
    mask.prefix = @"+7";

Installation

Drag *.h and *.m files to your project.

Usage

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.phoneNumberFormatter = [[CHRTextFieldFormatter alloc] initWithTextField:self.phoneNumberTextField mask:[CHRPhoneNumberMask new]];
    self.cardNumberFormatter = [[CHRTextFieldFormatter alloc] initWithTextField:self.cardNumberTextField mask:[CHRCardNumberMask new]];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (textField == self.phoneNumberTextField) {
        return [self.phoneNumberFormatter textField:textField shouldChangeCharactersInRange:range replacementString:string];
    } else if (textField == self.cardNumberTextField) {
        return [self.cardNumberFormatter textField:textField shouldChangeCharactersInRange:range replacementString:string];
    } else {
        return YES;
    }
}

Run the example project to see the demo.

chrtextfieldformatter's People

Contributors

chebur avatar revetkn 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  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

chrtextfieldformatter's Issues

crash in iPad

hello , my english bad. hope you can understand what i mean
i run you demo in iPad, i input number in Crad Number TextField, i input '12345678' . The TextField show '1234 5678'. i delete '5678', then i undo, crash
i use Apple Keyboard, This keyboard has a undo button

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.