[Demo下载地址]https://git.oschina.net/remainedmute/PhoneNumDemo.git
1.在UITextField的代理方法中实现手机号只能输入数字并满足我们的要求(首位只能是1,第二位只能是3,4,5,7,8其它不限制)
#pragma mark UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
previousTextFieldContent = textField.text;
previousSelection = textField.selectedTextRange;
if (range.location == 0){
NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:@"1"] invertedSet];
NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
BOOL basicTest = [string isEqualToString:filtered];
if (!basicTest){
//[self showMyMessage:@"只能输入数字"];
return NO;
}
}
else if (range.location == 1){
NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:@"^[3,4,5,7,8]$"] invertedSet];
NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
BOOL basicTest = [string isEqualToString:filtered];
if (!basicTest){
//[self showMyMessage:@"只能输入数字"];
return NO;
}
}
if (range.location > 12){
return NO;
}
return YES;
}
2.为了随时获取textfield里面的内容,给UITextField绑定
UIControlEventEditingChanged事件
[self.phoneTextField addTarget:self action:@selector(formatPhoneNumber:) forControlEvents:UIControlEventEditingChanged];
3.实现formatPhoneNumber:方法以来让手机号实现344格式
- (void)formatPhoneNumber:(UITextField)textField{
NSUInteger targetCursorPosition = [textField offsetFromPosition:textField.beginningOfDocument toPosition:textField.selectedTextRange.start];
NSLog(@"targetCursorPosition:%li", (long)targetCursorPosition);
NSString nStr = [textField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
NSString* preTxt = [previousTextFieldContent stringByReplacingOccurrencesOfString:@" " withString:@""];
char editFlag = 0;// 正在执行删除操作时为0,否则为1
if (nStr.length <= preTxt.length) {
editFlag = 0;
}else{
editFlag = 1;
}
if (nStr.length > 11) {
textField.text = previousTextFieldContent;
textField.selectedTextRange = previousSelection;
return;
}
NSString* spaceStr = @" ";
NSMutableString *mStrTemp = [NSMutableString string];
int spaceCount = 0;
if (nStr.length < 3 && nStr.length > -1) {
spaceCount = 0;
}else if (nStr.length < 7&& nStr.length > 2){
spaceCount = 1;
}else if (nStr.length < 12&& nStr.length > 6){
spaceCount = 2;
}
for (int i = 0; i < spaceCount; i++){
if (i == 0) {
[mStrTemp appendFormat:@"%@%@", [nStr substringWithRange:NSMakeRange(0, 3)],spaceStr];
}else if (i == 1){
[mStrTemp appendFormat:@"%@%@", [nStr substringWithRange:NSMakeRange(3, 4)], spaceStr];
}else if (i == 2){
[mStrTemp appendFormat:@"%@%@", [nStr substringWithRange:NSMakeRange(7, 4)], spaceStr];
}
}
if (nStr.length == 11){
[mStrTemp appendFormat:@"%@%@", [nStr substringWithRange:NSMakeRange(7, 4)], spaceStr];
}
if (nStr.length < 4 && nStr.length > 0){
[mStrTemp appendString:[nStr substringWithRange:NSMakeRange(nStr.length-nStr.length % 3,nStr.length % 3)]];
}else if(nStr.length > 3){
NSString *str = [nStr substringFromIndex:3];
[mStrTemp appendString:[str substringWithRange:NSMakeRange(str.length-str.length % 4,str.length % 4)]];
if (nStr.length == 11){
[mStrTemp deleteCharactersInRange:NSMakeRange(13, 1)];
}
}
NSLog(@"=======mstrTemp=%@",mStrTemp);
textField.text = mStrTemp;
// textField设置selectedTextRange
NSUInteger curTargetCursorPosition = targetCursorPosition;// 当前光标的偏移位置
if (editFlag == 0){
//删除
if (targetCursorPosition == 9 || targetCursorPosition == 4){
curTargetCursorPosition = targetCursorPosition - 1;
}
}
else {
//添加
if (nStr.length == 8 || nStr.length == 3){
curTargetCursorPosition = targetCursorPosition + 1;
}
}
UITextPosition *targetPosition = [textField positionFromPosition:[textField beginningOfDocument] offset:curTargetCursorPosition];
[textField setSelectedTextRange:[textField textRangeFromPosition:targetPosition toPosition :targetPosition]];
}