我遇到了更改密码字段语言的问题。在我的应用程序中,我需要输入希伯来语中的登录名/密码,而不管当前的语言环境。当我尝试输入登录时,它就可以了,我可以将键盘更改为希伯来语并输入登录名。但是当我尝试在安全的textField中输入密码时,键盘出现时没有选择语言按钮,所以我只能输入英文字母。
事情是登录/密码可以是英文或希伯来文。
如何将选择语言按钮放入受保护的textField?
答案 0 :(得分:6)
没有找到解决方案。不得不制作这个片段:
-(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:@"*"];
}
}
答案 1 :(得分:1)
优化@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;
}
}
答案 2 :(得分:1)
不幸的是,此处发布的解决方案不适用于具有复合字符的语言(如韩语)。
韩语(韩语)等语言具有复合字符,每个字母由多个符号组成。例如,'ㅁ','ㅏ'和'ㄴ'都是单独的字符,但组合后,它变为'만',被视为单个字母。
这是适用于所有语言的解决方案。
将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上设置。另外,安排一个隐藏最后一个字符的计时器。如果键入更多文本,则此计时器无效。
- (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没有闪烁的光标。所以我们通过在&#39; |&#39;之间交替来模仿它。和一个空间。编辑开始时会放置闪烁的光标,编辑结束时会将其删除。
- (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 project以供参考。