UITextField将不间断的空格转换为空格? [英] UITextField converts non-breaking spaces to spaces?

查看:162
本文介绍了UITextField将不间断的空格转换为空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看来,UITextFields会转换所有不间断空格(nbsp,U + 00A0 )到空格(U + 0020)。这会导致编辑区域设置特定数字时出现问题。例如,在法语/法语区域中,不间断空格分隔1和%形成1%。但是,由于UITextField将分隔符字符转换为空格,因此区域设置感知NSNumberFormatter无法解析语言环境字符串并将其转换为数字。

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];

我的问题:如何防止UITextField将不间断的空格转换为空格?

My question: How do I prevent the UITextField from converting non-breaking spaces to spaces?

我了解我可以使用的工作,如果我在一个使用不间断空格的区域设置,它可以工作。但是,如果有UITextField的属性/行为,我不知道要保留,我想知道。

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.

推荐答案

我很确定这个功能不存在。如何为iPhone打开错误报告?

I'm pretty sure this feature doesn't exist. How about opening a bug report for the iPhone?

这篇关于UITextField将不间断的空格转换为空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆