1

Hi just wondering if it is possible, as the user is entering a string of numbers to dynamically add a hyphen every 5th character of the string...

any help would be greatly appreciated.

tinhead
  • 385
  • 2
  • 10
  • 29

4 Answers4

5

Try this code.

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

    NSString *separator = @"-";
    int seperatorInterval = 5;
    NSString *originalString = [textField.text stringByReplacingOccurrencesOfString:separator withString:@""];

    if (![originalString isEqualToString:@""] && ![string isEqualToString:@""]) {

        NSString *lastChar = [textField.text substringFromIndex:[textField.text length] - 1];
        int modulus = [originalString length] % seperatorInterval;

        if (![lastChar isEqualToString:separator] && modulus == 0) {

            textField.text = [textField.text stringByAppendingString:separator];
        }
    }

    return YES;
}
EmptyStack
  • 51,274
  • 23
  • 147
  • 178
  • Just working at implementing this one now, getting a couple of errors... but only because I'm a noob :P – tinhead Apr 27 '11 at 21:21
  • How do I connect this to my regTextField ? Also I'm not sure what I have to declare in the header for this method. – tinhead Apr 27 '11 at 23:15
  • Okay this worked perfectly, however what I was getting confused about is where to declare the textfields delegate, I was doing it inside this methods so it wasn't loading properly, I changed it to initalise my textfield delegate i the loadview method, and it worked fine. – tinhead Apr 28 '11 at 03:15
  • @tinhead: Mostly, set the delegate while you create the textfield. You can also set it somewhere else. But make sure the delegate has been set before any text is typed in the text field. – EmptyStack Apr 28 '11 at 03:46
1

Assuming you are using a UITextField for input, you can use the UITextFieldTextDidChangeNotification action to respond each time the text is modified. A quick and rough example:

- (IBAction)textChanged:(UITextField*)sender
{
    NSString* curText = sender.text;

    //when checking the length, you need to exclude hyphens from the count
    //which is currently not being done (thanks @titaniumdecoy)
    if([curText length] % 5 == 0)
    {
        sender.text = [curText stringByAppendingString:@"-"];
    }
}
Ryan Wersal
  • 3,210
  • 1
  • 20
  • 29
  • This won't allow you to delete via backspace past the last inserted hyphen. – titaniumdecoy Apr 27 '11 at 04:51
  • You are also not taking into account the fact that the hyphens themselves contribute to the length of the string. – titaniumdecoy Apr 27 '11 at 04:53
  • Sorry it's late here and for some reason I thought it was a UILabel implementation thing. I'll add a comment to think of that (since the extra code would be beyond the scope of the answer). Thanks for pointing it out. – Ryan Wersal Apr 27 '11 at 04:55
  • Just be sure to keep titaniumdecoy's concerns in mind, but good luck! – Ryan Wersal Apr 27 '11 at 05:22
0

Just declare and alloc a 2 strings. Store the numbers in one string(str 1) and count the string length every entry. Check when the string lenght will four digit, perform

str2 = [str1 appendByString:@"`"];

now str1 = str2;

again repeat this process in loop with increment of 4 digit.

Hopes it will help.

Sandeep Singh
  • 826
  • 2
  • 10
  • 24
  • hrmm, yes it helps, but I'm not sure if this will dynamically add the hyphens as the user types.... Say when the user types into a text field as they type I want the hyphen to pop up after the fifth character and not interfear with the user typing the sequence of numbers. – tinhead Apr 27 '11 at 04:48
  • It will helps when your final string will come. Don't think it will show you at that time the user input numbers. – Sandeep Singh Apr 27 '11 at 04:56
0

Just a concept try to make substrings of each 5 characters then append a hyphen to these then concatenate each one. for this you need to make an array of substrings.use this logical function

-(NSString *)makeMyString:(NSString *)stringA
{
NSMutableArray *tempArray1=[NSMutableArray array];
    //NSString *s=@"12345123451234512";
       NSString *s=stringA;
    BOOL flag=YES;
    while(flag)
    {
        NSString *str;
        if([s length]>=5)
          str=[s substringWithRange:NSMakeRange(0,5)];
        else 
            str=s;
        [tempArray1 addObject:str];
        str=nil;
        if([s length]>=5)
          s=[s substringWithRange:NSMakeRange(5,([s length]-5))];
        else 
            s=@"";
        if([s isEqualToString:@""])
            flag=NO;
    }

    NSString *makeString=@"";
    for(int i=0;i<[tempArray1 count];i++)
    {
        if([[tempArray1 objectAtIndex:i] length]==5)
          makeString =[NSString stringWithFormat:@"%@%@`",makeString,[tempArray1 objectAtIndex:i]];
        else {
             makeString =[NSString stringWithFormat:@"%@%@",makeString,[tempArray1 objectAtIndex:i]];
        }

    }
    NSLog(@"%@",makeString);
return makeString;
}
Ishu
  • 12,797
  • 5
  • 35
  • 51
  • The method cannot return the actual String instead of just the pointer to the string,so the header of that method looks like this - (NSString *)makeMyString:(NSString *)stringA However I'm having issues trying to pass this method across to my textfield. – tinhead Apr 27 '11 at 20:42
  • What I really want to be able to do is as the user is typing into a text field the hyphen will pop up as the user types... I just don't know how to do this.. – tinhead Apr 27 '11 at 21:02
  • now this code is fine and return a string with hypen. you can call this method in textview delegate method pass the text of textfeild after each key pressed. delegate - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string – Ishu Apr 28 '11 at 04:00