Giter Site home page Giter Site logo

orucanil / stringformatter Goto Github PK

View Code? Open in Web Editor NEW
252.0 6.0 20.0 29 KB

Simple Text Formetter (Credit Card Number, Phone Number, Serial Number etc.) Can be used in all text inputs according to the format pattern. If desired, large minor character restrictions can be made in the format pattern.

Home Page: https://www.linkedin.com/in/annul

License: MIT License

Swift 100.00%
stringformatter stringformatter-extension text-formetter swift credit-card formatter phone-format creditcard-validator validator ios

stringformatter's Introduction

StringFormatter

Simple Text Formatter (Credit Card Number, Phone Number, Serial Number etc.) Can be used in all text inputs according to the format pattern. If desired, large minor character restrictions can be made in the format pattern.

TextFieldFormatter : https://github.com/orucanil/TextFieldFormatter

Display Visual Example


Visual1

Installation

To use the StringFormatter extension in an app, just drag the StringFormatter extension file (demo files and assets are not needed) into your project.

Methods

The StringFormatter extension has the following methods (note: for iOS, String in method arguments):

  • func format(_ format: String, oldString: String) -> String

Formatting method according to given format pattern. oldString can be empty(""), but the final character formatting may not work smoothly.

  • func unformat(_ format: String, oldString: String) -> String

Unformatting method according to given format pattern. oldString can be empty(""), but the final character unformatting may not work smoothly.

How to use ?

If the text format is entered uppercase, the character input from the keyboard is displayed as a upper case character.

'x' or 'X' -> Any character

'c' or 'C' -> Alphabetic character

'n' or 'N' -> Numerical character

extension ViewController: UITextFieldDelegate {
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

guard let text = textField.text else {
return true
}
let lastText = (text as NSString).replacingCharacters(in: range, with: string) as String

if textfieldPhoneNumber == textField {
textField.text = lastText.format("(NNN) NNN NN NN", oldString: text)
return false
} else if textfieldCreditCard == textField {
textField.text = lastText.format("nnnn nnnn nnnn nnnn", oldString: text)
return false
} else if textfieldSerialNumber == textField {
textField.text = lastText.format("XX NNNN", oldString: text)
return false
}
return true
}
}

Build and run the project files. Enjoy more examples!

stringformatter's People

Contributors

orucanil avatar schenkty 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  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

stringformatter's Issues

Error in xCode 10

After updating xCode from 9.4 to 10.0 I am getting below error at this line

return String(self[Range(start ..< end)]).

Cannot invoke initializer for type 'Range<String.Index>' with an argument list of type '(Range<String.Index>)'

How to fix this?

How to fix This Warning?

I got this warning at below code.. how can I fix this?

'init' is deprecated: CountableRange is now Range. No need to convert any more.

subscript (i: Int) -> String {
    return self[Range(i ..< i + 1)]
}

func isValidFormat() -> Bool

Would be cool to have a function that returns a Bool of true when text meets formatting.
For Validation purposes.

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.