今天有朋友的朋友新手,问输入手机号格式化的问题 随手写了点 不完善 ,但还能用,手机号目前仅支持 中国 11位的,金钱的输入 目前不支持小数点,问我也没说能输入小数点,我就没写 直接粘贴代码吧
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField) {
NSString* text = textField.text;
//删除
if([string isEqualToString:@""]){
//删除一位
if(range.length == 1){
//最后一位,遇到空格则多删除一次
if (range.location == text.length-1 ) {
if ([text characterAtIndex:text.length-1] == ' ') {
[textField deleteBackward];
}
return YES;
}
//从中间删除
else{
NSInteger offset = range.location;
if (range.location < text.length && [text characterAtIndex:range.location] == ' ' && [textField.selectedTextRange isEmpty]) {
[textField deleteBackward];
offset --;
}
[textField deleteBackward];
textField.text = [self parseString:textField.text];
UITextPosition *newPos = [textField positionFromPosition:textField.beginningOfDocument offset:offset];
textField.selectedTextRange = [textField textRangeFromPosition:newPos toPosition:newPos];
return NO;
}
}
else if (range.length > 1) {
BOOL isLast = NO;
//如果是从最后一位开始
if(range.location + range.length == textField.text.length ){
isLast = YES;
}
[textField deleteBackward];
textField.text = [self parseString:textField.text];
NSInteger offset = range.location;
if (range.location == 3 || range.location == 8) {
offset ++;
}
if (isLast) {
//光标直接在最后一位了
}else{
UITextPosition *newPos = [textField positionFromPosition:textField.beginningOfDocument offset:offset];
textField.selectedTextRange = [textField textRangeFromPosition:newPos toPosition:newPos];
}
return NO;
}
else{
return YES;
}
}else if(string.length >0){
//限制输入字符个数
if (([self noneSpaseString:textField.text].length + string.length - range.length > 11) ) {
return NO;
}
[textField insertText:string];
textField.text = [self parseString:textField.text];
NSInteger offset = range.location + string.length;
if (range.location == 3 || range.location == 8) {
offset ++;
}
UITextPosition *newPos = [textField positionFromPosition:textField.beginningOfDocument offset:offset];
textField.selectedTextRange = [textField textRangeFromPosition:newPos toPosition:newPos];
return NO;
}else{
return YES;
}
}
return YES;
}
-(NSString*)noneSpaseString:(NSString*)string
{
return [string stringByReplacingOccurrencesOfString:@" " withString:@""];
}
- (NSString*)parseString:(NSString*)string
{
if (!string)return nil;
NSMutableString* mStr = [NSMutableString stringWithString:[string stringByReplacingOccurrencesOfString:@" " withString:@""]];
if (mStr.length >3) {
[mStr insertString:@" " atIndex:3];
}if (mStr.length > 8) {
[mStr insertString:@" " atIndex:8];
}
return mStr;
}
//
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *text = [textField text];
NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789\b"];
string = [string stringByReplacingOccurrencesOfString:@"," withString:@""];
if ([string rangeOfCharacterFromSet:[characterSet invertedSet]].location != NSNotFound) {
return NO;
}
text = [text stringByReplacingCharactersInRange:range withString:string];
text = [text stringByReplacingOccurrencesOfString:@"," withString:@""];
NSString *newString = @"";
while (text.length > 0) {
NSInteger temp_length = text.length - 3;
NSString *subString = [text substringFromIndex:temp_length > 0?temp_length:0];
newString = [subString stringByAppendingString:newString];
text = [text stringByReplacingCharactersInRange:NSMakeRange(temp_length > 0?temp_length:0, subString.length) withString:@""];;
if (subString.length == 3 && text.length >0) {
newString = [@"," stringByAppendingString:newString];
}
}
newString = [newString stringByTrimmingCharactersInSet:[characterSet invertedSet]];
if (newString.length >= 20) {
return NO;
}
[textField setText:newString];
return NO;
}
/** 获取格式化后的金额*/
-(NSString*)toObtainTheActualAmount:(NSString*)string
{
return [string stringByReplacingOccurrencesOfString:@"," withString:@""];
}