Skip to content

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

License

Notifications You must be signed in to change notification settings

rajanshahsa/CustomTextField

Repository files navigation

CustomTextField

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
}

Author

rajanshahsa, rajan.shah@solutionanalysts.com

License

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

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published