Code Monkey home page Code Monkey logo

mstextfield's Introduction

MSTextField

MSTextField is a UITextField subclass that allows developers to pass in custom formatting and verification blocks as instance variables. It has a minimum length to verify parameter (so that your text field doesn't try to verify blank input) and max length parameter (to prevent a user from inputting something longer than desired). You may also set the validation image and the invalid input warning border color to be whatever you want (the defaults are a green checkmark and a darkish red respectively).

To use it, simply add pod 'MSTextField' to your podfile and run pod install. You can also just download the source and add it to your project.

Note: If you don't have CocoaPods integrated in your projects, you can find out how to add it at http://www.cocoapods.org

Here's an example of how it's used, taken from one of the factory methods that creates a text field to handle dates (month and day only):

MSTextField *dateField = [[MSTextField alloc] initWithFrame:frame];
dateField.placeholder = @"Enter date";
dateField.keyboardType = UIKeyboardTypeNumberPad;
dateField.maxLengthOfInput = 5;
dateField.formattingBlock = ^(MSTextField *textField, char newCharacter) {
    if (newCharacter == '\b') {
        if ([textField.text length] == 3) {
            textField.text = [textField.text substringToIndex:2];
        }
    } else {
        if ([textField.text length] == 2) {
            textField.text = [NSString stringWithFormat:@"%@/", textField.text];
        }
    }
};
dateField.verificationBlock = ^BOOL(NSString *text) {
    
    if ([text length] != 5) {
        return NO;
    }
    return NSLocationInRange([[text substringToIndex:2] intValue], NSMakeRange(1, 12));
};

As seen above, the formatting block automatically adds a slash after the second character is typed, and the verification block checks to make sure that the string is the correct length and that the month component is a valid month.

Additionally, MSTextField includes MSTextFieldDelegate, a protocol that conforms to UITextFieldDelegate, which provides two additional optional delegate methods that you can implement:

- (void)textFieldReceivedValidInput:(MSTextField *)textField;
- (void)textFieldReceivedInvalidInput:(MSTextField *)textField;

It's that simple! MSTextField comes with class methods that generate date fields (like the one above), phone number fields, email address fields, and credit card fields. However, you can always create your own and use those methods as examples.

In the example folder is a very simple app with a UISegmentedControl which allows you to toggle between different example fields to play with. If you have any questions, feel free to email me at [email protected]. Good luck!

mstextfield's People

Stargazers

Jacob Jennings avatar Daniel Rosado avatar

Watchers

Robert Diamond avatar Matt Nichols avatar Mason Silber avatar James Cloos avatar Valeriy Van 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.