gpt4 book ai didi

iphone - UIKeyboardTypeDecimalPad 检测它是否有逗号 ","或点 "."

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:09:27 25 4
gpt4 key购买 nike

如何检查 UIKeyboardTypeDecimalPad 是否有点/逗号?我需要这个来检查输入了多少个点/逗号,这样我就可以将它们限制为只有 1 个。当我知道 UIKeyboardTypeDecimalPad 有逗号时,我知道该怎么做。

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

NSCharacterSet *cs;
NSString *filtered;

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

if ([textField.text rangeOfString:@","].location == NSNotFound) {

cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERSPERIOD] invertedSet];
filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
return [string isEqualToString:filtered];
}


else {

cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS] invertedSet];
filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];

NSUInteger count = 0, length = [newString length];
NSRange range = NSMakeRange(0, length);
while(range.location != NSNotFound)
{
range = [newString rangeOfString: @"," options:0 range:range];
if(range.location != NSNotFound)
{
range = NSMakeRange(range.location + range.length, length - (range.location + range.length));
count++;
}
}

if (count < 2) {

NSArray *sep = [newString componentsSeparatedByString:@","];
if([sep count]>=2)
{
NSString *sepStr=[NSString stringWithFormat:@"%@",[sep objectAtIndex:1]];
return !([sepStr length]>2);
}

return YES;

}

return NO;

}
}

有没有简单的方法来检查键盘是否有“,”或“。”?

最佳答案

我怀疑那个键上的字符是由当前语言环境控制的。我说我怀疑是因为我没有测试过这个。如果这是正确的,则以下应该允许您确定是否存在逗号或句点/小数点:

NSString *symbol = [[NSLocale currentLocale] objectForKey:NSLocaleDecimalSeparator];

关于iphone - UIKeyboardTypeDecimalPad 检测它是否有逗号 ","或点 ".",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11654546/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com