xcode 如何在ios中隐藏和查看密码之间切换?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13919934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:20:51  来源:igfitidea点击:

How to switch between hide and view password in ios?

iphoneobjective-ciosxcodeuitextfield

提问by MR Mido

Is there a clever way to let the user switch between hide and view password in an ios/iphone UITextField where the user can enter a password and would have the option of either hide it or view it. thanks in advance.

是否有一种巧妙的方法可以让用户在 ios/iphone UITextField 中在隐藏和查看密码之间切换,用户可以在其中输入密码并可以选择隐藏或查看它。提前致谢。

回答by Mario

Simply setting

简单设置

.secureTextEntry = YES

wont work I think due to a bug (or a feature), if the textField has focus.

如果 textField 具有焦点,我认为由于错误(或功能)而无法工作。

Use something like this to make it work also if the textField is currently firstResponder

如果 textField 当前是 firstResponder,也可以使用类似的方法使其工作

-(void) toggleTextFieldSecureEntry: (UITextField*) textField {
    BOOL isFirstResponder = textField.isFirstResponder; //store whether textfield is firstResponder

    if (isFirstResponder) [textField resignFirstResponder]; //resign first responder if needed, so that setting the attribute to YES works
    textField.secureTextEntry = !textField.secureTextEntry; //change the secureText attribute to opposite
    if (isFirstResponder) [self.textField becomeFirstResponder]; //give the field focus again, if it was first responder initially
}

回答by Sakshi Singla

- (IBAction)ShowPass:(UIButton *)sender {
    if (self.password.secureTextEntry == YES) {
        [self.showPass setTitle:@"HIDE" forState:(UIControlStateNormal)];
        self.password.secureTextEntry = NO;   
    }else{
        [self.showPass setTitle:@"SHOW" forState:(UIControlStateNormal)];
        self.password.secureTextEntry = YES;
    }
}

回答by Lukas Kukacka

Mario's answer works fine and helped me a lot.

马里奥的回答很好,对我帮助很大。

But if you use custom fontin a text field, once you change from secure entry to plain text, the text field's text will be drawn with wrong (not custom) font until user inputs another character.

但是如果您在文本字段中使用自定义字体,一旦您从安全输入更改为纯文本,文本字段的文本将使用错误(非自定义)字体绘制,直到用户输入另一个字符。

With a little hack I was able to fix this issue by simulating that character input from code.

通过一些小技巧,我能够通过模拟代码中的字符输入来解决这个问题。

This modified Mario's code should work for fields with custom fonts:

这个修改后的马里奥代码应该适用于具有自定义字体的字段:

- (void)toggleTextFieldSecureEntry:(UITextField *)textField {
    BOOL isFirstResponder = textField.isFirstResponder;

    if (isFirstResponder) {
        [textField resignFirstResponder];
    }

    textField.secureTextEntry = !textField.secureTextEntry;

    if (isFirstResponder) {
        [textField becomeFirstResponder];
    }

    // When using custom font and changing from secure entry to plain text, the text is initially drawn with wrong font
    // until a next character is input by user. This hack fixes the font immediatelly (simulate add and delete character)
    if (!textField.isSecureTextEntry) {
        [textField insertText:@"x"];
        [textField deleteBackward];
    }
}

回答by Atif Azad

You can use secureTextEntryproperty of UITextField. When you want to hide (show dot for each character) you can use yourTextField.secureTextEntry=YES;And when you want to show password use yourTextField.secureTextEntry=NO;

您可以使用 的secureTextEntry属性UITextField。当您想隐藏(为每个字符显示点)时,您可以使用yourTextField.secureTextEntry=YES;当您想显示密码时使用yourTextField.secureTextEntry=NO;

回答by Abhijeet

For Swift, only the assignment values change from YES/NO to true/false boolean values.

对于 Swift,只有赋值值从 YES/NO 更改为 true/false 布尔值。

password.secureTextEntry = true //Visible
password.secureTextEntry = false //InVisible