我试图限制在UITextfied中输入的字符数,我希望用户输入范围40中的数字。[0到9]到250. [0到9]并且用户只能输入一个数字小数点,他也不能输入多个小数点。我希望我已经说清楚了,到目前为止,我已经尝试了一些代码,这些代码位于
之下- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSCharacterSet *nonNumberSet = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789."] invertedSet];
// allow backspace
if (range.length > 0 && [string length] == 0) {
return YES;
}
// do not allow . at the beggining
if (range.location == 0 && [string isEqualToString:@"."]) {
return NO;
}
// set the text field value manually
NSString *newValue = [[textField text] stringByReplacingCharactersInRange:range withString:string];
newValue = [[newValue componentsSeparatedByCharactersInSet:nonNumberSet] componentsJoinedByString:@""];
textField.text = newValue;
// return NO because we're manually setting the value
return NO;
}
所以朋友们请进一步帮助我。
此致 兰吉特