1

I found an annoying problem. I have a textfield with the numeric decimal keyboard, but the simulator has the point, the device has the comma (both 6.1). How can I do, if you insert a comma, this is replaced by a point?

Thanks

EDIT:

this process, must take place in this method, in real time

- (BOOL) textField: (UITextField *) theTextField shouldChangeCharactersInRange: (NSRange) range ReplacementString: (NSString *) string

otherwise the comma is written, and at the next character, it is replaced

Vins
  • 1,814
  • 4
  • 24
  • 40

3 Answers3

4

You can use

- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target 
                                        withString:(NSString *)replacement

eg:

NSString *str = myTextfield.text;

str = [str stringByReplacingOccurrencesOfString:@"."
                                     withString:@","];

for custom keyboard see this link

Edit You can check the string enter in this function and if its @"." then you can change it with @","

    // using something this


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    {
        NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
        [self updateTextLabelsWithText: newString];
        NSLog(@"Changed Str: %@",newString);

        return YES;
    }

    -(void)updateTextLabelsWithText:(NSString *)str
    {
     str = [str stringByReplacingOccurrencesOfString:@"."
                                             withString:@","];
         [myTextFiled setText:str];
    }
Community
  • 1
  • 1
HDdeveloper
  • 4,396
  • 6
  • 40
  • 65
  • sorry, look the change to the question – Vins Feb 16 '13 at 18:08
  • @Vins See updated answer.[Also see](http://stackoverflow.com/questions/10363856/iphone-how-to-get-uitextfields-text-while-typing). – HDdeveloper Feb 16 '13 at 18:24
  • 5
    This code won't work. You must return `NO` from `shouldChangeCharactersInRange` if you manually update the text field's text. Only return `YES` if you want the framework to continue and update the text field with the replacement string. – rmaddy Feb 16 '13 at 18:24
2

You are looking at this problem incorrectly. When you setup a text field with the Decimal keypad, the keypad will show either a period or a comma depending on the user's locale. This is what you want. Some users will want to enter decimal numbers like 3.14 while other users will want to enter them as 3,14.

Run the Settings app and go to General, then International. Look at the Region Format setting. This determines the user's locale and whether a period or comma is used. Your device and simulator must be setup with different region format settings.

Here's the final part - once a user enters a decimal value, you must use an NSNumberFormatter to properly convert the string entered into the text field to an NSNumber. The NSNumberFormatter will properly deal with a comma or period in the number based on the user's locale. Also, when you need to show the number to the user, use an NSNumberFormatter to convert the number to a string. This will ensure the number is displayed to the user in the format expected by the user.

Do not replace a comma with a period during user entry of the value. It will confuse the user.

Update

One thing you could do in the shouldChangeCharactersInRange delegate method is to just be sure all the characters being entered are valid number characters. I would not do things like checking for two decimal separator characters. Just let the user type in number related characters.

You do the final validation when the user tries to leave the text field. Example:

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    [formatter setNumberStyle:NSNumberFormatterDecimalStyle];

    NSNumber *val = [formatter numberFromString:textField.text];

    return val != nil; // don't let the user leave if text is not a valid number
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • ok, fine, but can you do me a quick example? related to my example? Thanks – Vins Feb 16 '13 at 18:42
  • Sorry, an example of what? I'm saying that you don't need to do anything with the `shouldChangeCharactersInRange` delegate. Just let the user type in a number with the comma or period as it appears on the decimal keypad. – rmaddy Feb 16 '13 at 18:45
  • because I make sure to put a single point. So I have other code within `if ([textFieldString rangeOfString:@"."].location == NSNotFound) { //does not contain the dot return YES; } else { //contain the dot if ([string isEqualToString:@"."]) { return NO; } }` – Vins Feb 16 '13 at 18:51
  • I keep saying that you do NOT need that code. Why do you want that code? – rmaddy Feb 16 '13 at 18:53
  • surely you are right, just that I'm not understanding where to put NSNumberFormatter. For example, since I do not have a label, but the number is represented in the textfield when the user writes, if the user enters two commas, what happens? where should I put this NSNumberFormatter? Thank you and I'm sorry if things seem trivial, but I've never used. – Vins Feb 16 '13 at 19:01
  • thanks for the example, but this might confuse even more the user .. http://postimage.org/image/6worjhjtx/ – Vins Feb 16 '13 at 19:32
  • No real user is going to type a number like that. And if they do, you need to indicate that it isn't valid when they try to leave the text field. Ultimately it is up to you how much validation you wish to do. The main point of my answer was that you should not convert commas to periods. The rest is up to you. – rmaddy Feb 16 '13 at 19:36
  • Things have changed with iOS 8 and the ability to have custom keyboards. Some keyboards will not localise the decimal separator. I have the issue with the Swift keyboard. In which case the number formatter is not the right solution. – aspyct May 04 '15 at 14:56
1

Swift 3.1

1) Replace the comma(,) in with dot(.)

2) Allow only one dot(.)

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {

    let dotsCount = textField.text!.componentsSeparatedByString(".").count - 1
    if dotsCount > 0 && (string == "." || string == ",") {
        return false
    }

    if string == "," {
        textField.text! += "."
        return false
    }

    return true
}
Himanshu padia
  • 7,428
  • 1
  • 47
  • 45
  • but what if the char was added in the middle of the string? You can not just add it to the end always. – Slavcho May 29 '19 at 13:23