It seems UITextFields convert all non-breaking spaces (nbsp, U+00A0) to spaces (U+0020). This causes problems for the editing of locale specific numbers. For instance, in the French/France locale, the non-breaking space separates 1 and % to form ‘1 %’. However, since the UITextField converts the separator character to a space, the locale aware NSNumberFormatter is unable to parse the locale string and convert it to a number.
The following code snippet illustrates this problem.
NSLocale *fr_FR = [[[NSLocale alloc] initWithLocaleIdentifier:@'fr_FR'] autorelease]; NSNumberFormatter *percentageFormatter = [[NSNumberFormatter alloc] init]; [percentageFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4]; [percentageFormatter setGeneratesDecimalNumbers:TRUE]; [percentageFormatter setNumberStyle:NSNumberFormatterPercentStyle]; [percentageFormatter setLocale:fr_FR]; NSDecimalNumber *one = [NSDecimalNumber decimalNumberWithString:@'0.01']; // will be '1 %' where the separator character is a nbsp (this is correct) NSString *percentage = [percentageFormatter stringFromNumber:one]; UITextField *textField = [[UITextField alloc] init]; textField.text = percentage; // will be '1 %' where the separator character is a space (this is wrong) NSString *textFieldValue = textField2.text; // returns an indeterminate value: may throw an exception, may return nil, etc // this should return the number 0.01 NSNumber *percentageNumber = [percentageFormatter numberFromString:textFieldValue];
My question: How do I prevent the UITextField from converting non-breaking spaces to spaces?
I understand the work arounds I can employ to hack this so that it works if I’m in a locale that uses non-breaking spaces. However, if there is property/behavior of the UITextField that I’m not aware of to preserve nbsp, I’d like to know.
I’m pretty sure this feature doesn’t exist. How about opening a bug report for the iPhone?