1、首先给TextField注册通知
[_mobileTextFeild addTarget:self action:@selector(textFieldChange:) forControlEvents:UIControlEventEditingChanged];
2、定义成员变量
NSString *previousTextFieldContent;
UITextRange *previousSelection;
3、实现
- (void)textFieldChange:(TsaaTextField *)sender{
if (sender == self.mobileTextFeild) {
UITextField *textField = (UITextField *)sender;
//限制手机账号长度13位(有两个空格)
NSUInteger targetCursorPosition = [textField offsetFromPosition:textField.beginningOfDocument toPosition:textField.selectedTextRange.start];
if (textField.text.length > 13) {
textField.text = self.oldMobile;
UITextPosition *targetPosition = [textField positionFromPosition:[textField beginningOfDocument] offset:targetCursorPosition - 1];
[textField setSelectedTextRange:[textField textRangeFromPosition:targetPosition toPosition :targetPosition]];
return;
}
NSString *currentStr = [textField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
NSString *preStr = [previousTextFieldContent stringByReplacingOccurrencesOfString:@" " withString:@""];
//正在执行删除操作时为0,否则为1
char editFlag = 0;
if (currentStr.length <= preStr.length) {
editFlag = 0;
}else{
editFlag = 1;
}
NSMutableString *tempStr = [NSMutableString new];
int spaceCount = 0;
if (currentStr.length < 3 && currentStr.length > -1) {
spaceCount = 0;
}else if (currentStr.length < 7 && currentStr.length > 2) {
spaceCount = 1;
}else if (currentStr.length < 12 && currentStr.length > 6) {
spaceCount = 2;
}
for (int i = 0; i < spaceCount; i++) {
if (i == 0) {
[tempStr appendFormat:@"%@%@", [currentStr substringWithRange:NSMakeRange(0, 3)], @" "];
}else if (i == 1) {
[tempStr appendFormat:@"%@%@", [currentStr substringWithRange:NSMakeRange(3, 4)], @" "];
}else if (i == 2) {
[tempStr appendFormat:@"%@%@", [currentStr substringWithRange:NSMakeRange(7, 4)], @" "];
}
}
if (currentStr.length == 11) {
[tempStr appendFormat:@"%@%@", [currentStr substringWithRange:NSMakeRange(7, 4)], @" "];
}
if (currentStr.length < 4) {
[tempStr appendString:[currentStr substringWithRange:NSMakeRange(currentStr.length - currentStr.length % 3, currentStr.length % 3)]];
}else if(currentStr.length > 3 && currentStr.length <12) {
NSString *str = [currentStr substringFromIndex:3];
[tempStr appendString:[str substringWithRange:NSMakeRange(str.length - str.length % 4, str.length % 4)]];
if (currentStr.length == 11) {
[tempStr deleteCharactersInRange:NSMakeRange(13, 1)];
}
}
textField.text = tempStr;
self.oldMobile = textField.text;
// 当前光标的偏移位置
NSUInteger curTargetCursorPosition = targetCursorPosition;
if (editFlag == 0) {
//删除
if (targetCursorPosition == 9 || targetCursorPosition == 4) {
// curTargetCursorPosition = targetCursorPosition - 1;
}
}else {
//添加
if (targetCursorPosition == 8 || targetCursorPosition == 4) {
curTargetCursorPosition = targetCursorPosition + 1;
}
if (textField.text.length > 9) {
if (targetCursorPosition == 9) {
curTargetCursorPosition = targetCursorPosition + 1;
}
}
}
UITextPosition *targetPosition = [textField positionFromPosition:[textField beginningOfDocument] offset:curTargetCursorPosition];
[textField setSelectedTextRange:[textField textRangeFromPosition:targetPosition toPosition :targetPosition]];
if (self.mobileTextFeild.text.length >= 13) {
self.mobileTextFeild.text = [self.mobileTextFeild.text substringToIndex:13];
self.accountVerify = YES;
[self changeConfirmButton];
return;
} else {
self.accountVerify = NO;
[self changeConfirmButton];
}
}
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (textField == self.mobileTextFeild) {
previousTextFieldContent = textField.text;
previousSelection = textField.selectedTextRange;
}
return YES;
}







网友评论