[英]Cocoa Can I Hide / Show an NSTextField / NSSecureTextField
Is there a way to turn secureTextField on and off in Cocoa?有没有办法在 Cocoa 中打开和关闭 secureTextField? (OSX).
(OSX)。 I'd like users to have the option to see their passwords.
我希望用户可以选择查看他们的密码。
In iOS, I can do something like [textField setSecureTextEntry:YES];在 iOS 中,我可以执行类似 [textField setSecureTextEntry:YES]; 的操作。
I found [secureTextField setEchoBullets] but that's not what I want.我找到了 [secureTextField setEchoBullets] 但这不是我想要的。
Any help appreciated.任何帮助表示赞赏。
I think you will need to have both an NSTextField
and an NSSecureTextField
. 我认为您将需要同时具有
NSTextField
和NSSecureTextField
。 You could put them in a tabless NSTabView
to make it a little easier to switch between them. 您可以将它们放在表格
NSTabView
以使其在它们之间进行切换更加容易。
For me works perfectly to have two different cells in the same NSTextField and switch between them. 对我来说,在同一个NSTextField中具有两个不同的单元格并在它们之间进行切换是完美的。
void osedit_set_password_mode(OSEdit *edit, const bool_t password_mode)
{
OSXEdit *ledit = (OSXEdit*)edit;
cassert_no_null(ledit);
if (password_mode == TRUE)
{
if ([ledit cell] == ledit->cell)
{
[ledit->scell setStringValue:[ledit->cell stringValue]];
[ledit->scell setBackgroundColor:[ledit->cell backgroundColor]];
[ledit->scell setTextColor:[ledit->cell textColor]];
[ledit->scell setAlignment:[ledit->cell alignment]];
[ledit->scell setFont:[ledit->cell font]];
[ledit setCell:ledit->scell];
}
}
else
{
if ([ledit cell] == ledit->scell)
{
[ledit->cell setStringValue:[ledit->scell stringValue]];
[ledit->cell setBackgroundColor:[ledit->scell backgroundColor]];
[ledit->cell setTextColor:[ledit->scell textColor]];
[ledit->cell setAlignment:[ledit->scell alignment]];
[ledit->cell setFont:[ledit->scell font]];
[ledit setCell:ledit->cell];
}
}
}
The interface 介面
@interface OSXEdit : NSTextField
{
@public
NSTextFieldCell *cell;
NSSecureTextFieldCell *scell;
}
@end
The constructor 构造函数
OSEdit *osedit_create()
{
OSXEdit *edit = nil;
NSTextFieldCell *cell = nil;
edit = [[OSXEdit alloc] initWithFrame:NSZeroRect];
cell = [edit cell];
[cell setEditable:YES];
[cell setSelectable:YES];
[cell setBordered:YES];
[cell setBezeled:YES];
[cell setDrawsBackground:YES];
edit->cell = [cell retain];
edit->scell = [[NSSecureTextFieldCell alloc] init];
[edit->scell setEchosBullets:YES];
[edit->scell setEditable:YES];
[edit->scell setSelectable:YES];
[edit->scell setBordered:YES];
[edit->scell setBezeled:YES];
[edit->scell setDrawsBackground:YES];
return (OSEdit*)edit;
}
And destructor 和析构函数
void osedit_destroy(OSEdit *edit)
{
OSXEdit *ledit = (OSXEdit*)edit;
[ledit->cell release];
[ledit->scell release];
[ledit release];
}
I have found a simple solution by creating a custom font showing a bullet for all character.我通过创建一个显示所有字符的项目符号的自定义字体找到了一个简单的解决方案。 By this way, I just need to switch between the system font and my bullets font to show/hide a password.
通过这种方式,我只需要在系统字体和项目符号字体之间切换即可显示/隐藏密码。
I've found an easy way for creating the bullets font on the site https://www.glyphrstudio.com/online/我找到了一种在网站https://www.glyphrstudio.com/online/上创建项目符号字体的简单方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.