如何在 iPhone 中使用自定义数字键盘显示单个点?

发布于 2024-12-04 01:37:04 字数 230 浏览 1 评论 0原文

我正在创建自定义 NumberPad

if (([[[UIDevice currentDevice] systemVersion] doubleValue] >= 4.1)) {
    inputBoxTextField.keyboardType = UIKeyboardTypeDecimalPad;
}

,我的问题是,我只想显示点一次。

提前致谢。

i am creating custom NumberPad

if (([[[UIDevice currentDevice] systemVersion] doubleValue] >= 4.1)) {
    inputBoxTextField.keyboardType = UIKeyboardTypeDecimalPad;
}

my problem is, i want to display dot only one time.

thanks in advance.

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

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

发布评论

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

评论(3

有深☉意 2024-12-11 01:37:05

如果你想让用户只输入一个点,你可以使用委托方法

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

并简单地检查textField是否已经包含一个点。

If you want to allow the user to enter only one dot, you can use the delegate method

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

And simply check if the textField already contains a dot.

风月客 2024-12-11 01:37:05
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];

NSNumber* candidateNumber;

NSString* candidateString = [textField.text stringByReplacingCharactersInRange:range withString:string];

range = NSMakeRange(0, [candidateString length]);

[numberFormatter getObjectValue:&candidateNumber forString:candidateString range:&range error:nil];

if (([candidateString length] > 0) && (candidateNumber == nil || range.length < [candidateString length])) {

    return NO;
}
else {
    return YES; } }
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];

NSNumber* candidateNumber;

NSString* candidateString = [textField.text stringByReplacingCharactersInRange:range withString:string];

range = NSMakeRange(0, [candidateString length]);

[numberFormatter getObjectValue:&candidateNumber forString:candidateString range:&range error:nil];

if (([candidateString length] > 0) && (candidateNumber == nil || range.length < [candidateString length])) {

    return NO;
}
else {
    return YES; } }
哎呦我呸! 2024-12-11 01:37:05

我使用以下代码。非常适合我并且代码更少。

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
    NSString *decimalSymbol = [numberFormatter decimalSeparator];

    if(([textField.text rangeOfString:decimalSymbol].location != NSNotFound) &&
       ([string rangeOfString:decimalSymbol].location != NSNotFound)) {
        return NO;
    }
return YES; 
}

I use the following code. Works perfectly for me and with less code.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
    NSString *decimalSymbol = [numberFormatter decimalSeparator];

    if(([textField.text rangeOfString:decimalSymbol].location != NSNotFound) &&
       ([string rangeOfString:decimalSymbol].location != NSNotFound)) {
        return NO;
    }
return YES; 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文