iPhone 文本字段只有一种输入类型

发布于 2025-01-04 02:35:05 字数 116 浏览 3 评论 0原文

我的项目中有一些文本字段。

我希望文本字段的输入类型只是一种类型。例如,信用卡号码文本字段的输入只能是小数,并且用户输入其他类型的字符,它将在文本字段中删除或不接受。

抱歉我的英语不好。

I have some text fields on my project.

I want that input types of text fields are only one type. For example credit card number text field's input must be only decimals and user enter another type character, it will remove or not accept in the text field.

Sorry my bad english.

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

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

发布评论

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

评论(3

ゃ懵逼小萝莉 2025-01-11 02:35:05

您必须在 textField 的委托中使用 textField:shouldChangeCharactersInRange:replacementString: ,对任何更改返回 YES 或 NO 。您可以分析 replacementString 中是否有禁止的字符,如果发现则拒绝它。

You have to use textField:shouldChangeCharactersInRange:replacementString: in textField's delegate, returning YES or NO for any change. You can analyze replacementString for forbidden characters and reject it if they are found.

我们的影子 2025-01-11 02:35:05

你可以像下面这样声明并使用

#define LEGAL   @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz "


// allow only legal letters
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:LEGAL] invertedSet];
    NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
    return [string isEqualToString:filtered];
}

,我们也可以这样使用

entryField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;

you can declare like the below and use

#define LEGAL   @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz "


// allow only legal letters
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:LEGAL] invertedSet];
    NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
    return [string isEqualToString:filtered];
}

and we can use like this also

entryField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
平安喜乐 2025-01-11 02:35:05

如果您通过代码创建了UITextField,那么您需要设置如下:

textField.keyboardType = UIKeyboardTypeNumberPad;

If you have created the UITextField by code, then you need to set as follows:

textField.keyboardType = UIKeyboardTypeNumberPad;

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