CustomTextField 1.0.2

CustomTextField 1.0.2

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Feb 2017
SwiftSwift Version 3.0
SPMSupports SPM

Maintained by Rajan Shah.



  • By
  • rajanshahsa

CustomTextField

Example

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

Requirements

Installation

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, [email protected]

License

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