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/