iOS 限制textField输入的长度

1.电话号码(带3-3-4效果)

  1. //指定代理
     self.phoneTextField.delegate = self;
  2.    //当编辑改变的时候,进行字符校验
    [self.phoneTextField addTarget:self action:@selector(reformatAsPhoneNumber:) forControlEvents:UIControlEventEditingChanged];
  3. #pragma mark - 手机号码分隔
    /**
    * 3-3-4手机号码分隔
    */
    -(void)reformatAsPhoneNumber:(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;
    } 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 ( textField == self.phoneHMDTextField &&range.location >= 11){
    return NO;
    }else {
    return YES;
    }
    }

二:监听代理方法(这样可以在if里通过判断,做几个textfield的限制输入)

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    if ( textField == self.phoneHMDTextField  && range.location >= 11){
return NO;
}else {
return YES;
}
}

三:

  //指定代理

    self.phoneTextField.delegate = self;

    //当编辑改变的时候,进行字符校验

    [self.phoneTextField addTarget:self action:@selector(reformatAsPhoneNumber:) forControlEvents:UIControlEventEditingChanged];

//监听字符改变的方法

- (void)textFieldDidChange:(UITextField *)textField
{
if (textField == self.textFieldName) {
if (textField.text.length > 15) {
        UITextRange *markedRange = [textField markedTextRange];
   if (markedRange) {
   return;
    }
//Emoji占2个字符,如果是超出了半个Emoji,用15位置来截取会出现Emoji截为2半
//超出最大长度的那个字符序列(Emoji算一个字符序列)的range
NSRange range = [textField.text rangeOfComposedCharacterSequenceAtIndex:15];
textField.text = [textField.text substringToIndex:range.location];
}
}
}
上一篇:iOS之限制TextField的输入长度


下一篇:lamp环境搭建(apache安装,mysql安装,php安装)