UITextField 输入掩码?

UITextField input mask?

http://sublime.nyasha.me/admin/form_masks.html

我使用这种技术在 html/css/java 等中取得了很好的效果,但是如何在 objective-c 中取得这种效果?

到目前为止,我已经阅读了有关数字格式的内容,但是我怎么才能真正拥有它,所以当内容可编辑时,它会在他们输入时显示格式化版本,并且不允许他们超过 .length 在某些地区。

我需要使用它的例子是

00:00:0000h:00m:00s 所以在编辑时它会达到 __:__:____h:__m:__s 我一直在网上搜索,到目前为止找不到这样的技术我考虑过以编程方式做这样的事情..

3x UITextField 1x UILabel

if (editing is commenced && value changed) {
    if (.length == 2) {
        Move onto next UITextField;
    }
} 

并在文本字段上显示 UILabel Masked,还自定义文本字段,使 3 个看起来是一个。

-- 编辑

//Programming the Textfield Mask
-(void)reformatAsCardNumber:(UITextField *)textField
{
    NSUInteger targetCursorPosition =
    [textField offsetFromPosition:textField.beginningOfDocument
                       toPosition:textField.selectedTextRange.start];

    NSString *cardNumberWithoutSpaces =
    [self removeNonDigits:textField.text
andPreserveCursorPosition:&targetCursorPosition];

    if ([cardNumberWithoutSpaces length] > 6) {
        [textField setText:previousTextFieldContent];
        textField.selectedTextRange = previousSelection;
        return;
    }

    NSString *cardNumberWithSpaces =
    [self insertSpacesEveryFourDigitsIntoString:cardNumberWithoutSpaces
                      andPreserveCursorPosition:&targetCursorPosition];

    textField.text = cardNumberWithSpaces;
    UITextPosition *targetPosition =
    [textField positionFromPosition:[textField beginningOfDocument]
                             offset:targetCursorPosition];

    [textField setSelectedTextRange:
     [textField textRangeFromPosition:targetPosition
                           toPosition:targetPosition]
     ];
}

-(BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string
{
    previousTextFieldContent = textField.text;
    previousSelection = textField.selectedTextRange;

    return YES;
}

- (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;
}
- (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) && ((i % 2) == 0)) {
            [stringWithAddedSpaces appendString:@":"];
            if (i < cursorPositionInSpacelessString) {
                (*cursorPosition)++;
            }
        }
        unichar characterToAdd = [string characterAtIndex:i];
        NSString *stringToAdd =
        [NSString stringWithCharacters:&characterToAdd length:1];

        [stringWithAddedSpaces appendString:stringToAdd];
    }

    return stringWithAddedSpaces;
}

然后调用它

[_answerTextField addTarget:self
                  action:@selector(reformatAsCardNumber:)
        forControlEvents:UIControlEventEditingChanged];

我需要预先确定的文本,例如 __:__:__ 或最好是 __h:__m:__s,因此当您开始输入时,您会在填写时得到 00h:0_m:__

就像现在一样,我得到 `` 然后当我开始输入时我得到 00:0 等等。

在屏幕上更新之前,使用 textFieldShouldChange 方法修改输入/移动您认为合适的光标

UITextFieldDelegate:

textField:shouldChangeCharactersInRange:replacementString:
询问代表是否应更改指定的文本。
每当用户在文本字段中键入新字符或删除现有字符时,文本字段都会调用此方法。


此外,委托中还有 textDidChange,它在显示文本后起作用


这些是 DELEGATE 方法,因此您必须成为 textField 的委托:

//1 conform to the delegate
@interface MyViewController <UITextFieldDelegate>

然后

//2 set yourself as delegate (can also be done through IB)
textView.delegate = self;