Core Code
- (NSString *)PhoneFormatter:(NSString*)str { NSNumber * number = [NSNumber numberWithInteger:[str integerValue]]; NSNumberFormatter * formatter = [NSNumberFormatter new]; //Set Separator [formatter setGroupingSeparator:@" "]; //Set split format formatter.positiveFormat = @"###,###0"; NSString * string = [formatter stringFromNumber:number]; NSLog(@"%@",string); }
Enter mobile number 13911112222 and output the following:
139 1111 2222Similarly, enter a bank card number 6228480402564890018 and output the following:
622 8480 4025 6489 0018Scenario applicable: data already available, formatted presentation.
Scenario 2. Formatting of mobile phone number/bank card <when entering>If you continue with the above method, the number will jump during the input process, killing OCD.Interested students can try it.
Core Code
NSNumber * number = [NSNumber numberWithInteger:[string integerValue]]; NSNumberFormatter * formatter = [NSNumberFormatter new]; //Set Separator [formatter setGroupingSeparator:@" "]; //Set Use Group Split formatter.usesGroupingSeparator = YES; // Dimensions of numeric divisions <right-to-left> formatter.groupingSize = ([string length]-3)%4>0?([string length]-3)%4:4; //Dimensions divided by digits other than those determined by groupingSize formatter.secondaryGroupingSize = [string length]>7?4:3; //Get formatted characters tf.text = [formatter stringFromNumber:number];formatter.gif
Scenario applicable: Input characters require formatting at the same time.
Packaging IntegrationIn a project, by encapsulating the TextField+More classification, you can use it in just one sentence of code:
self.inputTextField.style = UIInputTextFieldStyle_Phone;
Here's a look at the implementation of TextField+More
<UITextField+More.h> // UITextField+More.h // NSNumberFormatterTest // // Created by Li Yanpeng on 2017/12/7. // Copyright_2017 csfuwwc. All rights reserved. // #import <UIKit/UIKit.h> typedef void(^EditBlock)(UITextField * tf); typedef enum : NSUInteger { UIInputTextFieldStyle_Phone = 1, //Cell-phone number UIInputTextFieldStyle_BankCard = 2, //Bank card } UIInputTextFieldStyle; @interface UITextField (More)<UITextFieldDelegate> //Extend the input box style property to allow formatting by assigning style directly @property (assign, nonatomic)UIInputTextFieldStyle style; /** * Input box event triggers callback * * @param event Event * @param block Callback */ - (void)handleControlEvent:(UIControlEvents)event withBlock:(EditBlock)block; <UITextField+More.m> // // UITextField+More.m // NSNumberFormatterTest // // Created by Li Yanpeng on 2017/12/7. // Copyright_2017 csfuwwc. All rights reserved. // #import "UITextField+More.h" #import <objc/runtime.h> @implementation UITextField (More) #pragma mark - UIControlEventBlock input character Block callback implementation static const char * TextFieldMoreKey = "TextFieldMoreKey"; -(void)handleControlEvent:(UIControlEvents)event withBlock:(EditBlock)block { objc_setAssociatedObject(self, TextFieldMoreKey, block, OBJC_ASSOCIATION_COPY_NONATOMIC); [self addTarget:self action:@selector(editEvent:) forControlEvents:event]; } -(void)editEvent:(UITextField *)textField { EditBlock block = objc_getAssociatedObject(self, TextFieldMoreKey); if (block) { block(textField); } } #pragma mark - UITextFieldInputSytle Input Character Formatting Core Part static const char * TextFieldStyleKey = "TextFieldStyleKey"; - (void)setStyle:(UIInputTextFieldStyle)style { objc_setAssociatedObject(self, TextFieldStyleKey, @(style), OBJC_ASSOCIATION_ASSIGN); self.delegate = self; //Detect Input-Format [self formatterInputString]; } - (UIInputTextFieldStyle)style { return [objc_getAssociatedObject(self, TextFieldStyleKey) integerValue]; } //Format input characters - (void)formatterInputString { [self handleControlEvent:UIControlEventEditingChanged withBlock:^(UITextField *tf) { //Empty NSString * string = [tf.text stringByReplacingOccurrencesOfString:@" " withString:@""]; if ([string length]==0) { return; } NSNumber * number = [NSNumber numberWithInteger:[string integerValue]]; NSNumberFormatter * formatter = [NSNumberFormatter new]; //Set Separator [formatter setGroupingSeparator:@" "]; //Set Use Group Split formatter.usesGroupingSeparator = YES; // Dimensions of numeric divisions <right-to-left> formatter.groupingSize = ([string length]-3)%4>0?([string length]-3)%4:4; //Dimensions divided by digits other than those determined by groupingSize formatter.secondaryGroupingSize = [string length]>7?4:3; //Get formatted characters tf.text = [formatter stringFromNumber:number]; }]; } #pragma mark - UITextFieldDelegate -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { if (self.style != 0 && ![string isEqualToString:@""]) { NSString * string = [textField.text stringByReplacingOccurrencesOfString:@" " withString:@""]; switch (self.style) { case UIInputTextFieldStyle_Phone: { //No more input if more than 11 bits if ([string length]>= 11) { return NO; } } break; case UIInputTextFieldStyle_BankCard: { //No more input if more than 16 bits if ([string length]>= 16) { return NO; } } break; default: break; } } return YES; } @end
Source address: https://github.com/csfuwwc/NSNumberFormatterTest
Concluding remarks More Formats
http://unicode.org/reports/tr35/tr35-6.html#Number_Format_Patterns
PS: NSNumberFormatter is much more powerful than imagination!
Thank you all.