Cocoa Can I Hide / Show an NSTextField / NSSecureTextField

妖精的绣舞 提交于 2019-12-10 14:29:10

问题


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.

In iOS, I can do something like [textField setSecureTextEntry:YES];

I found [secureTextField setEchoBullets] but that's not what I want.

Any help appreciated.


回答1:


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];
        }
    }
}

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];
}



回答2:


I think you will need to have both an NSTextField and an NSSecureTextField. You could put them in a tabless NSTabView to make it a little easier to switch between them.



来源:https://stackoverflow.com/questions/21640093/cocoa-can-i-hide-show-an-nstextfield-nssecuretextfield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!