看来 UITextFields 全部转换不间断空格 http://en.wikipedia.org/wiki/Non-breaking_space(nbsp,U+00A0) 至spaces http://en.wikipedia.org/wiki/Space_character(U+0020)。这会导致编辑区域设置特定数字时出现问题。例如,在法语/法国语言环境中,不间断空格将 1 和 % 分隔开来形成“1 %”。但是,由于 UITextField 将分隔符转换为空格,因此区域设置感知 NSNumberFormatter 无法解析区域设置字符串并将其转换为数字。
下面的代码片段说明了这个问题。
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];
我的问题:如何防止 UITextField 将不间断空格转换为空格?
我了解可以用来破解此问题的解决方法,以便在我处于使用不间断空格的区域设置时它可以工作。但是,如果 UITextField 的某些属性/行为我不知道要保留 nbsp,我想知道。
我很确定这个功能不存在。打开 iPhone 的错误报告怎么样?
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)