2

I have a UITextField which basically take 4 digit NSInteger. Now I want to bring the "Pass code" look into it. What I mean is there will be a certain space between each digit. Something like the below picture.

enter image description here

I know I can put space at the font & back side of my UITextField, but I want a certain distance in between my each input character. Is it possible in iOS? If yes than any kind of suggestion of guide line will be highly appreciated.

Thanks a lot in advance.

Isuru
  • 30,617
  • 60
  • 187
  • 303
Tulon
  • 4,011
  • 6
  • 36
  • 56

4 Answers4

3

First of all, add Tag to all four textfield, i.e 1,2,3 and 4.

and use below code to enter text in textfield. and here StrTxt is combination string of all four textfield.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{

int currentTag = (int)textField.tag;

if(string.length==0 && range.length==1)
{
    textField.text = @"";
    UITextField *newTextField = (UITextField *) [textField.superview viewWithTag:(currentTag-1)];
    [newTextField becomeFirstResponder];
    StrTxt = [StrTxt substringToIndex:currentTag-1];
    return NO;
}
else
    StrTxt = [StrTxt stringByAppendingString:string];

if((textField.text.length + string.length) >= 1)
{
    if(currentTag == 4)
    {
        //do your stuff here.

        if(textField.text.length<1)
            return YES;
        else
            return NO;
    }


    UITextField *newTextField = (UITextField *) [textField.superview viewWithTag:(currentTag+1)];
    if(newTextField.text.length==0)
        [newTextField becomeFirstResponder];
    if(textField.text.length==0)
    {
        textField.text = [textField.text stringByAppendingString:string];
        return NO;
    }
    else
    {
        if(currentTag+1 == 4)
        {
            if(newTextField.text.length>=1)
                return NO;

            //do your stuff here.
        }
        else
            if(newTextField.text.length>=1)
                return NO;
        return YES;
    }

}
return YES;
}
VD Patel
  • 286
  • 1
  • 6
  • Thanks for your comment. It's works fine. Except getting the combination of four text field data. :( Which supposed to be hold NSString *StrTxt; – Tulon May 19 '15 at 13:48
  • Nope the result can be found in `string`. It's working perfectly. Thanks man. :) – Tulon May 19 '15 at 15:00
2

If there are four textfields then it's very simple. Assign tags to your textfields, i.e 1,2,3,4.... Assign delegate to your textfields and use it's Delegate method

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField.text.length == 1) {
        switch (textField.tag) {
            case 1:
                [txt2 becomeFirstResponder];
                break;
            case 2:
                [txt3 becomeFirstResponder];
                break;
            case 3:
                [txt4 becomeFirstResponder];
            default:
                [textField resignFirstResponder];
                break;
        }
    }
    return YES;
}

Let me know if you want any further help

iGatiTech
  • 2,306
  • 1
  • 21
  • 45
  • There was a bit of problem using this. But thanks for your comment. Have a good day. :) – Tulon May 20 '15 at 06:10
1
 -(void)textViewDidChange:(UITextView *)textView
{

NSInteger num=textView.selectedRange.location;

NSString *final_key=textView.text;
        final_key=[final_key uppercaseString];

final_key=[final_key stringByReplacingOccurrencesOfString:@" " withString:@""];

    textView.text=@"";
        while(final_key.length>3)
        {
            NSString *str=[final_key substringToIndex:4];
            textView.text=[textView.text stringByAppendingFormat:@"%@ ",str];
            final_key=[final_key substringFromIndex:4];

        }

        textView.text=[textView.text stringByAppendingFormat:@"%@",final_key];

if(!delete_key_pressed)
        {

 NSString *ch=[NSString stringWithFormat:@"%c",[textView.text characterAtIndex:num-1]];

 //NSLog(@"%@",ch);

        if([ch isEqualToString:@" "])
        {
            num=num+1;
        }
        }
        else{
           // num=textView.selectedRange.location;
           NSString *ch=[NSString stringWithFormat:@"%c",[textView.text characterAtIndex:num-1]];
           // NSLog(@"%@",ch);
            if([ch isEqualToString:@" "])
            {
                num=num-1;
            }
        }
        textView.selectedRange=NSMakeRange(num, 0);


}
Tulon
  • 4,011
  • 6
  • 36
  • 56
Anoop
  • 409
  • 3
  • 13
-1

Answered in Swift 3.0

textfield.defaultTextAttributes.updateValue(spacing, forKey: NSKernAttributeName)

where spacing is a CGFloat. For example 2.0.

For more details check the answer provided by me at the link shared below.

How to set letter spacing of UITextField

However, it doesn't deals with Character Limit.

iOSer
  • 2,241
  • 1
  • 18
  • 26
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/18688507) – Luca D'Alberti Feb 01 '18 at 08:41
  • @LucaD'Alberti. As per your suggestion, I've included the code snippet. Hope you and others find it useful now :) – iOSer Feb 01 '18 at 09:55