Tulon Tulon - 4 months ago 16
Objective-C Question

Is it possible to set wide space in UITextField

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.

Answer

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;
}