美文网首页我爱编程IOS
微信发红包UITextField金额输入格式化

微信发红包UITextField金额输入格式化

作者: 开心就好2008 | 来源:发表于2018-02-22 16:08 被阅读0次

    - (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;

    }

    相关文章

      网友评论

        本文标题:微信发红包UITextField金额输入格式化

        本文链接:https://www.haomeiwen.com/subject/cmrztftx.html