设置通知监听
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.navigationController.navigationBar.hidden = YES;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldTextChanged:) name:UITextFieldTextDidChangeNotification object:nil];
}
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
self.navigationController.navigationBar.hidden = NO;
[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
}
设置代理
self.phoneTF.delegate = self;
#define PhoneTFLength 13
#pragma mark - textField 通知 监听
- (void)textFieldTextChanged:(NSNotification *)noti {
UITextField *tf = noti.object;
if (tf == self.phoneTF) {
[self formatPhoneNumber:tf];
}
}
#pragma mark - Delegate Methods
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
previousTextFieldContent = textField.text;
previousSelection = textField.selectedTextRange;
if (range.location > PhoneTFLength-1){
return NO;
}
return YES;
}
#pragma mark - help
- (void)formatPhoneNumber:(UITextField*)textField{
NSUInteger targetCursorPosition = [textField offsetFromPosition:textField.beginningOfDocument toPosition:textField.selectedTextRange.start];
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)];
}
}
textField.text = mStrTemp;
// textField设置selectedTextRange
NSUInteger curTargetCursorPosition = targetCursorPosition;// 当前光标的偏移位置
if (targetCursorPosition == 9 || targetCursorPosition == 4){
if (editFlag == 0){//删除
curTargetCursorPosition = targetCursorPosition - 1;
}else {//添加
curTargetCursorPosition = targetCursorPosition + 1;
}
}
UITextPosition *targetPosition = [textField positionFromPosition:[textField beginningOfDocument] offset:curTargetCursorPosition];
[textField setSelectedTextRange:[textField textRangeFromPosition:targetPosition toPosition :targetPosition]];
}