thieumao

Custom Password TextField

Mar 12th, 2017
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.35 KB | None | 0 0
  1. import UIKit
  2.  
  3. class CustomPasswordTextField: CustomInsetTextField {
  4.    
  5.     let eyeOpenImage = UIImage(named: kEyeOpenImageName)
  6.     let eyeCloseImage = UIImage(named: kEyeCloseImageName)
  7.     var eyeButton = UIButton(type: .custom)
  8.    
  9.     public override init(frame: CGRect) {
  10.         super.init(frame: frame)
  11.         setup()
  12.     }
  13.    
  14.     public required init?(coder: NSCoder) {
  15.         super.init(coder: coder)
  16.         setup()
  17.     }
  18.    
  19.     func setup() {
  20.         eyeButton.frame = CGRect(x: 0, y: 0, width: 26, height: 9)
  21.         eyeButton.contentMode = UIViewContentMode.scaleAspectFit
  22.         eyeButton.backgroundColor = UIColor.clear
  23.         eyeButton.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 10)
  24.         eyeButton.addTarget(self, action: #selector(onEyeButtonClicked(_:)), for: .touchUpInside)
  25.         eyeButton.setImage(eyeCloseImage, for: .normal)
  26.         rightView = eyeButton
  27.         rightViewMode = .always
  28.         isSecureTextEntry = false
  29.     }
  30.    
  31.     func onEyeButtonClicked(_ sender: Any) {
  32.         isSecureTextEntry = !isSecureTextEntry
  33.         if isSecureTextEntry {
  34.             eyeButton.setImage(eyeCloseImage, for: .normal)
  35.             rightView = eyeButton
  36.         } else {
  37.             eyeButton.setImage(eyeOpenImage, for: .normal)
  38.             rightView = eyeButton
  39.         }
  40.     }
  41.    
  42. }
Advertisement
Add Comment
Please, Sign In to add comment