我已经尝试了下面的代码,但它只允许输入键盘上的数字.我的应用程序要求键盘使用句点/句号(用于货币交易).我试过的代码是:
I have tried the code below but that only allows for numbers on the keypad to be inputted. My app requires the keypad to use a period/full stop (for money transactions). The code I tried is:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSCharacterSet *nonNumberSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
if ([string rangeOfCharacterFromSet:nonNumberSet].location != NSNotFound)
{
return NO;
}
return YES;
}
感谢您的帮助.
试试这个
制作宏
#define ACCEPTABLE_CHARACTERS @"0123456789."
并使用它
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (textField==textFieldAmount)
{
NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:ACCEPTABLE_CHARACTERS] invertedSet];
NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
return [string isEqualToString:filtered];
}
return YES;
}
这篇关于UITextField - 只允许数字和标点输入/键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!