Objective-C如何限制文本字段在2次输入后只允许特定字符
Posted
技术标签:
【中文标题】Objective-C如何限制文本字段在2次输入后只允许特定字符【英文标题】:Objective-C How to restrict text field to only allow a specific character after 2 inputs 【发布时间】:2017-09-29 09:30:11 【问题描述】:我有一个文本字段,我应该只允许 2 位数字,最多 2 位小数(例如 25.50)。我想限制 3 个字符的输入。在我的情况下,如果有 2 个输入并且它们都不是句点“。”它应该只允许第三个输入的时间。
我在此文本字段中使用数字键盘,这意味着唯一可用的输入是数字 0 到 9 和句点,您可以在上传的屏幕截图中看到。
以下是我用于其他限制的代码,以及我假设代码需要满足我需要的部分。其他一切正常。
- (BOOL) textView:(UITextView *)sender shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)string
//set to possibly restrict the character .
NSMutableCharacterSet *mcs1 = [[NSCharacterSet letterCharacterSet] mutableCopy];
[mcs1 addCharactersInString:@"."];
NSMutableCharacterSet *mcs2 = [[NSCharacterSet letterCharacterSet] mutableCopy];/
[mcs2 addCharactersInString:@"123456789"];
// allow only one period to be inserted
NSRange temprange = [sender.text rangeOfString:@"."];
if ((temprange.location != NSNotFound) && [string isEqualToString:@"."])
return NO;
// allow to use backspace and overlap code below
if ([string isEqualToString:@""])
return YES;
// a max of 5 characters
if(sender.text.length == 5)
return NO;
// if there are 2 inputs and none of them is a period
if(sender.text.length == 2 && [sender.text rangeOfCharacterFromSet:mcs1].location == NSNotFound)
// NEEDED CODE
// allow only 2 decimal places after a period
NSString *newString = [sender.text stringByReplacingCharactersInRange:range withString:string];
NSArray *sep = [newString componentsSeparatedByString:@"."];
if([sep count] >= 2)
NSString *sepStr=[NSString stringWithFormat:@"%@",[sep objectAtIndex:1]];
return !([sepStr length]>2);
return YES;
【问题讨论】:
使用正则表达式或浮点验证取决于您的选择 【参考方案1】:这是我的问题的解决方案。我修改了您的建议,现在它适用于我目前正在使用的所有其他限制。 ACCEPTABLE_CHARACTERS
是在一个不同的类中定义的,我有我的所有常量。非常感谢:)
#define ACCEPTABLE_CHARACTERS @"."
- (BOOL) textView:(UITextView *)sender shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)string
//set to possibly restrict the character .
NSMutableCharacterSet *mcs1 = [[NSCharacterSet letterCharacterSet] mutableCopy];
[mcs1 addCharactersInString:@"."];
// allow only one period to be inserted
NSRange temprange = [sender.text rangeOfString:@"."];
if ((temprange.location != NSNotFound) && [string isEqualToString:@"."])
return NO;
// allow to use backspace and overlap code below
if ([string isEqualToString:@""])
return YES;
// allow only a period as the third input
if (sender.text.length < 2 || string.length == 0)
return YES;
else if (sender.text.length == 2 && [sender.text rangeOfCharacterFromSet:mcs1].location == NSNotFound)
NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:ACCEPTABLE_CHARACTERS] invertedSet];
NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
return [string isEqualToString:filtered];
// a max of 5 characters (not needed but it should stay)
if(sender.text.length == 5)
return NO;
// allow only 2 decimal places after a period
NSString *newString = [sender.text stringByReplacingCharactersInRange:range withString:string];
NSArray *sep = [newString componentsSeparatedByString:@"."];
if([sep count] >= 2)
NSString *sepStr=[NSString stringWithFormat:@"%@",[sep objectAtIndex:1]];
return !([sepStr length]>2);
【讨论】:
【参考方案2】:给你
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
if (textField == txtEmailAdd) // Change with your TextField object
NSUInteger newLength = [textField.text length] + [string length] - range.length;
if (newLength == 3 && ![textField.text containsString:@"."])
textField.text = [NSString stringWithFormat:@"%@.", textField.text];
else if (newLength == 3 && [textField.text containsString:@"."])
textField.text = [textField.text stringByReplacingOccurrencesOfString:@"." withString:@""];
return newLength <=5;
return YES;
【讨论】:
【参考方案3】:结合我的编辑和某人的建议提出的解决方案。
#define ACCEPTABLE_CHARACTERS @"."
- (BOOL) textView:(UITextView *)sender shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)string
//set to possibly restrict the character .
NSMutableCharacterSet *mcs1 = [[NSCharacterSet letterCharacterSet] mutableCopy];
[mcs1 addCharactersInString:@"."];
// allow only one period to be inserted
NSRange temprange = [sender.text rangeOfString:@"."];
if ((temprange.location != NSNotFound) && [string isEqualToString:@"."])
return NO;
// allow to use backspace and overlap code below
if ([string isEqualToString:@""])
return YES;
// allow only a period as the third input
if (sender.text.length < 2 || string.length == 0)
return YES;
else if (sender.text.length == 2 && [sender.text rangeOfCharacterFromSet:mcs1].location == NSNotFound)
NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:ACCEPTABLE_CHARACTERS] invertedSet];
NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
return [string isEqualToString:filtered];
// a max of 5 characters (not needed but it should stay)
if(sender.text.length == 5)
return NO;
// allow only 2 decimal places after a period
NSString *newString = [sender.text stringByReplacingCharactersInRange:range withString:string];
NSArray *sep = [newString componentsSeparatedByString:@"."];
if([sep count] >= 2)
NSString *sepStr=[NSString stringWithFormat:@"%@",[sep objectAtIndex:1]];
return !([sepStr length]>2);
【讨论】:
以上是关于Objective-C如何限制文本字段在2次输入后只允许特定字符的主要内容,如果未能解决你的问题,请参考以下文章
TextfieldShouldBeginediting Objective-c 中的 UIAlertcontroller