swift - 为隐藏和显示密码创建一个通用类

标签 swift

func hideShowPasswordButton() {

    var hideShowSize: CGSize = "12345".sizeWithAttributes([NSFontAttributeName:UIFont.systemFontOfSize(14.0)])
    var hideShow: UIButton = UIButton(type: UIButtonType.System)
    hideShow.frame = CGRect(x: 0, y: 0, width: hideShowSize.width, height: self.userPasswordText.frame.size.height)
    hideShow.setImage(hidePasswordImage, forState: UIControlState.Normal)
    self.userPasswordText.rightView = hideShow
    self.userPasswordText.rightViewMode = UITextFieldViewMode.Always
    hideShow.addTarget(self, action: "hideShowPasswordTextField:", forControlEvents: UIControlEvents.TouchUpInside)
}

func hideShowPasswordTextField(sender: AnyObject) {

    var hideShow: UIButton = (self.userPasswordText.rightView as? UIButton)!
    if !self.userPasswordText.secureTextEntry {
        self.userPasswordText.secureTextEntry = true
        hideShow.setImage(hidePasswordImage, forState: UIControlState.Normal)
    } else {
        self.userPasswordText.secureTextEntry = false
        hideShow.setImage(showPasswordImage, forState: UIControlState.Normal)
    }
    self.userPasswordText.becomeFirstResponder()
}

我想用这些函数创建一个自定义类,以便我可以在所有 View Controller 中使用它。 任何帮助将不胜感激谢谢

最佳答案

您有 3 个选择:

1) 子类 UITextField 并使用这个子类文本字段:

 class PasswordField: UITextField {
    func hideShowPasswordButton() {

        var hideShowSize: CGSize = "12345".sizeWithAttributes([NSFontAttributeName:UIFont.systemFontOfSize(14.0)])
        var hideShow: UIButton = UIButton(type: UIButtonType.System)
        hideShow.frame = CGRect(x: 0, y: 0, width: hideShowSize.width, height: frame.size.height)
        hideShow.setImage(hidePasswordImage, forState: UIControlState.Normal)
        rightView = hideShow
        rightViewMode = UITextFieldViewMode.Always
        hideShow.addTarget(self, action: #selector(hideShowPasswordTextField(_:)), forControlEvents: UIControlEvents.TouchUpInside)
    }

    func hideShowPasswordTextField(sender: AnyObject) {

        var hideShow: UIButton = (rightView as? UIButton)!
        if !secureTextEntry {
            secureTextEntry = true
            hideShow.setImage(hidePasswordImage, forState: UIControlState.Normal)
        } else {
            secureTextEntry = false
            hideShow.setImage(showPasswordImage, forState: UIControlState.Normal)
        }
        becomeFirstResponder()
    }
}

2) 扩展 UITextField:

extension UITextField {
    func hideShowPasswordButton() {

        var hideShowSize: CGSize = "12345".sizeWithAttributes([NSFontAttributeName:UIFont.systemFontOfSize(14.0)])
        var hideShow: UIButton = UIButton(type: UIButtonType.System)
        hideShow.frame = CGRect(x: 0, y: 0, width: hideShowSize.width, height: frame.size.height)
        hideShow.setImage(hidePasswordImage, forState: UIControlState.Normal)
        rightView = hideShow
        rightViewMode = UITextFieldViewMode.Always
        hideShow.addTarget(self, action: #selector(hideShowPasswordTextField(_:)), forControlEvents: UIControlEvents.TouchUpInside)
    }

    func hideShowPasswordTextField(sender: AnyObject) {

        var hideShow: UIButton = (rightView as? UIButton)!
        if !secureTextEntry {
            secureTextEntry = true
            hideShow.setImage(hidePasswordImage, forState: UIControlState.Normal)
        } else {
            secureTextEntry = false
            hideShow.setImage(showPasswordImage, forState: UIControlState.Normal)
        }
        becomeFirstResponder()
    }
}

3) 创建协议(protocol),例如ViewController 并设置其默认实现:

protocol PasswordHidable: class {
    var userPasswordText: UITextField { get }

    func hideShowPasswordButton()
    func hideShowPasswordTextField(sender: AnyObject)
}

extension PasswordHidable {
    func hideShowPasswordButton() {

        var hideShowSize: CGSize = "12345".sizeWithAttributes([NSFontAttributeName:UIFont.systemFontOfSize(14.0)])
        var hideShow: UIButton = UIButton(type: UIButtonType.System)
        hideShow.frame = CGRect(x: 0, y: 0, width: hideShowSize.width, height: self.userPasswordText.frame.size.height)
        hideShow.setImage(hidePasswordImage, forState: UIControlState.Normal)
        self.userPasswordText.rightView = hideShow
        self.userPasswordText.rightViewMode = UITextFieldViewMode.Always
        hideShow.addTarget(self, action: #selector(hideShowPasswordTextField(_:), forControlEvents: UIControlEvents.TouchUpInside)
    }

    func hideShowPasswordTextField(sender: AnyObject) {

        var hideShow: UIButton = (self.userPasswordText.rightView as? UIButton)!
        if !self.userPasswordText.secureTextEntry {
            self.userPasswordText.secureTextEntry = true
            hideShow.setImage(hidePasswordImage, forState: UIControlState.Normal)
        } else {
            self.userPasswordText.secureTextEntry = false
            hideShow.setImage(showPasswordImage, forState: UIControlState.Normal)
        }
        self.userPasswordText.becomeFirstResponder()
    }
}

关于swift - 为隐藏和显示密码创建一个通用类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38761403/

相关文章:

swift - 快速检查 Parse 电子邮件验证的状态

swift - Swift3 中多返回值函数的文档注释

ios - 如何在没有 CaptiveNetwork 弃用框架的情况下在 Swift 2.0 中获取 ssid?

swift - String.init(contentsOfFile :) replacement for Linux?

json - 使用带参数的 POST 请求获取 JSON 结果

swift - 改变触摸按钮的颜色......然后再回来

ios - swift : how to detect the error type using the debugger?

swift - 我查看用户是否赢得井字游戏比赛的逻辑不起作用

ios - Playground - DispatchQueue.main.asyncAfter 不工作

swift - 在 Swift 中编写多个嵌套的 for...in 循环