如何在 iOS 中构建计算器风格的数字输入字段?

发布于 2024-09-30 09:16:51 字数 518 浏览 0 评论 0原文

我已经知道如何启用数字键盘并验证字段输入以确保它是数字。我想知道的是如何设置一个输入视图,让用户从右到左输入数值,就像计算器或 ATM 机一样。

换句话说,我希望用户能够输入:

2 0 0 4 9

并将其显示为 $200.49

然后,一旦输入,我还需要以 NSNumber 或 NSDecimalNumber 的形式在输入视图中或从该输入视图中获取值,并具有正确的小数位置。

例如,我有一个相当于 17.50 的 NSDecimalNumber。我需要能够将此数字传递到键盘视图中进行编辑,然后将编辑后的值作为数字对象返回。

假设用户编辑了该值;他们看到的值是

1 7 5 0

然后他们按退格键。显示的数字将是

1 7 5

我需要将该数字恢复为 1.75,而不是 175 或 17.5。

有什么建议吗?

I already know how to enable the numeric keyboards and validate the field input to ensure it's numeric. What I want to know is how to set up an input view that'll let my user enter a number value from right to left, like a calculator or ATM machine.

In other words, I want the user to be able to type:

2 0 0 4 9

and have that be displayed as $200.49.

Then, once that's been input, I also need to get values in or out of this input view as an NSNumber or NSDecimalNumber, with correct decimal placement.

For example, I have an NSDecimalNumber equivalent to 17.50. I need to be able to pass this number into the keypad view for editing, then get back the edited value as a number object.

Say the user edits that value; the value they see would be

1 7 5 0

Then they hit the backspace key. The number displayed would then be

1 7 5

I need to get that number back as 1.75, not 175 or 17.5.

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

八巷 2024-10-07 09:16:51

要格式化字符串,您可以使用 NSNumberFormatter。像这样的东西:

float input = 20049.0f / 100.0;

NSNumberFormatter* formatter = [[NSNumberFormatter alloc] init];    
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];

NSNumber *nsInput = [NSNumber numberWithFloat:input];
NSString* formattedOutput = [formatter stringFromNumber:nsInput];
NSLog(@"Output as currency: %@", formattedOutput);

[formatter release];

To format the string you can use NSNumberFormatter. Something like this:

float input = 20049.0f / 100.0;

NSNumberFormatter* formatter = [[NSNumberFormatter alloc] init];    
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];

NSNumber *nsInput = [NSNumber numberWithFloat:input];
NSString* formattedOutput = [formatter stringFromNumber:nsInput];
NSLog(@"Output as currency: %@", formattedOutput);

[formatter release];
旧伤还要旧人安 2024-10-07 09:16:51

我已经实现了内置此功能的SATextField。它的工作方式与 UITextField 类似,并且具有相同的外观。只需将 fixedDecimalPoint 属性设置为 YES

编辑: 我创建了一个包含此功能的新项目:STAControls (具体来说,使用 STAATMTextField -- 它甚至支持 . 输入!) 。不同之处在于,该项目已作为 UITextField 的合法子类实现,而 SATextField 只是容纳 UITextField 实例的容器类。

I have implemented SATextField that has this functionality built-in. It works just like a UITextField and has the same appearance. Merely set the fixedDecimalPoint property to YES.

EDIT: I have created a new project that contains this functionality: STAControls (specifically, use STAATMTextField -- it even supports . entry!). The difference is that this project has been implemented as a legitimate subclass of UITextField whereas SATextField was merely a container class housing a UITextField instance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文