http://sublime.nyasha.me/admin/form_masks.html
我使用这种技术用html / css / java等实现了很好的结果,但是如何在objective-c中实现这个呢?
到目前为止,我已经阅读了有关数字格式的内容,但我怎么能真正拥有它,所以当内容可编辑时,它会显示格式化的版本,因为它们正在键入,并且不允许它们超过.length
in某些领域。
我需要使用它的例子是
00:00:00
或00h:00m:00s
因此,在编辑时,它会实现__:__:__
或__h:__m:__s
我一直在搜索互联网并找不到这样的技术,到目前为止我我以编程方式做了类似的事情。
3x UITextField
1x UILabel
if (editing is commenced && value changed) {
if (.length == 2) {
Move onto next UITextField;
}
}
在文本字段上显示UILabel Masked,但也自定义文本字段,使其看起来像是一个。
- 编辑
//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
等等。
答案 0 :(得分:3)
在屏幕上更新之前,使用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;