Ios 如何在用户编辑UITEXTFIELD时自动插入字符串
我希望我的uitextfield在键入结束时类似于XXX.XXX.XXX/XX 为了限制长度,我使用以下方法:Ios 如何在用户编辑UITEXTFIELD时自动插入字符串,ios,objective-c,uitextfield,uitextfielddelegate,Ios,Objective C,Uitextfield,Uitextfielddelegate,我希望我的uitextfield在键入结束时类似于XXX.XXX.XXX/XX 为了限制长度,我使用以下方法: - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { if (textField == _cpfField) { NSUInteger newLength =
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (textField == _cpfField) {
NSUInteger newLength = [textField.text length] + [string length] - range.length;
NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS_ONLY] invertedSet];
NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
return (([string isEqualToString:filtered])&&(newLength <= CHARACTER_LIMIT));
} else{
return YES;
}
}
-(BOOL)textField:(UITextField*)textField应更改字符范围:(NSRange)范围替换字符串:(NSString*)字符串{
if(textField==\u cpfField){
NSUInteger newLength=[textField.text length]+[string length]-range.length;
NSCharacterSet*cs=[[NSCharacterSet characterSetWithCharactersInString:仅限数字]逆变器集];
NSString*筛选=[[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@”“];
返回([string isEqualToString:filtered])&&(newLength-(BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string{
NSString*text=textField.text;
text=[text stringByReplacingCharactersInRange:range with string:string];
text=[text stringByReplacingOccurrencesOfString:@.“with string:@]”;
//你在这里检查长度吗
NSMutableString*mutableText=[text mutableCopy];
//每四个字符将是一个字符。
对于(i=3;i
以下代码应执行以下操作:
限制可键入/粘贴到文本字段中的字符数
在适当的位置自动添加句点和斜杠
防止用户复制/粘贴已具有必需句点/斜杠的字符串时出现问题
也就是说,几乎可以肯定有更有效的方法来实现这一点;但是如果您不关心代码长度,那么它就可以很好地实现这一点
- (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *text = textField.text;
// If we're trying to add more than the max amount of characters, don't allow it
if ([text length] == 14 && range.location > 13) {
return NO;
}
// First lets add the whole string we're going for
text = [text stringByReplacingCharactersInRange:range withString:string];
// Now remove spaces, periods, and slashes (since we'll add these automatically in a minute)
text = [text stringByReplacingOccurrencesOfString:@" " withString:@""];
text = [text stringByReplacingOccurrencesOfString:@"." withString:@""];
text = [text stringByReplacingOccurrencesOfString:@"/" withString:@""];
// We need to use an NSMutableString to do insertString calls in a moment
NSMutableString *mutableText = [text mutableCopy];
// Every 4th char will be a '.', but we don't want to check more than the first 8 characters
for (NSUInteger i = 3; i < mutableText.length && i < 8; i += 4) {
[mutableText insertString:@"." atIndex:i];
}
// If the text is more than 11 characters, we also want to insert a '/' at the 11th character index
if (mutableText.length > 11) {
[mutableText insertString:@"/" atIndex:11];
}
// lets set text to our new string
text = mutableText;
// Now, lets check if we need to cut off extra characters (like if the person pasted a too-long string)
if (text.length > 14) {
text = [text stringByReplacingCharactersInRange:NSMakeRange(14, mutableText.length-14) withString:@""];
}
// Finally, set the textfield to our newly modified string!
textField.text = text;
return NO;
}
-(BOOL)textField:(UITextField*)textField应更改字符范围:(NSRange)范围替换字符串:(NSString*)字符串{
NSString*text=textField.text;
//如果我们试图添加超过最大字符数的字符,则不允许添加
如果([text length]==14&&range.location>13){
返回否;
}
//首先,让我们添加整个字符串
text=[text stringByReplacingCharactersInRange:range with string:string];
//现在删除空格、句点和斜杠(因为我们将在一分钟内自动添加它们)
text=[text stringByReplacingOccurrencesOfString:@“with string:@]”;
text=[text stringByReplacingOccurrencesOfString:@.“with string:@]”;
text=[text stringByReplacingOccurrencesOfString:@”/“with string:@”“”;
//我们需要使用一个NSMutableString来立即执行insertString调用
NSMutableString*mutableText=[text mutableCopy];
//每4个字符将是一个“.”,但我们不希望检查超过前8个字符
对于(i=3;i11){
[mutableText-insertString:@/“atIndex:11];
}
//让我们将文本设置为新字符串
文本=可变文本;
//现在,让我们检查是否需要删除额外的字符(例如,如果用户粘贴的字符串太长)
如果(text.length>14){
text=[text stringByReplacingCharactersInRange:NSMakeRange(14,可变文本.length-14),带字符串:@”“;
}
//最后,将textfield设置为我们新修改的字符串!
textField.text=文本;
返回否;
}
英国国家保险号码文本字段
-(BOOL)textField:(UITextField*)textField应更改字符范围:(NSRange)范围替换字符串:(NSString*)字符串
{
如果(textField==\u txtNationalInsuranceNumber){
NSString*text=textField.text;
//如果我们试图添加超过最大字符数的字符,则不允许添加
如果([text length]==13&&range.location>12){
返回否;
}
//首先,让我们添加整个字符串
text=[text stringByReplacingCharactersInRange:range with string:string];
//现在删除空格、句点和斜杠(因为我们将在一分钟内自动添加它们)
text=[text stringByReplacingOccurrencesOfString:@“with string:@]”;
//我们需要使用一个NSMutableString来立即执行insertString调用
NSMutableString*mutableText=[text mutableCopy];
//每4个字符将是一个“.”,但我们不希望检查超过前8个字符
对于(i=2;i11){
[mutableText insertString:@“atIndex:11];
}
//让我们将文本设置为新字符串
文本=可变文本;
//现在,让我们检查是否需要删除额外的字符(例如,如果用户粘贴的字符串太长)
如果(text.length>14){
text=[text stringByReplacingCharactersInRange:NSMakeRange(14,可变文本.length-14),带字符串:@”“;
}
//最后,将textfield设置为我们新修改的字符串!
textField.text=文本;
返回否;
}
其他的
{
返回YES;
}
}
你的解决方案不起作用,它会不断添加点,你也不能使用退格。你知道你可以自己思考吗?这是给你一个方向,而不是给你一个完整的解决方案。在第4行,我有一个空格,而不是一个点(现在已编辑),现在它按预期工作:每4个字符是一个点。在我提供的帮助下,您应该能够真正理解这一点,以获得完整的解决方案。Danilo的解决方案,虽然已损坏(至少最初是这样),但不符合所有要求,但并不是
- (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *text = textField.text;
// If we're trying to add more than the max amount of characters, don't allow it
if ([text length] == 14 && range.location > 13) {
return NO;
}
// First lets add the whole string we're going for
text = [text stringByReplacingCharactersInRange:range withString:string];
// Now remove spaces, periods, and slashes (since we'll add these automatically in a minute)
text = [text stringByReplacingOccurrencesOfString:@" " withString:@""];
text = [text stringByReplacingOccurrencesOfString:@"." withString:@""];
text = [text stringByReplacingOccurrencesOfString:@"/" withString:@""];
// We need to use an NSMutableString to do insertString calls in a moment
NSMutableString *mutableText = [text mutableCopy];
// Every 4th char will be a '.', but we don't want to check more than the first 8 characters
for (NSUInteger i = 3; i < mutableText.length && i < 8; i += 4) {
[mutableText insertString:@"." atIndex:i];
}
// If the text is more than 11 characters, we also want to insert a '/' at the 11th character index
if (mutableText.length > 11) {
[mutableText insertString:@"/" atIndex:11];
}
// lets set text to our new string
text = mutableText;
// Now, lets check if we need to cut off extra characters (like if the person pasted a too-long string)
if (text.length > 14) {
text = [text stringByReplacingCharactersInRange:NSMakeRange(14, mutableText.length-14) withString:@""];
}
// Finally, set the textfield to our newly modified string!
textField.text = text;
return NO;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField == _txtNationalInsuranceNumber) {
NSString *text = textField.text;
// If we're trying to add more than the max amount of characters, don't allow it
if ([text length] == 13 && range.location > 12) {
return NO;
}
// First lets add the whole string we're going for
text = [text stringByReplacingCharactersInRange:range withString:string];
// Now remove spaces, periods, and slashes (since we'll add these automatically in a minute)
text = [text stringByReplacingOccurrencesOfString:@" " withString:@""];
// We need to use an NSMutableString to do insertString calls in a moment
NSMutableString *mutableText = [text mutableCopy];
// Every 4th char will be a '.', but we don't want to check more than the first 8 characters
for (NSUInteger i = 2; i < mutableText.length && i < 10; i += 3) {
[mutableText insertString:@" " atIndex:i];
}
// If the text is more than 11 characters, we also want to insert a '/' at the 11th character index
if (mutableText.length > 11) {
[mutableText insertString:@" " atIndex:11];
}
// lets set text to our new string
text = mutableText;
// Now, lets check if we need to cut off extra characters (like if the person pasted a too-long string)
if (text.length > 14) {
text = [text stringByReplacingCharactersInRange:NSMakeRange(14, mutableText.length-14) withString:@""];
}
// Finally, set the textfield to our newly modified string!
textField.text = text;
return NO;
}
else
{
return YES;
}
}