gpt4 book ai didi

ios - 放置范围并避免在文本字段中使用特殊字符

转载 作者:行者123 更新时间:2023-12-01 20:19:48 28 4
gpt4 key购买 nike

我正在使用iOS。我有一个文本字段,其范围应为7位数字,并且还应避免使用特殊字符。我只想输入0-9999999之间的数字。请帮忙

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
{
if(range.length + range.location > textField.text.length)
{
return NO;
}

NSUInteger newLength = [textField.text length] + [string length] - range.length;
return newLength <= 7;

}

最佳答案

用这个

#define acceptableCharacterSet @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:acceptableCharacterSet] invertedSet];

NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];

if(![string isEqualToString:filtered])
return NO;
NSUInteger newLength = [textField.text length] + [string length] - range.length;
return (newLength > 7) ? NO : YES;
}

关于ios - 放置范围并避免在文本字段中使用特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35891620/

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