第一次写东西,也是借鉴两位前辈的
iOS高度封装格式化手机号,银行卡输入样式,限制输入内容 - 简书
我把两位前辈的东西整合了一下,第一位的问题在于格式化话后,删除的时候有点小bug,然后我就使用了第二篇的方法来进行删除的优化
@interface UITextField ()(format)
- (BOOL)valueChangeValueString:(NSString *)string shouldChangeCharactersInRange:(NSRange)range;
@end
PhoneTextField.h
#import "NSString+Helper.h"
@interface PhoneTextField : UITextField
@end
PhoneTextField.m
@implementation PhoneTextField
- (BOOL)valueChangeValueString:(NSString *)string shouldChangeCharactersInRange:(NSRange)range{
// NSString *text = self.text;
if (self.text.length == 13 && range.length == 0) {
return NO;
}
string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSMutableString *mStr = [NSMutableString stringWithString:self.text];
// 删减字符
if(string.length == 0 && range.location < self.text.length) {
NSString *removeTemp = [self.text substringWithRange:NSMakeRange(range.location, range.length)];
NSString *removeTempFontier = @"";
if(range.location >= 1) {
removeTempFontier = [self.text substringWithRange:NSMakeRange(range.location - 1, range.length)];
}
if(![removeTemp isEqualToString:@" "]) {
[mStr deleteCharactersInRange:NSMakeRange(range.location, range.length)];
NSMutableString *tempMutableStr = [NSMutableString stringWithString:[mStr stringRemoveBlank]];
if(tempMutableStr.length >= 4) {
[tempMutableStr insertString:@" " atIndex:3];
}
if(tempMutableStr.length >= 9) {
[tempMutableStr insertString:@" " atIndex:8];
}
[self setText:tempMutableStr];
}
// 判断当前位置往前一个字符是否为空格
if([removeTempFontier isEqualToString:@" "]) {
[self setTextRangeWithOffset:range.location - 1];
} else {
[self setTextRangeWithOffset:range.location];
}
return NO;
}
// 输入字符
if(string.length >0) {
[mStr deleteCharactersInRange:NSMakeRange(range.location, range.length)];
NSUInteger location = range.location + 1;
NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789\\b"];
if ([string rangeOfCharacterFromSet:[characterSet invertedSet]].location != NSNotFound) {
return NO;
}
if(location==1&&![string isEqualToString:@"1"]){
return NO;
}
if(range.location == 3 || range.location == 8) {
location += 1;
}
[mStr insertString:string atIndex:range.location];
// 每次输入都先清除空格
NSMutableString *noBlankString = [NSMutableString stringWithString:[mStr stringRemoveBlank]];
// 插入空格
if(noBlankString.length >= 4 && noBlankString.length < 8) {
[noBlankString insertString:@" " atIndex:3];
} else if(noBlankString.length > 7) {
[noBlankString insertString:@" " atIndex:3];
[noBlankString insertString:@" " atIndex:8];
}
[self setText:noBlankString];
[self setTextRangeWithOffset:location];
return NO;
}
return YES;
}
- (void)setTextRangeWithOffset:(NSUInteger)offset {
UITextPosition* beginning = self.beginningOfDocument;
UITextPosition* startPosition = [self positionFromPosition:beginning offset:offset];
UITextPosition* endPosition = [self positionFromPosition:beginning offset:offset];
UITextRange* selectionRange = [self textRangeFromPosition:startPosition toPosition:endPosition];
[self setSelectedTextRange:selectionRange];
}
@end
NSString+Helper.h
-(NSString *)stringRemoveBlank{
return [self stringByReplacingOccurrencesOfString:@" " withString:@""];
}