1. Create two global variables to comply with Uitextfield agents
NSString *_previoustextfieldcontent;
Uitextrange *_previousselection;
2. Add an event setting agent to the TextField of the input bank card number
_bankcardnumtextfield.delegate=self
[_bankcardnumtextfield addtarget:self Action: @selector (Reformatasbankcardnumber:) forControlEvents: Uicontroleventeditingchanged];
3. Paste the following code in your. m file
#pragma mark-reformat the bank card number
-(void) Reformatasbankcardnumber: (Uitextfield *) TextField
{
//Determine the correct cursor position
Nsuinteger targetcursorpostion = [TextField offsetFromPosition:textField.beginningOfDocument toposition: TextField.selectedTextRange.start];
No bank card number with spaces inserted
NSString *bankcardnumberwithoutspaces = [self RemoveNonDigits:textField.text andpreservecursorposition:& Targetcursorpostion];
Avoid more than 19 bits of input
if (Bankcardnumberwithoutspaces.length > 19) {
[TextField settext:_previoustextfieldcontent];
Textfield.selectedtextrange = _previousselection;
Return
}
Get the bank card number after inserting a space
NSString *bankcardnumberwithspaces = [Self insertspaceseveryfourdigitsintostring:bankcardnumberwithoutspaces andpreservecursorposition:&targetcursorpostion];
Textfield.text = bankcardnumberwithspaces;
Uitextposition *targetpostion = [TextField positionFromPosition:textField.beginningOfDocument offset: Targetcursorpostion];
[TextField Setselectedtextrange:[textfield textrangefromposition:targetpostion toposition:targetpostion];
}
#pragma mark-Remove non-numeric characters (spaces) to determine the correct position of the cursor
String: Current string cursorposition: string after return processing of the cursor position
-(NSString *) Removenondigits: (NSString *) string andpreservecursorposition: (Nsuinteger *) Cursorposition
{
Nsuinteger originalcursorposition =*cursorposition;
nsmutablestring *digitsonlystring = [nsmutablestring new];
for (Nsuinteger i=0; i<string.length; i++) {
Unichar Charactertoadd = [string characteratindex:i];
if (IsDigit (Charactertoadd)) {
NSString *stringtoadd = [NSString stringwithcharacters:&charactertoadd length:1];
[Digitsonlystring Appendstring:stringtoadd];
}
else {
if (i<originalcursorposition) {
(*cursorposition)--;
}
}
}
return digitsonlystring;
}
#pragma mark-Inserts a space into our current string and determines the correct position of the cursor to prevent problems in the space
String: Current string cursorposition: Cursor position return: string with space after processing
-(NSString *) insertspaceseveryfourdigitsintostring: (NSString *) string andpreservecursorposition: (NSUInteger *) Cursorposition
{
nsmutablestring *stringwithaddedspaces = [nsmutablestring new];
Nsuinteger cursorpositioninspacelessstring = *cursorposition;
for (Nsuinteger i=0; i<string.length; i++) {
if (i > 0)
{
if (i%4 = = 0) {
[Stringwithaddedspaces appendstring:@ ""];
if (I < cursorpositioninspacelessstring) {
(*cursorposition) + +;
}
}
}
Unichar Charactertoadd = [string characteratindex:i];
NSString *stringtoadd = [NSString stringwithcharacters:&charactertoadd length:1];
[Stringwithaddedspaces Appendstring:stringtoadd];
}
return stringwithaddedspaces;
}
#pragma mark-uitextfielddelegate
-(BOOL) TextField: (Uitextfield *) TextField Shouldchangecharactersinrange: (nsrange) range replacementstring: ( NSString *) string
{
_previousselection = Textfield.selectedtextrange;
_previoustextfieldcontent = Textfield.text;
return YES;
}
Bank card number Split input