Allowing single digit in UITextField in iOS - ios

I have a Verification ViewController, I get 4 digit verification code by SMS and I need to enter those code to login, I have created the ViewController like this
As you can see four UITextFields, I need to allow only single digit for each UITextField,
What I tried: I was trying to use shouldChangeCharactersInRange:method: , but its not getting called, I don't know what's wrong, I think because UITextFields are in UITableView so it is not working.

You can change the text field like this by using the delegate function of the text field. Initially, you need to set the delegate and the tag of each text field.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if ((textField.text.length >= 1) && (string.length > 0))
{
NSInteger nextTag = textField.tag + 1;
// Try to find next responder
UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
if (! nextResponder)
nextResponder = [textField.superview viewWithTag:1];
if (nextResponder)
// Found next responder, so set it.
[nextResponder becomeFirstResponder];
return NO;
}
return YES;
}
Swift 2
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
// On inputing value to textfield
if (textField.text?.characters.count < 1 && string.characters.count > 0){
let nextTag = textField.tag + 1;
// get next responder
var nextResponder = textField.superview?.viewWithTag(nextTag);
if (nextResponder == nil){
nextResponder = textField.superview?.viewWithTag(1);
}
textField.text = string;
nextResponder?.becomeFirstResponder();
return false;
}
else if (textField.text?.characters.count >= 1 && string.characters.count == 0){
// on deleting value from Textfield
let previousTag = textField.tag - 1;
// get next responder
var previousResponder = textField.superview?.viewWithTag(previousTag);
if (previousResponder == nil){
previousResponder = textField.superview?.viewWithTag(1);
}
textField.text = "";
previousResponder?.becomeFirstResponder();
return false;
}
return true;
}
Swift 4
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if textField.text!.count < 1 && string.count > 0{
let nextTag = textField.tag + 1
// get next responder
var nextResponder = textField.superview?.viewWithTag(nextTag)
if (nextResponder == nil){
nextResponder = textField.superview?.viewWithTag(1)
}
textField.text = string
nextResponder?.becomeFirstResponder()
return false
}
else if textField.text!.count >= 1 && string.count == 0{
// on deleting value from Textfield
let previousTag = textField.tag - 1
// get next responder
var previousResponder = textField.superview?.viewWithTag(previousTag)
if (previousResponder == nil){
previousResponder = textField.superview?.viewWithTag(1)
}
textField.text = ""
previousResponder?.becomeFirstResponder()
return false
}
return true
}

Use this code if you don't want to work with tag and it works better then above
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
// On inputing value to textfield
if ((textField.text?.characters.count)! < 1 && string.characters.count > 0){
if(textField == txtOne)
{
txtTwo.becomeFirstResponder()
}
if(textField == txtTwo)
{
txtThree.becomeFirstResponder()
}
if(textField == txtThree)
{
txtFour.becomeFirstResponder()
}
textField.text = string
return false
}
else if ((textField.text?.characters.count)! >= 1 && string.characters.count == 0){
// on deleting value from Textfield
if(textField == txtTwo)
{
txtOne.becomeFirstResponder()
}
if(textField == txtThree)
{
txtTwo.becomeFirstResponder()
}
if(textField == txtFour)
{
txtThree.becomeFirstResponder()
}
textField.text = ""
return false
}
else if ((textField.text?.characters.count)! >= 1 )
{
textField.text = string
return false
}
return true
}

Swift 4
Inspired by #Anurag Soni and #Varun Naharia answers
Variant A
extension EnterConfirmationCodeTextField: UITextFieldDelegate {
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard let textFieldCount = textField.text?.count else { return false }
// Сlosure
let setValueAndMoveForward = {
textField.text = string
let nextTag = textField.tag + 1
if let nextResponder = textField.superview?.viewWithTag(nextTag) {
nextResponder.becomeFirstResponder()
}
}
// Сlosure
let clearValueAndMoveBack = {
textField.text = ""
let previousTag = textField.tag - 1
if let previousResponder = textField.superview?.viewWithTag(previousTag) {
previousResponder.becomeFirstResponder()
}
}
if textFieldCount < 1 && string.count > 0 {
setValueAndMoveForward()
if textField.tag == 4 {
print("Do something")
}
return false
} else if textFieldCount >= 1 && string.count == 0 {
clearValueAndMoveBack()
return false
} else if textFieldCount >= 1 && string.count > 0 {
let nextTag = self.tag + 1
if let previousResponder = self.superview?.viewWithTag(nextTag) {
previousResponder.becomeFirstResponder()
if let activeTextField = previousResponder as? UITextField {
activeTextField.text = string
}
}
return false
}
return true
}
}
Variant B (a little bit another behavior):
extension EnterConfirmationCodeTextField: UITextFieldDelegate {
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard let textFieldCount = textField.text?.count else { return false }
// Сlosure
let setValueAndMoveForward = {
textField.text = string
let nextTag = textField.tag + 1
if let nextResponder = textField.superview?.viewWithTag(nextTag) {
nextResponder.becomeFirstResponder()
}
}
// Сlosure
let clearValueAndMoveBack = {
textField.text = ""
let previousTag = textField.tag - 1
if let previousResponder = textField.superview?.viewWithTag(previousTag) {
previousResponder.becomeFirstResponder()
}
}
if textFieldCount < 1 && string.count > 0 {
setValueAndMoveForward()
if textField.tag == 4 {
print("Do something")
}
return false
} else if textFieldCount >= 1 && string.count == 0 {
clearValueAndMoveBack()
return false
} else if textFieldCount >= 1 {
setValueAndMoveForward()
return false
}
return true
}
}
Also, I implemented this feature:
In the case where the last textFiled is empty, I just want to switch to the previous textFiled. I tried all this methods. But as for me the method below more elegant and works like a charm:
Variant A
class EnterConfirmationCodeTextField: UITextField {
// MARK: Life cycle
override func awakeFromNib() {
super.awakeFromNib()
delegate = self
}
// MARK: Methods
override func deleteBackward() {
super.deleteBackward()
let previousTag = self.tag - 1
if let previousResponder = self.superview?.viewWithTag(previousTag) {
previousResponder.becomeFirstResponder()
if let activeTextField = previousResponder as? UITextField {
if let isEmpty = activeTextField.text?.isEmpty, !isEmpty {
activeTextField.text = String()
}
}
}
}
}
Variant B (a little bit another behavior):
class EnterConfirmationCodeTextField: UITextField {
// MARK: Life cycle
override func awakeFromNib() {
super.awakeFromNib()
delegate = self
}
// MARK: Methods
override func deleteBackward() {
super.deleteBackward()
let previousTag = self.tag - 1
if let previousResponder = self.superview?.viewWithTag(previousTag) {
previousResponder.becomeFirstResponder()
}
}
}
Assign EnterConfirmationCodeTextField for each of your textFields and set they appropriate tag value.

I have taken one Hidden text field & four imageViews for that with two images. One for Blank and other for Bullet same as iOS default.
Also set tags for four imageviews.
On Load set Focus for Pin Code
- (void)startPinCode
{
txtPinCodeLockDigits.text = #"";
for (int i = 1; i <= 4; i++) {
UIImageView *img = (UIImageView *)[self.view viewWithTag:i];
[img setImage:[UIImage imageNamed:#"Img_BG_PinCode.png"]];
}
[txtPinCodeLockDigits becomeFirstResponder];
}
Then change imageview's images as per user input and only allow four characters
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *result = [textField.text stringByReplacingCharactersInRange:range withString:string];
textField.text = result;
for (int i = 1; i <= 4; i++) {
UIImageView *img = (UIImageView *)[self.view viewWithTag:i];
if (i <= [result length])
[img setImage:[UIImage imageNamed:#"Img_BG_PinCode_Filled.png"]];
else
[img setImage:[UIImage imageNamed:#"Img_BG_PinCode.png"]];
}
NSLog(#"Result :: %#", result);
if ([result length] == 4) {
[self performSelector:#selector(keyGenerationForApplication:) withObject:result afterDelay:0.2];
}
return NO;
}
After Four characters call function for generated PIN Code and store it in User Defaults same as iOS default PIN settings
- (void)keyGenerationForApplication:(NSString *)pinCode
{
int appCode = [pinCode intValue];
[DefaultsValues setIntegerValueToUserDefaults:appCode ForKey:PIN_LOCK_PATTERN];
}
Here, you can again call StartPinCode method for re-confirming code.
Hopefully, it'll help you.
Thanks

It can be achieve using UITextField delegate & by setting Tag for each Textfield in increasing order (say 1 - 4), below is the delegate handler to solve the issue.
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
// On inputing value to textfield
if (textField.text?.characters.count < 1 && string.characters.count > 0){
let nextTag = textField.tag + 1;
// get next responder
var nextResponder = textField.superview?.viewWithTag(nextTag);
if (nextResponder == nil){
nextResponder = textField.superview?.viewWithTag(1);
}
textField.text = string;
nextResponder?.becomeFirstResponder();
return false;
}
else if (textField.text?.characters.count >= 1 && string.characters.count == 0){
// on deleteing value from Textfield
let previousTag = textField.tag - 1;
// get next responder
var previousResponder = textField.superview?.viewWithTag(previousTag);
if (previousResponder == nil){
previousResponder = textField.superview?.viewWithTag(1);
}
textField.text = "";
previousResponder?.becomeFirstResponder();
return false;
}
return true;
}

swift 2.3
class BankDepositsWithOTPVC: UIViewController {
let limitLength = 1
override func viewDidLoad() {
super.viewDidLoad()
}
}
// MARK: Textfield Validator
extension BankDepositsWithOTPVC : UITextFieldDelegate {
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
// On inputing value to textfield
if (textField.text?.characters.count < 1 && string.characters.count > 0){
let nextTag = textField.tag + 1;
// get next responder
let nextResponder = textField.superview?.viewWithTag(nextTag);
if (nextResponder == nil){
textField.resignFirstResponder()
// nextResponder = textField.superview?.viewWithTag(1);
}
textField.text = string;
nextResponder?.becomeFirstResponder();
return false;
}else if (textField.text?.characters.count >= 1 && string.characters.count > 0){
// maximum 1 digit
textField.text = "";
let nextTag = textField.tag + 1;
// get next responder
let nextResponder = textField.superview?.viewWithTag(nextTag);
if (nextResponder == nil){
textField.resignFirstResponder()
// nextResponder = textField.superview?.viewWithTag(1);
}
textField.text = string;
nextResponder?.becomeFirstResponder();
return false;
}
else if (textField.text?.characters.count >= 1 && string.characters.count == 0){
// on deleteing value from Textfield
let previousTag = textField.tag - 1;
// get next responder
var previousResponder = textField.superview?.viewWithTag(previousTag);
if (previousResponder == nil){
previousResponder = textField.superview?.viewWithTag(1);
}
textField.text = "";
previousResponder?.becomeFirstResponder();
return false;
}
//return true;
guard let text = textField.text else { return true }
let newLength = text.characters.count + string.characters.count - range.length
return newLength <= limitLength
}
}
Objective-C
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if ((textField.text.length < 1) && (string.length > 0))
{
NSInteger nextTag = textField.tag + 1;
UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
if (! nextResponder){
[textField resignFirstResponder];
}
textField.text = string;
if (nextResponder)
[nextResponder becomeFirstResponder];
return NO;
}else if ((textField.text.length >= 1) && (string.length > 0)){
//FOR MAXIMUM 1 TEXT
NSInteger nextTag = textField.tag + 1;
UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
if (! nextResponder){
[textField resignFirstResponder];
}
textField.text = string;
if (nextResponder)
[nextResponder becomeFirstResponder];
return NO;
}
else if ((textField.text.length >= 1) && (string.length == 0)){
// on deleteing value from Textfield
NSInteger prevTag = textField.tag - 1;
// Try to find prev responder
UIResponder* prevResponder = [textField.superview viewWithTag:prevTag];
if (! prevResponder){
[textField resignFirstResponder];
}
textField.text = string;
if (prevResponder)
// Found next responder, so set it.
[prevResponder becomeFirstResponder];
return NO;
}
return YES;
}

Modified Anurag Soni's answer in Swift 3.
It assumes you have outlet collection named textFields and the text fields have ordered tags set
It adds case when there's already some digit in text field and when user types something new - the digit is replaced
Input is restricted to digits only
It prevents from pasting more than one digit
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
// Restrict to only digits
let aSet = NSCharacterSet(charactersIn:"0123456789").inverted
let compSepByCharInSet = string.components(separatedBy: aSet)
let numberFiltered = compSepByCharInSet.joined(separator: "")
if string != numberFiltered {
return false
}
// Get the unwrapped text
guard let text = textField.text else {
return false
}
if (text.characters.count < 1 && string.characters.count == 1) {
// New value to empty text field
textField.text = string
// Next responder
if let someTextField = (textFields.filter { $0.tag == textField.tag + 1 }).first {
someTextField.becomeFirstResponder()
} else {
view.endEditing(true)
}
return false
} else if (text.characters.count >= 1 && string.characters.count == 0){
// On deleting value from Textfield
textField.text = ""
// Previous responder
if let someTextField = (textFields.filter { $0.tag == textField.tag - 1 }).first {
someTextField.becomeFirstResponder()
} else {
view.endEditing(true)
}
return false
} else if string.characters.count == 1 {
// There's already some digit in text field
// Replace it with new one
textField.text = string
// Next responder
if let someTextField = (textFields.filter { $0.tag == textField.tag + 1 }).first {
someTextField.becomeFirstResponder()
} else {
view.endEditing(true)
}
}
return false
}

Provide the tag to the textfield like 1,2,3,4 and directly use it
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
let next:NSInteger
if string == "" {
next = textField.tag - 1;
}
else{
next = textField.tag + 1;
}
if (textField.text?.characters.count)! >= 1 {
if textField.tag == 4 {
if string == "" {
textField.text = ""
let temptf = self.view.viewWithTag(next) as! UITextField
temptf.becomeFirstResponder()
return false
}
else{
if (textField.text?.characters.count)! > 1 {
let stringg = textField.text!
textField.text = stringg.replacingOccurrences(of: stringg, with: string)
}
return false
}
}
else{
if string == "" {
textField.text = ""
if next != 0 {
let temptf = self.view.viewWithTag(next) as! UITextField
temptf.becomeFirstResponder()
}
return false
}
else{
if (textField.text?.characters.count)! > 1 {
let stringg = textField.text!
textField.text = stringg.replacingOccurrences(of: stringg, with: string)
}
let temptf = self.view.viewWithTag(next) as! UITextField
temptf.becomeFirstResponder()
}
}
}
return true
}

Swift 4
when you input a number from the pin code into a text field, you should let a number be shown and then the next text field will become the first responder, so change the first responder after the code was in the text view
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard let textField = textField as? PinCodeTextField else { return true }
if string == "" {// when the backward clicked, let it go :)
return true
}
// when textfield is not empty, well, next number
if textField.pinCode.count == textField.maxCount {
becomeFirstResponder(after: textField)
return false
}
if string.count > textField.maxCharacterCount {// the max character count should be 1
return false
}
return true
}
// now the text field has been filled with a number
func textFieldCotentDidChange(_ textField: UITextField) {
print("didchange")
guard let textField = textField as? PinCodeTextField else { return }
if textField.pinCode.count == 0 {
becomeFirstResponder(before: textField)
}
// when textfield has been filled, ok! next!
if textField.pinCode.count == textField.maxCharacterCount {
becomeFirstResponder(after: textField)
}
}
for more details and the simple demo, see this link

Try this
sample tutorial passcode lock
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController<UITextFieldDelegate>
{
IBOutlet UITextField *txtPassword;
}
#end
ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
txtPassword.delegate=self;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSUInteger newLength = [textField.text length] + [string length] - range.length;
return (newLength > 1) ? NO : YES;
}

Just use TextFieldDelegate method and check the length of the TextField after every changes
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let newString = (textField.text! as NSString).stringByReplacingCharactersInRange(range, withString: string)
if newString.characters.count == 1
{
nextTextField.becomeFirstResponder()
return true
}
else
{
return false
}
}

I was working on a similar functionality and did it in my way. Solution below.
Swift 4
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
//For clear button pressed
//If the textfield has already text in it
if string.count == 0 {
textField.text = string
return true
}
//For First time entry into the text field
guard let text = textField.text, text.count <= 0 else {
//If user enter second character
return false
}
//For First time entry into the text field
if text.count == 0 {
textField.text = string
textField.resignFirstResponder()
self.nextResponde(tag: textField.tag)
return true
}
return false
}
//To make the next field as responder
func nextResponde(tag: Int) {
switch tag {
case self.PINTextField.tag:
guard let text = self.PINTextField1.text, text.count == 1 else {
self.PINTextField1.becomeFirstResponder()
return
}
case self.PINTextField1.tag:
guard let text = self.PINTextField2.text, text.count == 1 else {
self.PINTextField2.becomeFirstResponder()
return
}
case self.PINTextField2.tag:
guard let text = self.PINTextField3.text, text.count == 1 else {
self.PINTextField3.becomeFirstResponder()
return
}
default:
let _ = tag
}
}

Credit to #Anurag Soni, here's an alternate UX pushing onto the next field and dismissing upon completion (with layout logic also above):
-(UIView *)addSpacedTextInputToView:(UIView *)parent subtitle:(NSString *)subtitle spaces:(int)spaces atOffset:(float)offset{
float parentWidth = parent.frame.size.width;
float tfWidth = 2 * increment;
float tfHeight = 3 * increment;
float startX = 3 * increment;
float remainingWidth = parentWidth - 2 * startX - spaces * tfWidth;
float padding = remainingWidth / (float)(spaces-1);
UIView * holder = [self addViewToView:parent withFrame:CGRectMake(0, offset, parentWidth, 0)];
UIView * tfHolder = [self addViewToView:holder withFrame:CGRectMake(0, 0, parentWidth, tfHeight)];
float localX = startX;
float localY = 0.0f;
for (int n = 0; n < spaces; n++){
UITextField * tf = [UITextField new];
tf.frame = CGRectMake(localX, localY, tfWidth, tfHeight);
tf.textColor = [UIColor whiteColor];
tf.font = headerFont;
tf.textAlignment = NSTextAlignmentCenter;
tf.tintColor = [UIColor whiteColor];
tf.keyboardAppearance = UIKeyboardAppearanceDark;
tf.tag = n;
tf.delegate = self;
[tfHolder addSubview:tf];
UIView * div = [self addViewToView:tf withFrame:CGRectMake(0, tfHeight-2, tfWidth, 1)];
div.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.5f];
localX += tfWidth;
localX += padding;
}
localY += tfHeight;
localY += increment;
localY += [self addSystemLabelToView:holder colour:nil text:subtitle maxWidth:0 offset:localY].frame.size.height;
holder.frame = CGRectMake(0, offset, parent.frame.size.width, localY);
return holder;
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if (string.length > 0){
textField.text = string.uppercaseString; //optional to uppercase
UIView * div = textField.subviews.firstObject; //change div colour
div.backgroundColor = [UIColor whiteColor];
int nextTag = (int)textField.tag + 1;
UIResponder * nextResponder = [textField.superview viewWithTag:nextTag];
if (!nextResponder){
[textField resignFirstResponder];
}
if (nextResponder){
[nextResponder becomeFirstResponder];
}
}
return false;
}

Objective-c answer of Varun Naharia solution.
Solution using IBOutletCollections
#property (strong, nonatomic) IBOutletCollection(UITextField) NSArray *otpTextFields;
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if(textField.text.length < 1 && string.length > 0) {
for(int i=0; i<self.otpTextFields.count-1; i++) {
if(textField == self.otpTextFields[i]) {
UITextField* nextTextField = (UITextField*) self.otpTextFields[i+1];
[nextTextField becomeFirstResponder];
}
}
textField.text = string;
return NO;
} else if (textField.text.length >= 1 && string.length == 0) {
for(int i=(int) self.otpTextFields.count-1;i>=1;i--) {
if(textField == self.otpTextFields[i]) {
UITextField* nextTextField = (UITextField*) self.otpTextFields[i-1];
[nextTextField becomeFirstResponder];
}
}
textField.text = #"";
return NO;
} else if(textField.text.length >= 1){
textField.text = string;
return NO;
}
return YES;
}

try this : - For swift 3.0
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
// On inputing value to textfield
if ((textField.text?.characters.count)! < 1 && string.characters.count > 0){
let nextTag = textField.tag + 1;
// get next responder
let nextResponder = textField.superview?.viewWithTag(nextTag);
textField.text = string;
if (nextResponder == nil){
textField.resignFirstResponder()
}
nextResponder?.becomeFirstResponder();
return false;
}
else if ((textField.text?.characters.count)! >= 1 && string.characters.count == 0){
// on deleting value from Textfield
let previousTag = textField.tag - 1;
// get next responder
var previousResponder = textField.superview?.viewWithTag(previousTag);
if (previousResponder == nil){
previousResponder = textField.superview?.viewWithTag(1);
}
textField.text = "";
previousResponder?.becomeFirstResponder();
return false;
}
return true;
}
converted Anurag Soni answer in swift 3.0
You just have to implement this method only.

Related

Cursor goes to end after setting text to UITextField inside shouldChangeCharactersIn

I have text label which has phone number in it. I mask the phone number when user is typing so that in shouldChangeCharactersIn function;
I get user input (string)
Add that input to text which is already written in UITextField
Mask text and set it to UITextField
Return false
My question is that after I set text of UITextfield (delete or add new character UITextField, cursor moves to the end but I want it to stay in the same position. (By meaning same position, I mean same when I don't implement shouldChangeCharactersIn function) How can I do that? Thank you.
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard CharacterSet(charactersIn: "0123456789").isSuperset(of: CharacterSet(charactersIn: string)) else {
return false
}
if let text = textField.text {
let newLength = text.count + string.count - range.length
let newText = text + string
let textFieldText: NSString = (textField.text ?? "") as NSString
let txtAfterUpdate = textFieldText.replacingCharacters(in: range, with: string)
if(newLength <= 15){
//textField.text = txtAfterUpdate
textField.text = txtAfterUpdate.digits.applyPatternOnNumbers()
return false
}
return newLength <= 15
}
return true
}
Mask Function:
func applyPatternOnNumbers(pattern: String = "(###) ### ## ##", replacmentCharacter: Character = "#") -> String {
var pureNumber = self.replacingOccurrences( of: "[^0-9]", with: "", options: .regularExpression)
for index in 0 ..< pattern.count {
guard index < pureNumber.count else { return pureNumber }
let stringIndex = String.Index(encodedOffset: index)
let patternCharacter = pattern[stringIndex]
guard patternCharacter != replacmentCharacter else { continue }
pureNumber.insert(patternCharacter, at: stringIndex)
}
return pureNumber
}
What I want in GIF
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard CharacterSet(charactersIn: "0123456789").isSuperset(of: CharacterSet(charactersIn: string)) else {
return false
}
if let text = textField.text {
let newLength = text.count + string.count - range.length
let newText = text + string
let textFieldText: NSString = (textField.text ?? "") as NSString
let txtAfterUpdate = textFieldText.replacingCharacters(in: range, with: string)
if(newLength <= 15){
var currentPosition = 0
if let selectedRange = textField.selectedTextRange {
currentPosition = textField.offset(from: textField.beginningOfDocument, to: selectedRange.start)
}
if(string == ""){
if(currentPosition == 2){
if(textField.text!.count > 2){
currentPosition = 1
}else{
currentPosition = 0
}
}else if(currentPosition == 7){
currentPosition = 4
}else if(currentPosition == 11){
currentPosition = 9
}else if(currentPosition == 14){
currentPosition = 12
}else{
if(currentPosition != 1){
currentPosition = currentPosition - 1
}
}
}else{
if(currentPosition == 0){
currentPosition = 2
}else if(currentPosition == 4){
currentPosition = 7
}else if(currentPosition == 9){
currentPosition = 11
}else if(currentPosition == 12){
currentPosition = 14
}else{
currentPosition = currentPosition + 1
}
}
textField.text = txtAfterUpdate.applyPatternOnNumbers()
print("textField Length -> : \(textField.text?.count ?? 0)")
if let newPosition = textField.position(from: textField.beginningOfDocument, offset: currentPosition) {
textField.selectedTextRange = textField.textRange(from: newPosition, to: newPosition)
}
return false
}
return newLength <= 15
}
return true
}

textfield auto focus not working properly

I have 6 text field text box. And when user finish enter in first text field, then it should automatically have to go second and so on. Here my code hats not working. when i enter the value in first text field, the values is entering in second ..here my code :
This below code i have done to enter only one digit in each text field. But here how can i add the text field responder...once i finish with first text field, adn so on...
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if textField == self.cardTextFieldOne{
guard let text = textField.text else {return true}
let newLength = text.characters.count + string.characters.count - range.length
return newLength <= 1
}else if textField == self.cardTextFieldTwo{
guard let text = textField.text else { return true }
let newLength = text.characters.count + string.characters.count - range.length
return newLength <= 1
}
else if textField == self.cardTextFieldTwo{
guard let text = textField.text else { return true }
let newLength = text.characters.count + string.characters.count - range.length
return newLength <= 1
}
else if textField == self.cardTextFieldThree{
guard let text = textField.text else { return true }
let newLength = text.characters.count + string.characters.count - range.length
return newLength <= 1
}
else if textField == self.cardTextFieldFour{
guard let text = textField.text else { return true }
let newLength = text.characters.count + string.characters.count - range.length
return newLength <= 1
}
else if textField == self.cardTextFieldFive{
guard let text = textField.text else { return true }
let newLength = text.characters.count + string.characters.count - range.length
return newLength <= 1
}
else if textField == self.cardTextFieldSix{
guard let text = textField.text else { return true }
let newLength = text.characters.count + string.characters.count - range.length
return newLength <= 1
}
else if textField == self.cardTextFieldMonth{
guard let text = textField.text else { return true }
let newLength = text.characters.count + string.characters.count - range.length
return newLength <= 2
}
else if textField == self.cardTextFieldYear{
guard let text = textField.text else { return true }
let newLength = text.characters.count + string.characters.count - range.length
return newLength <= 2
}
return true
}
Thanks in advance !!
Update
if textField == self.cardTextFieldOne{
guard let text = textField.text else {return true}
let newLength = text.characters.count + string.characters.count - range.length
if (string == "") || string.characters.count < 1
{
return true //This also allow you to backspace
}else if (cardTextFieldOne.text?.characters.count)! >= 1
{
cardTextFieldOne.resignFirstResponder();
self.cardTextFieldTwo.becomeFirstResponder()
return false
}
return newLength <= 1
}
Simple Solution
Create Method which called every time you change the textField text like below you can see in the Image, while you right click on textfield, you see the Editing change drag it and create method like
#IBAction func editing(_ sender: UITextField)
{
if sender == yourFirstTextField {
if sender.text?.characters.count == 1 {
yourNextTextField.becomeFirstResponder()
}
}
}
Try using textField's delegate method
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField == self.cardTextFieldOne{
textField.resignFirstResponder();
self.cardTextFieldTwo.becomeFirstResponder();
return false;
}else if textField == self.cardTextFieldTwo{
textField.resignFirstResponder();
self.cardTextFieldThree.becomeFirstResponder()
// if this is last field then return true
return true
}
}
Try to respond manually while textfield fill with single text.
if textField == self.cardTextFieldTwo{
guard let text = textField.text else { return true }
let newLength = text.characters.count + string.characters.count - range.length
if textField.text?.utf16.count==1 { // adjust your condition here
textField.resignFirstResponder()
self.cardTextFieldTwo.becomeFirstResponder()
}
return newLength <= 1
}
Note do this for all textfield
Use textFieldDidEndEditing method, than just call becomeFirstResponder() on next textField
func textFieldDidEndEditing(_ textField: UITextField) {
textField.becomeFirstResponder()
}

Swift UITextField deleteBackward Issue

I have a series of textfields in a single row. Each textfield is limited to two characters.
My problem is that I want to delete all the characters and go back through all the textfields until I get to the first textfield.
I have subclassed the textfield and overridden the deleteBackward method to determine if the text in the textfield is empty. If so then change the responder to the previous textfield.
This is all fine, but when there is one character in the textfield and backspace is pressed, the character is deleted and the responder is changed to the previous textfield.
This is incorrect behaviour as I want the focus to stay on that textfield that has had its character deleted and if the delete key is pressed again, then responder should change to previous textfield.
I corrected this my setting a flag in the shouldChangeCharacters delegate method of the textfield. The flag is set to true if ...
if (newLength == 0 && currentLength == 0) || currentLength == 0 {
textFieldEmpty = true
} else {
textFieldEmpty = false
}
This works fine, but when the textfield is empty, and backspace is pressed, this delegate method is not called?
Just wandering if anyone has solved a similar problem and what approach they may have taken?
Thanks
I have a similar scenario but with single characters in every text field. I have also used deleteBackward in a custom textField to achieve this.
Try this code:
import UIKit
protocol CustomTextFieldDelegate{
func textFieldDidDelete()
}
class CustomTextField: UITextField {
var myCustomTextFieldDelegate : CustomTextFieldDelegate!
override func deleteBackward() {
super.deleteBackward()
myCustomTextFieldDelegate.textFieldDidDelete()
}
}
And this is how you should implement the deleteBackward method:
func textFieldDidDelete() {
print("Entered Delete");
print("Tag!!\(currentActiveTextField.tag)")
var previousTag = 0
if currentActiveTextField.text == ""{
previousTag = currentActiveTextField.tag - 1
}else{
previousTag = currentActiveTextField.tag
}
let previousResponder = currentActiveTextField.superview?.viewWithTag(previousTag)
if(previousTag > 0){
let previousTextField = previousResponder as! CustomTextField
previousTextField.text = ""
currentActiveTextField = previousTextField
previousResponder?.becomeFirstResponder()
}
}
Also in shouldChangeCharacters in range method, dont forget to set your currentlyActiveTextField as the text field you wish to edit. Check the following code, might help. My text fields have tags 1,2,3,4.
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
currentActiveTextField = textField as! CustomTextField
let nextTag = textField.tag + 1;
// get next responder
var nextResponder = textField.superview?.viewWithTag(nextTag);
// On inputing value to textfield
if (textField.text?.characters.count < 1 && string.characters.count > 0){
if (nextResponder == nil){
nextResponder = textField.superview?.viewWithTag(1);
}
textField.text = string;
if nextTag == 5 {
self.digitFourTextField.resignFirstResponder()
return true;
}
nextResponder?.becomeFirstResponder();
return false;
}
if (textField.text?.characters.count >= 1 && string.characters.count == 0){
// on deleteing value from Textfield
let presentTag = textField.tag;
// get next responder
var presentResponder = textField.superview?.viewWithTag(presentTag);
if (presentResponder == nil){
presentResponder = textField.superview?.viewWithTag(1);
}
textField.text = "";
presentResponder?.becomeFirstResponder();
return false;
}
let currentCharacterCount = textField.text?.characters.count ?? 0
let newLength = currentCharacterCount + string.characters.count - range.length
if newLength > 1 {
textField.text = string
if (nextResponder != nil){
nextResponder?.becomeFirstResponder();
}
}
let returnVal = newLength <= 1
return returnVal;
}
Call the delegate method and then call super.deleteBackwards() after.
This way, at the time you call the delegate method the text.count == 1. If you flip the order and call super.deleteBackwards() first the text.count == 0 and you cannot accurately check for an empty string.

Best way to force a text field to have a single word only in swift? [duplicate]

I have a Verification ViewController, I get 4 digit verification code by SMS and I need to enter those code to login, I have created the ViewController like this
As you can see four UITextFields, I need to allow only single digit for each UITextField,
What I tried: I was trying to use shouldChangeCharactersInRange:method: , but its not getting called, I don't know what's wrong, I think because UITextFields are in UITableView so it is not working.
You can change the text field like this by using the delegate function of the text field. Initially, you need to set the delegate and the tag of each text field.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if ((textField.text.length >= 1) && (string.length > 0))
{
NSInteger nextTag = textField.tag + 1;
// Try to find next responder
UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
if (! nextResponder)
nextResponder = [textField.superview viewWithTag:1];
if (nextResponder)
// Found next responder, so set it.
[nextResponder becomeFirstResponder];
return NO;
}
return YES;
}
Swift 2
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
// On inputing value to textfield
if (textField.text?.characters.count < 1 && string.characters.count > 0){
let nextTag = textField.tag + 1;
// get next responder
var nextResponder = textField.superview?.viewWithTag(nextTag);
if (nextResponder == nil){
nextResponder = textField.superview?.viewWithTag(1);
}
textField.text = string;
nextResponder?.becomeFirstResponder();
return false;
}
else if (textField.text?.characters.count >= 1 && string.characters.count == 0){
// on deleting value from Textfield
let previousTag = textField.tag - 1;
// get next responder
var previousResponder = textField.superview?.viewWithTag(previousTag);
if (previousResponder == nil){
previousResponder = textField.superview?.viewWithTag(1);
}
textField.text = "";
previousResponder?.becomeFirstResponder();
return false;
}
return true;
}
Swift 4
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if textField.text!.count < 1 && string.count > 0{
let nextTag = textField.tag + 1
// get next responder
var nextResponder = textField.superview?.viewWithTag(nextTag)
if (nextResponder == nil){
nextResponder = textField.superview?.viewWithTag(1)
}
textField.text = string
nextResponder?.becomeFirstResponder()
return false
}
else if textField.text!.count >= 1 && string.count == 0{
// on deleting value from Textfield
let previousTag = textField.tag - 1
// get next responder
var previousResponder = textField.superview?.viewWithTag(previousTag)
if (previousResponder == nil){
previousResponder = textField.superview?.viewWithTag(1)
}
textField.text = ""
previousResponder?.becomeFirstResponder()
return false
}
return true
}
Use this code if you don't want to work with tag and it works better then above
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
// On inputing value to textfield
if ((textField.text?.characters.count)! < 1 && string.characters.count > 0){
if(textField == txtOne)
{
txtTwo.becomeFirstResponder()
}
if(textField == txtTwo)
{
txtThree.becomeFirstResponder()
}
if(textField == txtThree)
{
txtFour.becomeFirstResponder()
}
textField.text = string
return false
}
else if ((textField.text?.characters.count)! >= 1 && string.characters.count == 0){
// on deleting value from Textfield
if(textField == txtTwo)
{
txtOne.becomeFirstResponder()
}
if(textField == txtThree)
{
txtTwo.becomeFirstResponder()
}
if(textField == txtFour)
{
txtThree.becomeFirstResponder()
}
textField.text = ""
return false
}
else if ((textField.text?.characters.count)! >= 1 )
{
textField.text = string
return false
}
return true
}
Swift 4
Inspired by #Anurag Soni and #Varun Naharia answers
Variant A
extension EnterConfirmationCodeTextField: UITextFieldDelegate {
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard let textFieldCount = textField.text?.count else { return false }
// Сlosure
let setValueAndMoveForward = {
textField.text = string
let nextTag = textField.tag + 1
if let nextResponder = textField.superview?.viewWithTag(nextTag) {
nextResponder.becomeFirstResponder()
}
}
// Сlosure
let clearValueAndMoveBack = {
textField.text = ""
let previousTag = textField.tag - 1
if let previousResponder = textField.superview?.viewWithTag(previousTag) {
previousResponder.becomeFirstResponder()
}
}
if textFieldCount < 1 && string.count > 0 {
setValueAndMoveForward()
if textField.tag == 4 {
print("Do something")
}
return false
} else if textFieldCount >= 1 && string.count == 0 {
clearValueAndMoveBack()
return false
} else if textFieldCount >= 1 && string.count > 0 {
let nextTag = self.tag + 1
if let previousResponder = self.superview?.viewWithTag(nextTag) {
previousResponder.becomeFirstResponder()
if let activeTextField = previousResponder as? UITextField {
activeTextField.text = string
}
}
return false
}
return true
}
}
Variant B (a little bit another behavior):
extension EnterConfirmationCodeTextField: UITextFieldDelegate {
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard let textFieldCount = textField.text?.count else { return false }
// Сlosure
let setValueAndMoveForward = {
textField.text = string
let nextTag = textField.tag + 1
if let nextResponder = textField.superview?.viewWithTag(nextTag) {
nextResponder.becomeFirstResponder()
}
}
// Сlosure
let clearValueAndMoveBack = {
textField.text = ""
let previousTag = textField.tag - 1
if let previousResponder = textField.superview?.viewWithTag(previousTag) {
previousResponder.becomeFirstResponder()
}
}
if textFieldCount < 1 && string.count > 0 {
setValueAndMoveForward()
if textField.tag == 4 {
print("Do something")
}
return false
} else if textFieldCount >= 1 && string.count == 0 {
clearValueAndMoveBack()
return false
} else if textFieldCount >= 1 {
setValueAndMoveForward()
return false
}
return true
}
}
Also, I implemented this feature:
In the case where the last textFiled is empty, I just want to switch to the previous textFiled. I tried all this methods. But as for me the method below more elegant and works like a charm:
Variant A
class EnterConfirmationCodeTextField: UITextField {
// MARK: Life cycle
override func awakeFromNib() {
super.awakeFromNib()
delegate = self
}
// MARK: Methods
override func deleteBackward() {
super.deleteBackward()
let previousTag = self.tag - 1
if let previousResponder = self.superview?.viewWithTag(previousTag) {
previousResponder.becomeFirstResponder()
if let activeTextField = previousResponder as? UITextField {
if let isEmpty = activeTextField.text?.isEmpty, !isEmpty {
activeTextField.text = String()
}
}
}
}
}
Variant B (a little bit another behavior):
class EnterConfirmationCodeTextField: UITextField {
// MARK: Life cycle
override func awakeFromNib() {
super.awakeFromNib()
delegate = self
}
// MARK: Methods
override func deleteBackward() {
super.deleteBackward()
let previousTag = self.tag - 1
if let previousResponder = self.superview?.viewWithTag(previousTag) {
previousResponder.becomeFirstResponder()
}
}
}
Assign EnterConfirmationCodeTextField for each of your textFields and set they appropriate tag value.
I have taken one Hidden text field & four imageViews for that with two images. One for Blank and other for Bullet same as iOS default.
Also set tags for four imageviews.
On Load set Focus for Pin Code
- (void)startPinCode
{
txtPinCodeLockDigits.text = #"";
for (int i = 1; i <= 4; i++) {
UIImageView *img = (UIImageView *)[self.view viewWithTag:i];
[img setImage:[UIImage imageNamed:#"Img_BG_PinCode.png"]];
}
[txtPinCodeLockDigits becomeFirstResponder];
}
Then change imageview's images as per user input and only allow four characters
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *result = [textField.text stringByReplacingCharactersInRange:range withString:string];
textField.text = result;
for (int i = 1; i <= 4; i++) {
UIImageView *img = (UIImageView *)[self.view viewWithTag:i];
if (i <= [result length])
[img setImage:[UIImage imageNamed:#"Img_BG_PinCode_Filled.png"]];
else
[img setImage:[UIImage imageNamed:#"Img_BG_PinCode.png"]];
}
NSLog(#"Result :: %#", result);
if ([result length] == 4) {
[self performSelector:#selector(keyGenerationForApplication:) withObject:result afterDelay:0.2];
}
return NO;
}
After Four characters call function for generated PIN Code and store it in User Defaults same as iOS default PIN settings
- (void)keyGenerationForApplication:(NSString *)pinCode
{
int appCode = [pinCode intValue];
[DefaultsValues setIntegerValueToUserDefaults:appCode ForKey:PIN_LOCK_PATTERN];
}
Here, you can again call StartPinCode method for re-confirming code.
Hopefully, it'll help you.
Thanks
It can be achieve using UITextField delegate & by setting Tag for each Textfield in increasing order (say 1 - 4), below is the delegate handler to solve the issue.
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
// On inputing value to textfield
if (textField.text?.characters.count < 1 && string.characters.count > 0){
let nextTag = textField.tag + 1;
// get next responder
var nextResponder = textField.superview?.viewWithTag(nextTag);
if (nextResponder == nil){
nextResponder = textField.superview?.viewWithTag(1);
}
textField.text = string;
nextResponder?.becomeFirstResponder();
return false;
}
else if (textField.text?.characters.count >= 1 && string.characters.count == 0){
// on deleteing value from Textfield
let previousTag = textField.tag - 1;
// get next responder
var previousResponder = textField.superview?.viewWithTag(previousTag);
if (previousResponder == nil){
previousResponder = textField.superview?.viewWithTag(1);
}
textField.text = "";
previousResponder?.becomeFirstResponder();
return false;
}
return true;
}
swift 2.3
class BankDepositsWithOTPVC: UIViewController {
let limitLength = 1
override func viewDidLoad() {
super.viewDidLoad()
}
}
// MARK: Textfield Validator
extension BankDepositsWithOTPVC : UITextFieldDelegate {
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
// On inputing value to textfield
if (textField.text?.characters.count < 1 && string.characters.count > 0){
let nextTag = textField.tag + 1;
// get next responder
let nextResponder = textField.superview?.viewWithTag(nextTag);
if (nextResponder == nil){
textField.resignFirstResponder()
// nextResponder = textField.superview?.viewWithTag(1);
}
textField.text = string;
nextResponder?.becomeFirstResponder();
return false;
}else if (textField.text?.characters.count >= 1 && string.characters.count > 0){
// maximum 1 digit
textField.text = "";
let nextTag = textField.tag + 1;
// get next responder
let nextResponder = textField.superview?.viewWithTag(nextTag);
if (nextResponder == nil){
textField.resignFirstResponder()
// nextResponder = textField.superview?.viewWithTag(1);
}
textField.text = string;
nextResponder?.becomeFirstResponder();
return false;
}
else if (textField.text?.characters.count >= 1 && string.characters.count == 0){
// on deleteing value from Textfield
let previousTag = textField.tag - 1;
// get next responder
var previousResponder = textField.superview?.viewWithTag(previousTag);
if (previousResponder == nil){
previousResponder = textField.superview?.viewWithTag(1);
}
textField.text = "";
previousResponder?.becomeFirstResponder();
return false;
}
//return true;
guard let text = textField.text else { return true }
let newLength = text.characters.count + string.characters.count - range.length
return newLength <= limitLength
}
}
Objective-C
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if ((textField.text.length < 1) && (string.length > 0))
{
NSInteger nextTag = textField.tag + 1;
UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
if (! nextResponder){
[textField resignFirstResponder];
}
textField.text = string;
if (nextResponder)
[nextResponder becomeFirstResponder];
return NO;
}else if ((textField.text.length >= 1) && (string.length > 0)){
//FOR MAXIMUM 1 TEXT
NSInteger nextTag = textField.tag + 1;
UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
if (! nextResponder){
[textField resignFirstResponder];
}
textField.text = string;
if (nextResponder)
[nextResponder becomeFirstResponder];
return NO;
}
else if ((textField.text.length >= 1) && (string.length == 0)){
// on deleteing value from Textfield
NSInteger prevTag = textField.tag - 1;
// Try to find prev responder
UIResponder* prevResponder = [textField.superview viewWithTag:prevTag];
if (! prevResponder){
[textField resignFirstResponder];
}
textField.text = string;
if (prevResponder)
// Found next responder, so set it.
[prevResponder becomeFirstResponder];
return NO;
}
return YES;
}
Modified Anurag Soni's answer in Swift 3.
It assumes you have outlet collection named textFields and the text fields have ordered tags set
It adds case when there's already some digit in text field and when user types something new - the digit is replaced
Input is restricted to digits only
It prevents from pasting more than one digit
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
// Restrict to only digits
let aSet = NSCharacterSet(charactersIn:"0123456789").inverted
let compSepByCharInSet = string.components(separatedBy: aSet)
let numberFiltered = compSepByCharInSet.joined(separator: "")
if string != numberFiltered {
return false
}
// Get the unwrapped text
guard let text = textField.text else {
return false
}
if (text.characters.count < 1 && string.characters.count == 1) {
// New value to empty text field
textField.text = string
// Next responder
if let someTextField = (textFields.filter { $0.tag == textField.tag + 1 }).first {
someTextField.becomeFirstResponder()
} else {
view.endEditing(true)
}
return false
} else if (text.characters.count >= 1 && string.characters.count == 0){
// On deleting value from Textfield
textField.text = ""
// Previous responder
if let someTextField = (textFields.filter { $0.tag == textField.tag - 1 }).first {
someTextField.becomeFirstResponder()
} else {
view.endEditing(true)
}
return false
} else if string.characters.count == 1 {
// There's already some digit in text field
// Replace it with new one
textField.text = string
// Next responder
if let someTextField = (textFields.filter { $0.tag == textField.tag + 1 }).first {
someTextField.becomeFirstResponder()
} else {
view.endEditing(true)
}
}
return false
}
Provide the tag to the textfield like 1,2,3,4 and directly use it
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
let next:NSInteger
if string == "" {
next = textField.tag - 1;
}
else{
next = textField.tag + 1;
}
if (textField.text?.characters.count)! >= 1 {
if textField.tag == 4 {
if string == "" {
textField.text = ""
let temptf = self.view.viewWithTag(next) as! UITextField
temptf.becomeFirstResponder()
return false
}
else{
if (textField.text?.characters.count)! > 1 {
let stringg = textField.text!
textField.text = stringg.replacingOccurrences(of: stringg, with: string)
}
return false
}
}
else{
if string == "" {
textField.text = ""
if next != 0 {
let temptf = self.view.viewWithTag(next) as! UITextField
temptf.becomeFirstResponder()
}
return false
}
else{
if (textField.text?.characters.count)! > 1 {
let stringg = textField.text!
textField.text = stringg.replacingOccurrences(of: stringg, with: string)
}
let temptf = self.view.viewWithTag(next) as! UITextField
temptf.becomeFirstResponder()
}
}
}
return true
}
Swift 4
when you input a number from the pin code into a text field, you should let a number be shown and then the next text field will become the first responder, so change the first responder after the code was in the text view
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard let textField = textField as? PinCodeTextField else { return true }
if string == "" {// when the backward clicked, let it go :)
return true
}
// when textfield is not empty, well, next number
if textField.pinCode.count == textField.maxCount {
becomeFirstResponder(after: textField)
return false
}
if string.count > textField.maxCharacterCount {// the max character count should be 1
return false
}
return true
}
// now the text field has been filled with a number
func textFieldCotentDidChange(_ textField: UITextField) {
print("didchange")
guard let textField = textField as? PinCodeTextField else { return }
if textField.pinCode.count == 0 {
becomeFirstResponder(before: textField)
}
// when textfield has been filled, ok! next!
if textField.pinCode.count == textField.maxCharacterCount {
becomeFirstResponder(after: textField)
}
}
for more details and the simple demo, see this link
Try this
sample tutorial passcode lock
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController<UITextFieldDelegate>
{
IBOutlet UITextField *txtPassword;
}
#end
ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
txtPassword.delegate=self;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSUInteger newLength = [textField.text length] + [string length] - range.length;
return (newLength > 1) ? NO : YES;
}
Just use TextFieldDelegate method and check the length of the TextField after every changes
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let newString = (textField.text! as NSString).stringByReplacingCharactersInRange(range, withString: string)
if newString.characters.count == 1
{
nextTextField.becomeFirstResponder()
return true
}
else
{
return false
}
}
I was working on a similar functionality and did it in my way. Solution below.
Swift 4
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
//For clear button pressed
//If the textfield has already text in it
if string.count == 0 {
textField.text = string
return true
}
//For First time entry into the text field
guard let text = textField.text, text.count <= 0 else {
//If user enter second character
return false
}
//For First time entry into the text field
if text.count == 0 {
textField.text = string
textField.resignFirstResponder()
self.nextResponde(tag: textField.tag)
return true
}
return false
}
//To make the next field as responder
func nextResponde(tag: Int) {
switch tag {
case self.PINTextField.tag:
guard let text = self.PINTextField1.text, text.count == 1 else {
self.PINTextField1.becomeFirstResponder()
return
}
case self.PINTextField1.tag:
guard let text = self.PINTextField2.text, text.count == 1 else {
self.PINTextField2.becomeFirstResponder()
return
}
case self.PINTextField2.tag:
guard let text = self.PINTextField3.text, text.count == 1 else {
self.PINTextField3.becomeFirstResponder()
return
}
default:
let _ = tag
}
}
Credit to #Anurag Soni, here's an alternate UX pushing onto the next field and dismissing upon completion (with layout logic also above):
-(UIView *)addSpacedTextInputToView:(UIView *)parent subtitle:(NSString *)subtitle spaces:(int)spaces atOffset:(float)offset{
float parentWidth = parent.frame.size.width;
float tfWidth = 2 * increment;
float tfHeight = 3 * increment;
float startX = 3 * increment;
float remainingWidth = parentWidth - 2 * startX - spaces * tfWidth;
float padding = remainingWidth / (float)(spaces-1);
UIView * holder = [self addViewToView:parent withFrame:CGRectMake(0, offset, parentWidth, 0)];
UIView * tfHolder = [self addViewToView:holder withFrame:CGRectMake(0, 0, parentWidth, tfHeight)];
float localX = startX;
float localY = 0.0f;
for (int n = 0; n < spaces; n++){
UITextField * tf = [UITextField new];
tf.frame = CGRectMake(localX, localY, tfWidth, tfHeight);
tf.textColor = [UIColor whiteColor];
tf.font = headerFont;
tf.textAlignment = NSTextAlignmentCenter;
tf.tintColor = [UIColor whiteColor];
tf.keyboardAppearance = UIKeyboardAppearanceDark;
tf.tag = n;
tf.delegate = self;
[tfHolder addSubview:tf];
UIView * div = [self addViewToView:tf withFrame:CGRectMake(0, tfHeight-2, tfWidth, 1)];
div.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.5f];
localX += tfWidth;
localX += padding;
}
localY += tfHeight;
localY += increment;
localY += [self addSystemLabelToView:holder colour:nil text:subtitle maxWidth:0 offset:localY].frame.size.height;
holder.frame = CGRectMake(0, offset, parent.frame.size.width, localY);
return holder;
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if (string.length > 0){
textField.text = string.uppercaseString; //optional to uppercase
UIView * div = textField.subviews.firstObject; //change div colour
div.backgroundColor = [UIColor whiteColor];
int nextTag = (int)textField.tag + 1;
UIResponder * nextResponder = [textField.superview viewWithTag:nextTag];
if (!nextResponder){
[textField resignFirstResponder];
}
if (nextResponder){
[nextResponder becomeFirstResponder];
}
}
return false;
}
Objective-c answer of Varun Naharia solution.
Solution using IBOutletCollections
#property (strong, nonatomic) IBOutletCollection(UITextField) NSArray *otpTextFields;
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if(textField.text.length < 1 && string.length > 0) {
for(int i=0; i<self.otpTextFields.count-1; i++) {
if(textField == self.otpTextFields[i]) {
UITextField* nextTextField = (UITextField*) self.otpTextFields[i+1];
[nextTextField becomeFirstResponder];
}
}
textField.text = string;
return NO;
} else if (textField.text.length >= 1 && string.length == 0) {
for(int i=(int) self.otpTextFields.count-1;i>=1;i--) {
if(textField == self.otpTextFields[i]) {
UITextField* nextTextField = (UITextField*) self.otpTextFields[i-1];
[nextTextField becomeFirstResponder];
}
}
textField.text = #"";
return NO;
} else if(textField.text.length >= 1){
textField.text = string;
return NO;
}
return YES;
}
try this : - For swift 3.0
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
// On inputing value to textfield
if ((textField.text?.characters.count)! < 1 && string.characters.count > 0){
let nextTag = textField.tag + 1;
// get next responder
let nextResponder = textField.superview?.viewWithTag(nextTag);
textField.text = string;
if (nextResponder == nil){
textField.resignFirstResponder()
}
nextResponder?.becomeFirstResponder();
return false;
}
else if ((textField.text?.characters.count)! >= 1 && string.characters.count == 0){
// on deleting value from Textfield
let previousTag = textField.tag - 1;
// get next responder
var previousResponder = textField.superview?.viewWithTag(previousTag);
if (previousResponder == nil){
previousResponder = textField.superview?.viewWithTag(1);
}
textField.text = "";
previousResponder?.becomeFirstResponder();
return false;
}
return true;
}
converted Anurag Soni answer in swift 3.0
You just have to implement this method only.

How do I configure the text field to account for the expiry date?

I am entering credit card information and I want the expiry date to be a max of 5 characters 4 numbers and a "/" automatically enters after the first two characters i.e. 01/17 after typing 01 the / is automatically entered, and allows only two more characters "17".
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
{
if textField == expire{
}
return true
}
Swift 4 solution, based on #Jigar's answer:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if range.length > 0 {
return true
}
if string == "" {
return false
}
if range.location > 4 {
return false
}
var originalText = textField.text
let replacementText = string.replacingOccurrences(of: " ", with: "")
//Verify entered text is a numeric value
if !CharacterSet.decimalDigits.isSuperset(of: CharacterSet(charactersIn: replacementText)) {
return false
}
//Put / after 2 digit
if range.location == 2 {
originalText?.append("/")
textField.text = originalText
}
return true
}
Try like this, Its working at my end.
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
{
//Range.Lenth will greater than 0 if user is deleting text - Allow it to replce
if range.length > 0
{
if range.location == 3 {
var originalText = textField.text
originalText = originalText?.replacingOccurrences(of: "/", with: "")
textField.text = originalText
}
return true
}
//Dont allow empty strings
if string == " "
{
return false
}
//Check for max length including the spacers we added
if range.location >= 5
{
return false
}
var originalText = textField.text
let replacementText = string.stringByReplacingOccurrencesOfString(" ", withString: "")
//Verify entered text is a numeric value
let digits = NSCharacterSet.decimalDigitCharacterSet()
for char in replacementText.unicodeScalars
{
if !digits.longCharacterIsMember(char.value)
{
return false
}
}
//Put / space after 2 digit
if range.location == 2
{
originalText?.appendContentsOf("/")
textField.text = originalText
}
return true
}
Hope this help you.
The answers do not handle backspace really well. Here's my solution (implemented in Objective-C unfortunately):
#interface CreditCardViewController()
<UITextFieldDelegate>
#property (weak,nonatomic) IBOutlet UITextField * cardExpirationTextField;
#property (strong,nonatomic) NSString * previousExpiryDate;
#property (strong,nonatomic) UITextRange * previousExpiryDateSelection;
#property (assign,readonly,nonatomic) NSString * minYearLast2Digits;
#end
#implementation CreditCardViewController
-(instancetype) init
{
self = [super initWithNibName:NSStringFromClass([CreditCardViewController class]) bundle:nil];
if(self){
_minYearLast2Digits = [[[NSNumber numberWithInteger:[NSDate date].year] stringValue] substringFromIndex:2];
}
return self;
}
-(void) viewDidLoad
{
[super viewDidLoad];
[self setupView]
}
-(void) setupView
{
self.cardExpirationTextField.delegate = self;
[self.cardExpirationTextField addTarget:self
action:#selector(reformatCardExpiryDate:)
forControlEvents:UIControlEventEditingChanged];
}
-(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSUInteger newLength = [textField.text length] + [string length] - range.length;
if(textField == self.cardExpirationTextField){
self.previousExpiryDate = textField.text;
self.previousExpiryDateSelection = textField.selectedTextRange;
return newLength <= 5;
}
return YES;
}
-(void) reformatCardExpiryDate:(UITextField*) textField
{
const BOOL isErasing = self.previousExpiryDate.length > textField.text.length;
BOOL invalid = [textField.text length] > 5;
if([textField.text length] > 0){
unichar firstChar = [textField.text characterAtIndex:0];
invalid |= (firstChar > '1');
}
if([textField.text length] > 1){
unichar firstChar = [textField.text characterAtIndex:0];
unichar secondChar = [textField.text characterAtIndex:1];
invalid |= (firstChar == '1' && secondChar > '2');
}
if([textField.text length] > 2){
invalid |= [textField.text characterAtIndex:2] != '/';
}
if([textField.text length] > 3){
unichar yearFirstDigit = [textField.text characterAtIndex:3];
unichar minYearFirstDigit = [self.minYearLast2Digits characterAtIndex:0];
invalid |= yearFirstDigit < minYearFirstDigit;
}
if([textField.text length] > 4){
NSString* yearLastTwoDigits = [textField.text substringFromIndex:3];
invalid |= [yearLastTwoDigits compare:_minYearLast2Digits] == NSOrderedAscending;
}
if(invalid){
[textField setText:self.previousExpiryDate];
textField.selectedTextRange = self.previousExpiryDateSelection;
return;
}
if(!isErasing && textField.text.length == 2){
textField.text = [textField.text stringByAppendingString:#"/"];
UITextPosition *targetPosition =
[textField positionFromPosition:[textField beginningOfDocument]
offset:textField.text.length];
[textField setSelectedTextRange:
[textField textRangeFromPosition:targetPosition
toPosition:targetPosition]
];
}
}
#end
try this code:
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
{
let num:NSString = textField.text! ;
if num.length == 1
{
textField.text = NSString(format:"%#%#/",num,string) as String
return false;
}
if num.length >= 5 {
// allows only two more characters "17".
return false;
}
return true;
}
i just coded this and it work fine for me.
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
var index = range.lowerBound
let fecha = textField.text!
if string == "" {
var array = Array(fecha)
array.remove(at: index)
if array.count > 2 {
if !array.contains("/"){
array.insert("/", at: 2)
}
}
textField.text = String(array)
let cursorPosition = textField.position(from: textField.beginningOfDocument, offset: index)!
textField.selectedTextRange = textField.textRange(from: cursorPosition, to: cursorPosition)
}else{
let expiracion = fecha.replacingOccurrences(of: "/", with: "")
if fecha != expiracion{
if index > 1 {
index = range.lowerBound - 1
}
}
var array = Array(expiracion)
if array.count < 4 {
print("index: \(index) - \(array)")
let newChar = Character(string)
array.insert(newChar, at: index)
print("new array: ", array)
if array.count > 2 {
array.insert("/", at: 2)
}
textField.text = String(array)
index = index > 1 ? index + 2 : index + 1
let cursorPosition = textField.position(from: textField.beginningOfDocument, offset: index)!
textField.selectedTextRange = textField.textRange(from: cursorPosition, to: cursorPosition)
}
}
return false
}

Resources