UITextField手机号输入加空格
2019-07-01 本文已影响0人
没得到的是你没遇到呢
导语
现在写登录最长用到就是手机号的输入,有一个简单的需求 手机号留空格类似“123 4567 8910” 这里简单的记录一下
思路
1.监听UITextField 的输入在指定位置插入或删除空格
2.去除空格得到实际手机号
代码
监听UITextField 的输入
//实时更新输入框内容
[self.phoneTextfield addTarget:self action:@selector(phoneNum_tfChange:) forControlEvents:UIControlEventEditingChanged];
#pragma mark - 实时更新输入框
- (void)phoneNum_tfChange:(UITextField *)textField
{
/**
* 判断正确的光标位置
*/
NSUInteger targetCursorPostion = [textField offsetFromPosition:textField.beginningOfDocument toPosition:textField.selectedTextRange.start];
NSString *phoneNumberWithoutSpaces = [self removeNonDigits:textField.text andPreserveCursorPosition:&targetCursorPostion];
if([phoneNumberWithoutSpaces length]>11)
{
/**
* 避免超过11位的输入
*/
[textField setText:_previousTextFieldContent];
textField.selectedTextRange = _previousSelection;
return;
}
if ([phoneNumberWithoutSpaces length]>=11)
{
[textField resignFirstResponder];
phoneTypeLab.hidden = NO;
/**
* 此处运营商类型 填写后台判断返回的数据
*/
// phoneTypeLab.text = [HJHelper judgePhoneNumTypeOfMobileNum:[self.phoneTextfield.text stringByReplacingOccurrencesOfString:@" " withString:@""]];
phoneTypeLab.text = @"上海移动";
}
//号码小于11位
else
{
phoneTypeLab.hidden = YES;
}
NSString *phoneNumberWithSpaces = [self insertSpacesEveryFourDigitsIntoString:phoneNumberWithoutSpaces andPreserveCursorPosition:&targetCursorPostion];
textField.text = phoneNumberWithSpaces;
UITextPosition *targetPostion = [textField positionFromPosition:textField.beginningOfDocument offset:targetCursorPostion];
[textField setSelectedTextRange:[textField textRangeFromPosition:targetPostion toPosition:targetPostion]];
}
/**
* 除去非数字字符,确定光标正确位置
*
* @param string 当前的string
* @param cursorPosition 光标位置
*
* @return 处理过后的string
*/
- (NSString *)removeNonDigits:(NSString *)string andPreserveCursorPosition:(NSUInteger *)cursorPosition {
NSUInteger originalCursorPosition =*cursorPosition;
NSMutableString *digitsOnlyString = [NSMutableString new];
for (NSUInteger i=0; i<string.length; i++) {
unichar characterToAdd = [string characterAtIndex:i];
if(isdigit(characterToAdd)) {
NSString *stringToAdd = [NSString stringWithCharacters:&characterToAdd length:1];
[digitsOnlyString appendString:stringToAdd];
}
else {
if(i<originalCursorPosition) {
(*cursorPosition)--;
}
}
}
return digitsOnlyString;
}
/**
* 将空格插入我们现在的string 中,并确定我们光标的正确位置,防止在空格中
*
* @param string 当前的string
* @param cursorPosition 光标位置
*
* @return 处理后有空格的string
*/
- (NSString *)insertSpacesEveryFourDigitsIntoString:(NSString *)string andPreserveCursorPosition:(NSUInteger *)cursorPosition{
NSMutableString *stringWithAddedSpaces = [NSMutableString new];
NSUInteger cursorPositionInSpacelessString = *cursorPosition;
for (NSUInteger i=0; i<string.length; i++) {
if(i>0)
{
if(i==3 || i==7) {
[stringWithAddedSpaces appendString:@" "];
if(i<cursorPositionInSpacelessString) {
(*cursorPosition)++;
}
}
}
unichar characterToAdd = [string characterAtIndex:i];
NSString *stringToAdd = [NSString stringWithCharacters:&characterToAdd length:1];
[stringWithAddedSpaces appendString:stringToAdd];
}
return stringWithAddedSpaces;
}
#pragma mark - UITextFieldDelegate 判断输入框是否还可以编辑
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
_previousSelection = textField.selectedTextRange;
_previousTextFieldContent = textField.text;
if(range.location==0) {
if(string.integerValue >1)
{
return NO;
}
}
return YES;
}
demo
结束语
有用的话给个喜欢和Star 蟹蟹