OOPasswordTextField.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // OOPasswordTextField.swift
  3. // o2app
  4. //
  5. // Created by 刘振兴 on 2017/10/16.
  6. // Copyright © 2017年 zone. All rights reserved.
  7. //
  8. import UIKit
  9. import SwiftValidator
  10. class OOPasswordTextField: UITextField {
  11. //左视图普通图片
  12. @IBInspectable open var customLeftViewText:String?
  13. //左视图高亮图片
  14. @IBInspectable open var customRightView:UIButton?
  15. //下划线普通颜色
  16. @IBInspectable open var lineColor:UIColor?
  17. //下划线高亮颜色
  18. @IBInspectable open var lineLightColor:UIColor?
  19. //下划线
  20. fileprivate var lineView:UIView!
  21. let validator = Validator()
  22. var rule:RegexRule? {
  23. didSet {
  24. validator.registerField(self, rules: [rule!])
  25. }
  26. }
  27. override init(frame: CGRect) {
  28. super.init(frame: frame)
  29. commonInit()
  30. }
  31. required init?(coder aDecoder: NSCoder) {
  32. super.init(coder: aDecoder)
  33. }
  34. override func prepareForInterfaceBuilder() {
  35. commonInit()
  36. }
  37. open override func awakeFromNib() {
  38. commonInit()
  39. }
  40. func commonInit(){
  41. self.borderStyle = .none
  42. self.isSecureTextEntry = true
  43. self.delegate = self
  44. self.textColor = UIColor.hexInt(0x666666)
  45. self.font = UIFont(name: "PingFangSC-Regular", size: 15)!
  46. self.attributedPlaceholder = NSMutableAttributedString(string: self.placeholder ?? "", attributes: [NSAttributedString.Key.foregroundColor:UIColor.hexInt(0x999999),NSAttributedString.Key.font:UIFont(name: "PingFangSC-Regular", size: 14)!])
  47. //leftView
  48. self.leftViewMode = .always
  49. let leftLabel = UILabel(frame:CGRect(x: 0, y: 0, width: 120, height: 30))
  50. leftLabel.font = UIFont.systemFont(ofSize: 14)
  51. leftLabel.text = customLeftViewText
  52. leftLabel.textColor = UIColor.hexInt(0x333333)
  53. self.leftView = leftLabel
  54. //rightView
  55. self.rightViewMode = .always
  56. self.customRightView = UIButton(type: .custom)
  57. self.customRightView?.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
  58. self.customRightView?.setImage(#imageLiteral(resourceName: "icon_password_read_normal"), for: .normal)
  59. self.customRightView?.theme_setImage(ThemeImagePicker(keyPath: "Icon.icon_password_read_selected"), forState: .selected)
  60. self.customRightView?.addTarget(self, action: #selector(pwdTextSwitch(_:)), for: .touchUpInside)
  61. self.rightView = customRightView!
  62. lineView = UIView(frame:CGRect(x: 0, y: frame.height - 0.5, width: frame.width, height: 0.5))
  63. lineView.backgroundColor = lineColor
  64. addSubview(lineView)
  65. }
  66. @objc func pwdTextSwitch(_ sender:UIButton){
  67. sender.isSelected = !sender.isSelected
  68. if sender.isSelected {
  69. let tempPwdStr = self.text
  70. self.text = ""
  71. self.isSecureTextEntry = false
  72. self.text = tempPwdStr
  73. } else {
  74. let tempPwdStr = self.text
  75. self.text = ""
  76. self.isSecureTextEntry = true
  77. self.text = tempPwdStr
  78. }
  79. }
  80. open override func layoutSubviews() {
  81. super.layoutSubviews()
  82. lineView.frame = CGRect(x: 0, y: self.frame.height - 0.5, width: self.frame.width, height: 0.5)
  83. }
  84. fileprivate func changeShowStatus(_ status:Bool = false) -> Void {
  85. if status {
  86. lineView.backgroundColor = lineLightColor
  87. }else{
  88. lineView.backgroundColor = lineColor
  89. }
  90. }
  91. }
  92. extension OOPasswordTextField:UITextFieldDelegate {
  93. public func textFieldDidBeginEditing(_ textField: UITextField) {
  94. changeShowStatus(true)
  95. }
  96. public func textFieldDidEndEditing(_ textField: UITextField) {
  97. changeShowStatus(false)
  98. validationRule(textField)
  99. }
  100. public func textFieldShouldReturn(_ textField: UITextField) -> Bool {
  101. guard let _ = rule else {
  102. return true
  103. }
  104. validationRule(textField)
  105. return true
  106. }
  107. func validationRule(_ textField: UITextField) {
  108. validator.validateField(textField) { (error) in
  109. if error != nil {
  110. }
  111. }
  112. }
  113. }