本文介绍了我可可可隐藏/显示NSTextField/NSSecureTextField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有一种方法可以在Cocoa中打开和关闭secureTextField? (OSX).我希望用户可以选择查看其密码.
Is there a way to turn secureTextField on and off in Cocoa? (OSX). I'd like users to have the option to see their passwords.
在iOS中,我可以执行[textField setSecureTextEntry:YES];
In iOS, I can do something like [textField setSecureTextEntry:YES];
我找到了[secureTextField setEchoBullets],但这不是我想要的.
I found [secureTextField setEchoBullets] but that's not what I want.
任何帮助表示赞赏.
推荐答案
对我来说,在同一个NSTextField中具有两个不同的单元格并在它们之间进行切换非常好.
For me works perfectly to have two different cells in the same NSTextField and switch between them.
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];
}
}
}
界面
@interface OSXEdit : NSTextField
{
@public
NSTextFieldCell *cell;
NSSecureTextFieldCell *scell;
}
@end
构造函数
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;
}
和析构函数
void osedit_destroy(OSEdit *edit)
{
OSXEdit *ledit = (OSXEdit*)edit;
[ledit->cell release];
[ledit->scell release];
[ledit release];
}
这篇关于我可可可隐藏/显示NSTextField/NSSecureTextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!