- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string {
NSString*text =self.moneyTextField.text;
NSString*decimalSeperator =@".";
NSCharacterSet*charSet =nil;
NSString*numberChars =@"0123456789";
if([stringisEqualToString:decimalSeperator] && [textlength] ==0) {
returnNO;
}
NSRangedecimalRange = [textrangeOfString:decimalSeperator];
BOOLisDecimalNumber = (decimalRange.location!=NSNotFound);
if(isDecimalNumber) {
charSet = [NSCharacterSet characterSetWithCharactersInString:numberChars];
if([stringrangeOfString:decimalSeperator].location!=NSNotFound) {
returnNO;
}
}
else{
numberChars = [numberCharsstringByAppendingString:decimalSeperator];
charSet = [NSCharacterSet characterSetWithCharactersInString:numberChars];
}
NSCharacterSet*invertedCharSet = [charSetinvertedSet];
NSString*trimmedString = [stringstringByTrimmingCharactersInSet:invertedCharSet];
text = [textstringByReplacingCharactersInRange:range withString:trimmedString];
if(isDecimalNumber) {
NSArray*arr = [textcomponentsSeparatedByString:decimalSeperator];
if([arrcount] ==2) {
if([arr[1]length] >2) {
returnNO;
}
}
}
if(text.length>=7) {
returnNO;
}
textField.text= text;
return NO;
}
网友评论