使用secureTextEntry为UITextFieldselect键盘的语言

我坚持改变密码字段的语言的问题。 在我的应用程序中,我需要在希伯来语中inputlogin名/密码,而不必关心当前的语言环境。 当我尝试inputlogin,那么它是好的,我可以将键盘更改为希伯来语,并inputlogin。 但是当我尝试在安全的textField中input密码时,键盘出现时没有select语言button,所以我只能input英文字母。

事情是,login/密码可以用英文或希伯来语。

我怎样才能把select语言button到安全的textField?

没有find解决办法。 必须做这个片段:

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { textField.text = [textField.text stringByReplacingCharactersInRange:range withString:string]; NSString *pass = password; pass = [pass stringByReplacingCharactersInRange:range withString:string]; [password release]; password = nil; password = [[NSString stringWithString:pass] retain]; [self hideTextInTextFieldExceptOne:string]; [self performSelector:@selector(hideTextInTextField) withObject:self afterDelay:1.0]; return NO; } - (void)hideTextInTextFieldExceptOne:(NSString *)string { int lenght = [passwordTextField.text length]; for (int i = 0; i < lenght-1; i++) { NSRange range = NSMakeRange(i, 1); passwordTextField.text = [passwordTextField.text stringByReplacingCharactersInRange:range withString:@"*"]; } } - (void)hideTextInTextField { NSUInteger lenght = [passwordTextField.text length]; passwordTextField.text = @""; for (int i = 0; i < lenght; i++) { passwordTextField.text = [passwordTextField.text stringByAppendingString:@"*"]; } } 

优化的@lonlywolf回答更好的性能代码:优化只有两个方法,其中周期,这减慢了程序时,键入超过30个符号

 - (void)hideTextInTextFieldExceptOne:(NSString *)string { int lenght = [passwordTextField.text length]; if (lenght -1 > 0) { NSString *resultString = @""; for (int i = 0; i < lenght-1; i++) { resultString = [resultString stringByAppendingFormat:@"*"]; } NSRange range = NSMakeRange(0, lenght - 1); passwordTextField.text = [fieldPassword.text stringByReplacingCharactersInRange:range withString:resultString]; } } - (void)hideTextInTextField { int lenght = [passwordTextField.text length]; if (lenght > 0) { NSString *resultString = @""; for (int i = 0; i < lenght; i++) { resultString = [resultString stringByAppendingFormat:@"*"]; } passwordTextField.text = resultString; } } 

不幸的是,这里发布的解决scheme不适用于具有复合字符的语言(如韩语)。

韩语(韩语)等语言具有复合字符,每个字母由多个符号组成。 例如,'ㅁ','ㅏ'和'ㄴ'都是单个字符,但是合并后就变成'만'了。

这是一个适用于所有语言的解决scheme。

将一个UILabel放在UITextField的顶部。 将UILabel的框架设置得比UITextField稍微小一些,这样它就可以放在UITextField中,但是仍然会遮住UITextField的文本。 textField:shouldChangeCharactersInRange:withReplacementString在文本完成之前调用。 这意味着我们需要自己完成文本。 这对于像韩文这样的复合字符语言来说很难做到。 相反,注册UITextFieldTextDidChangeNotification将在新文本出现在UITextField后被调用。

 @interface MKSecureTextField()<UITextFieldDelegate> @property (nonatomic, strong) UITextField* textField; @property (nonatomic, strong) UILabel* hideLabel; @property (nonatomic, strong) NSTimer* hideTimer; @property (nonatomic, strong) NSTimer* blinkTimer; @end @implementation MKSecureTextField - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)]; _textField.userInteractionEnabled = YES; _textField.borderStyle = UITextBorderStyleRoundedRect; _textField.font = [UIFont systemFontOfSize:14]; _textField.placeholder = @"enter text"; _textField.autocorrectionType = UITextAutocorrectionTypeNo; _textField.keyboardType = UIKeyboardTypeDefault; _textField.returnKeyType = UIReturnKeyDone; _textField.clearButtonMode = UITextFieldViewModeWhileEditing; _textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; _textField.delegate = self; self.hideLabel = [[UILabel alloc] initWithFrame:CGRectMake(6, 5, frame.size.width-10, frame.size.height-12)]; _hideLabel.backgroundColor = [UIColor whiteColor]; [self addSubview:_textField]; [self addSubview:_hideLabel]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidChange:) name:UITextFieldTextDidChangeNotification object:nil]; } return self; } 

当收到UITextFieldTextDidChangeNotification时,隐藏除最后一个字符以外的所有字符。 隐藏文本将以编程方式设置在UILabel上。 此外,安排一个计时器,将隐藏最后一个字符。 如果input更多文本,则此计时器将失效。

 - (void)textFieldDidChange:(NSNotification*)notification { UITextField* textField = notification.object; if (textField == _textField) { NSString* text = textField.text; [self hideExceptLastCharacter:text]; [self.hideTimer invalidate]; self.hideTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(hideLastCharacter) userInfo:nil repeats:NO]; } } 

UILabel没有闪烁的光标。 所以我们通过在'|'之间交替来模拟它, 和一个空间。 编辑开始时闪烁的光标被放置,编辑结束时被删除。

 - (void)textFieldDidBeginEditing:(UITextField *)textField { if (_hideLabel.text == nil) { _hideLabel.text = @"|"; } else { _hideLabel.text = [_hideLabel.text stringByAppendingString:@"|"]; } self.blinkTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(blinkCursor) userInfo:nil repeats:YES]; } - (void)textFieldDidEndEditing:(UITextField *)textField { NSRange range; range.location = _hideLabel.text.length - 1; range.length = 1; _hideLabel.text = [_hideLabel.text stringByReplacingCharactersInRange:range withString:@""]; [_blinkTimer invalidate]; } - (void)blinkCursor { if (_hideLabel.text.length > 0) { static BOOL visible = YES; NSRange range; range.location = _hideLabel.text.length - 1; range.length = 1; if (visible) { _hideLabel.text = [_hideLabel.text stringByReplacingCharactersInRange:range withString:@" "]; } else { _hideLabel.text = [_hideLabel.text stringByReplacingCharactersInRange:range withString:@"|"]; } visible = !visible; } } 

除最后一个字符以外的字符被隐藏。 这在UILabel上设置。 UITextField保持不变。

 - (void)hideExceptLastCharacter:(NSString*)string { int length = [_textField.text length]; NSString* s = @""; for (int i = 0; i < length-1; i++) { s = [s stringByAppendingString:@"●"]; } if (_textField.text.length > 0) { _hideLabel.text = [s stringByAppendingString:[_textField.text substringFromIndex:_textField.text.length-1]]; _hideLabel.text = [_hideLabel.text stringByAppendingString:@"|"]; } else { _hideLabel.text = @"|"; } } - (void)hideLastCharacter { if (_hideLabel.text.length > 1) { NSRange range; range.location = [_hideLabel.text length]-2; range.length = 1; _hideLabel.text = [_hideLabel.text stringByReplacingCharactersInRange:range withString:@"●"]; } } - (void)dealloc { [_hideTimer invalidate]; [_blinkTimer invalidate]; [[NSNotificationCenter defaultCenter] removeObserver:self]; } @end 

看到这个Github项目作为参考。