默认支持小数点之前每三位用逗号分隔,可输入小数点,小数点后最多输入两位
#import <UIKit/UIKit.h>
@interface UITextField (MicrometerLevelFormat)
/*!
* @author ma dongkai, 16-03-15 18:03:03
*
* @brief 打开数字千分位的开关,需要设置数字键盘、需要在
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string中打开
*
* @since 1.0
*/
- (void)openMicrometerLevelFormat;
//直接赋值的时候 手动调用此方法防止光标错位
- (void)reformatAsMicrometerLevel:(UITextField *)textField;
@end
import "UITextField+MicrometerLevelFormat.h"
#import <objc/runtime.h>
@interface UITextField ()
@property (retain, nonatomic) UITextRange * textrange;
@property (retain, nonatomic) NSString * preString;
@end
@implementation UITextField (MicrometerLevelFormat)
static char kTextRange;
static char kPreString;
#pragma mark - method swizzle
- (void)setTextrange:(UITextRange *)textrange{
objc_setAssociatedObject(self, &kTextRange, textrange, OBJC_ASSOCIATION_COPY);
}
- (UITextRange *)textrange{
return objc_getAssociatedObject(self, &kTextRange);
}
- (void)setPreString:(NSString *)preString{
objc_setAssociatedObject(self, &kPreString, preString, OBJC_ASSOCIATION_COPY);
}
- (NSString *)preString{
return objc_getAssociatedObject(self, &kPreString);
}
#pragma mark - 千分位
//开启千分位模式
- (void)openMicrometerLevelFormat {
[self addTarget:self action:@selector(reformatAsMicrometerLevel:) forControlEvents:UIControlEventEditingChanged];
NSLog(@"++++ = %lu",(unsigned long)[[self allTargets] count]);
[self configSelectedRange];
}
-(NSString*)formatString:(NSString*)replaceString
{
if([replaceString rangeOfString:@"."].length == 0){
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
numberFormatter.numberStyle = NSNumberFormatterDecimalStyle;
NSString * formattedNumberString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:replaceString.doubleValue]];
if ([replaceString isEqualToString:@""]) {
return @"";
}else{
return formattedNumberString;
}
}else{
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
numberFormatter.numberStyle = NSNumberFormatterDecimalStyle;
NSString * formattedNumberString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:replaceString.doubleValue]];
NSMutableString *resultStre = [[NSMutableString alloc]init];
[resultStre appendString:formattedNumberString];
if ([[replaceString substringFromIndex:replaceString.length-1] isEqualToString:@"."]) {
[resultStre appendString:@"."];
} else {
NSString *pointStr = [replaceString componentsSeparatedByString:@"."][1];//点后面的数字
if (pointStr.length >2) {
return self.preString;
}
NSLog(@"%@",self.preString);
return [NSString stringWithFormat:@"%@.%@",[resultStre componentsSeparatedByString:@"."][0],pointStr];
}
// if ((replaceString.length-1) - [replaceString rangeOfString:@"."].location == 0) {
//
// }else if ((replaceString.length-1) - [replaceString rangeOfString:@"."].location > 2)
// {
//
// return self.preString;
// }
return resultStre;
}
}
#pragma mark - private method
//千分位校验
- (void)reformatAsMicrometerLevel:(UITextField *)textField {
NSString *replaceString = [textField.text stringByReplacingOccurrencesOfString:@"," withString:@""];
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
//[numberFormatter setPositiveFormat:@"###,###.00"];
numberFormatter.numberStyle = NSNumberFormatterDecimalStyle;
NSString * formattedNumberString = [self formatString:replaceString];
textField.text = formattedNumberString;
textField.selectedTextRange = self.textrange; //保证光标在逗号后时,删除功能正常
NSString * beforeString = self.preString;
NSString * afterString = formattedNumberString;
NSInteger off ;
UITextPosition *newPos;
if([formattedNumberString rangeOfString:@"."].length == 0){
if(beforeString.length - afterString.length == 1){//仅减一个数字
off = -1;
newPos = [textField positionFromPosition:self.textrange.start offset:off];
textField.selectedTextRange = [textField textRangeFromPosition:newPos toPosition:newPos];
}else if (beforeString.length - afterString.length == 2){//减一个数字 一个逗号
off = -2;
newPos = [textField positionFromPosition:self.textrange.start offset:off];
textField.selectedTextRange = [textField textRangeFromPosition:newPos toPosition:newPos];
}else if (beforeString.length - afterString.length == -1){//仅加一个数字
off = 1;
newPos = [textField positionFromPosition:self.textrange.end offset:off];
textField.selectedTextRange = [textField textRangeFromPosition:newPos toPosition:newPos];
}else if (beforeString.length-afterString.length == -2){//加一个数字 一个逗号
off = 2;
newPos = [textField positionFromPosition:self.textrange.start offset:off];
textField.selectedTextRange = [textField textRangeFromPosition:newPos toPosition:newPos];
}
}else{
//有小数点了
NSString *lastChara = [afterString substringFromIndex:afterString.length-1];
if (![lastChara isEqualToString:@"."]) {
if(beforeString.length - afterString.length == 1){//仅减一个数字
off = -1;
newPos = [textField positionFromPosition:self.textrange.start offset:off];
textField.selectedTextRange = [textField textRangeFromPosition:newPos toPosition:newPos];
} if (beforeString.length - afterString.length == -1){//仅加一个数字
off = 1;
newPos = [textField positionFromPosition:self.textrange.end offset:off];
textField.selectedTextRange = [textField textRangeFromPosition:newPos toPosition:newPos];
}
} else {
if(beforeString.length - afterString.length == 1){//仅减一个数字
off = -1;
newPos = [textField positionFromPosition:self.textrange.start offset:off];
textField.selectedTextRange = [textField textRangeFromPosition:newPos toPosition:newPos];
} if (beforeString.length - afterString.length == -1){//仅加一个数字
off = 1;
newPos = [textField positionFromPosition:self.textrange.end offset:off];
textField.selectedTextRange = [textField textRangeFromPosition:newPos toPosition:newPos];
}
// NSInteger offset = textField.text.length;
// [textField setSelectedTextRange:[textField textRangeFromPosition:[textField positionFromPosition:textField.beginningOfDocument offset:offset] toPosition:[textField positionFromPosition:textField.beginningOfDocument offset:offset]]];
}
}
self.preString = formattedNumberString;//把修改之后的赋值给prestring
}
- (void)configSelectedRange{
UITextRange *selectedRange = [self selectedTextRange];
[self textRange:selectedRange];
}
//传回光标的位置
- (void)textRange:(UITextRange *)range {
self.textrange = range;
}
@end
使用方法:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
[textField openMicrometerLevelFormat];
return YES;
}