Secure text .echosbullets not working for password field

白昼怎懂夜的黑 提交于 2019-12-02 06:48:27

You can accomplish what you want adding a second text field in top of your secure field. Add an IBAction to your check box to switch your fields isHidden property and copy the other textField stringValue and make it the first responder. Your implementation should look like something like this:

import Cocoa

class ViewController: NSViewController {
    @IBOutlet weak var password: NSSecureTextField!
    @IBOutlet weak var showPassword: NSTextField!
    @IBOutlet weak var shwpswd: NSButton!
    override func viewDidLoad() {
        super.viewDidLoad()
        shwpswd.state = .off
        showPassword.isHidden = true
    }
    override func viewDidAppear() {
        super.viewDidAppear()
        password.window?.makeFirstResponder(password)
    }
    @IBAction func showHidePassword(_ sender: NSButton) {
        showPassword.isHidden = !showPassword.isHidden
        password.isHidden = !password.isHidden
        if !showPassword.isHidden {
            showPassword.stringValue = password.stringValue
            showPassword.becomeFirstResponder()
        } else {
            password.stringValue = showPassword.stringValue
            password.becomeFirstResponder()
        }
    }
}

show/hide password sample

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