Latest 1.0.2
Homepage https://github.com/rajanshahsa/CustomTextField
License MIT
Platforms ios 8.0
Authors

CustomTextField is used to mask the text entered in textfield(e.g: Credit card number -> " **** 1111").
CI Status
Version
License
Platform

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

Installation

1. CocoaPods

CustomTextField is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod "CustomTextField"

2. Manual

Add CustomTextField.swift file into your project.

Usage

1. Import CustomTextField in any class you want to use it.

import CustomTextField

1.1. Add Following code to your ViewController

  • Assgin Textfield’s delegate to viewController
  • Declare a String variable to hold Textfield value for later purpose.
    var cardNumberBuffer : String = ""
  • Implement ‘shouldChangeCharactersIn’ method of UITextFieldDelegate.
  • return false for the textfield you want to mask the text.
  • Call textField.shouldChangeValue(cardNumberBuffer: String)
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    if (string.characters.count > 0)
    {
    if (self.isMaxLength(textField)) {
    return false
    }

self.cardNumberBuffer = String(format: "%@%@", self.cardNumberBuffer, string)
}
else
{
if (self.cardNumberBuffer.characters.count > 1)
{
let length = self.cardNumberBuffer.characters.count-1
self.cardNumberBuffer = self.cardNumberBuffer[self.cardNumberBuffer.index(self.cardNumberBuffer.startIndex, offsetBy: 0)…self.cardNumberBuffer.index(self.cardNumberBuffer.startIndex, offsetBy: length-1)]
}
else
{
self.cardNumberBuffer = ""
}
}

textField.shouldChangeValue(cardNumberBuffer: self.cardNumberBuffer)
return false
}

func isMaxLength(_ textField:UITextField) -> Bool {
var result = false

if ((textField.text?.characters.count)! > 19)
{
result = true
}

return result
}



<!--CustomTextField is available through [CocoaPods](http://cocoapods.org). To install-->
<!--it, simply add the following line to your Podfile:-->
<!---->
<!--```ruby-->
<!--pod "CustomTextField"-->
<!--```-->

## Author

rajanshahsa, [email protected]

## License

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

Latest podspec

{
    "name": "CustomTextField",
    "version": "1.0.2",
    "summary": "CustomTextField is used to mask the text entered in textfield(e.g: Credit card number -> "**** **** **** 1111").",
    "description": "TODO: Add long description of the pod here.nLast login: Mon Feb 20 10:57:36 on ttys000nSA-MAC-019:~ rajan.shah$ cd /Users/rajan.shah/Documents/SA Projects/iOS/Github/CustomTextField/CustomTextFieldn-bash: cd: /Users/rajan.shah/Documents/SA: No such file or directorynSA-MAC-019:~ rajan.shah$ cd "/Users/rajan.shah/Documents/SA Projects/iOS/Github/CustomTextField/CustomTextField"nSA-MAC-019:CustomTextField rajan.shah$ cd n.DS_Store                .travis.yml              LICENSEn.git/                    CustomTextField/         README.mdn.gitignore               CustomTextField.podspec  _Pods.xcodeproj/n.swift-version           Example/                 nSA-MAC-019:CustomTextField rajan.shah$ vi CustomTextField.podspec nSA-MAC-019:CustomTextField rajan.shah$ vi README.md nSA-MAC-019:CustomTextField rajan.shah$ git add .nSA-MAC-019:CustomTextField rajan.shah$ git commit -m "Update Description"n[master 7e33a19] Update Descriptionn 2 files changed, 2 insertions(+), 2 deletions(-)nSA-MAC-019:CustomTextField rajan.shah$ git pushnCounting objects: 4, done.nDelta compression using up to 4 threads.nCompressing objects: 100% (4/4), done.nWriting objects: 100% (4/4), 551 bytes | 0 bytes/s, done.nTotal 4 (delta 3), reused 0 (delta 0)nremote: Resolving deltas: 100% (3/3), completed with 3 local objects.nTo https://github.com/rajanshahsa/CustomTextField.gitn   ec521e7..7e33a19  master -> masternSA-MAC-019:CustomTextField rajan.shah$ vi README.md nn```n- Implement 'shouldChangeCharactersIn' method of UITextFieldDelegate.n- return false for the textfield you want to mask the text.n- Call textField.shouldChangeValue(cardNumberBuffer: String)n```swiftnfunc textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {nif (string.characters.count > 0)n{nif (self.isMaxLength(textField)) {nreturn falsen}nnself.cardNumberBuffer = String(format: "%@%@", self.cardNumberBuffer, string)n}nelsen{nif (self.cardNumberBuffer.characters.count > 1)n{nlet length = self.cardNumberBuffer.characters.count-1nself.cardNumberBuffer = self.cardNumberBuffer[self.cardNumberBuffer.index(self.cardNumberBuffer.startIndex, offsetBy: 0)...self.cardNumberBuffer.index(self.cardNumberBuffer.startIndex, offsetBy: length-1)]n}nelsen{nself.cardNumberBuffer = ""n}n}nntextField.shouldChangeValue(cardNumberBuffer: self.cardNumberBuffer)nreturn falsen}nnfunc isMaxLength(_ textField:UITextField) -> Bool {nvar result = falsennif ((textField.text?.characters.count)! > 19)n{nresult = truen}nnreturn resultn}n```nnnnnnnnnn## Authornnrajanshahsa, [email protected]",
    "homepage": "https://github.com/rajanshahsa/CustomTextField",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "rajanshahsa": "[email protected]"
    },
    "source": {
        "git": "https://github.com/rajanshahsa/CustomTextField.git",
        "tag": "1.0.2"
    },
    "platforms": {
        "ios": "8.0"
    },
    "source_files": "CustomTextField/Classes/**/*",
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This