OOUITextField.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //
  2. // OOUITextField.swift
  3. // o2app
  4. //
  5. // Created by 刘振兴 on 2017/9/6.
  6. // Copyright © 2017年 zone. All rights reserved.
  7. //
  8. import UIKit
  9. import SwiftValidator
  10. protocol OOUITextFieldReturnNextDelegate {
  11. func next()
  12. }
  13. @IBDesignable
  14. open class OOUITextField: UITextField {
  15. //左视图普通图片
  16. @IBInspectable open var leftImage:UIImage?
  17. //左视图高亮图片
  18. @IBInspectable open var leftLightImage:UIImage?
  19. //下划线普通颜色
  20. @IBInspectable open var lineColor:UIColor?
  21. //下划线高亮颜色
  22. @IBInspectable open var lineLightColor:UIColor?
  23. //下划线
  24. fileprivate var lineView:UIView!
  25. let validator = Validator()
  26. var rule:RegexRule? {
  27. didSet {
  28. validator.registerField(self, rules: [rule!])
  29. }
  30. }
  31. var returnNextDelegate: OOUITextFieldReturnNextDelegate?
  32. public override init(frame: CGRect) {
  33. super.init(frame: frame)
  34. commitInit()
  35. }
  36. open override func prepareForInterfaceBuilder() {
  37. super.prepareForInterfaceBuilder()
  38. }
  39. required public init?(coder aDecoder: NSCoder) {
  40. super.init(coder: aDecoder)
  41. }
  42. open override func awakeFromNib() {
  43. commitInit()
  44. }
  45. fileprivate func commitInit() {
  46. self.textColor = UIColor.hexInt(0x666666)
  47. self.font = UIFont(name: "PingFangSC-Regular", size: 15)!
  48. self.attributedPlaceholder = NSMutableAttributedString(string: self.placeholder ?? "", attributes: [NSAttributedString.Key.foregroundColor:UIColor.hexInt(0x999999),NSAttributedString.Key.font:UIFont(name: "PingFangSC-Regular", size: 13)!])
  49. leftViewMode = .always
  50. self.delegate = self
  51. let iv = UIImageView(image: leftImage, highlightedImage: leftLightImage)
  52. iv.frame = CGRect(x: 0, y: 0, width: (leftImage?.size.width)! , height: (leftImage?.size.height)!)
  53. let ivContainer = UIView(frame: CGRect(x: 0, y: 0, width: iv.frame.width + 10, height: iv.frame.height))
  54. ivContainer.addSubview(iv)
  55. leftView = ivContainer
  56. lineView = UIView(frame:CGRect(x: 0, y: frame.height - 0.5, width: frame.width, height: 0.5))
  57. lineView.backgroundColor = lineColor
  58. addSubview(lineView)
  59. }
  60. open override func layoutSubviews() {
  61. super.layoutSubviews()
  62. lineView.frame = CGRect(x: 0, y: self.frame.height - 0.5, width: self.frame.width, height: 0.5)
  63. }
  64. open func themeUpdate(leftImage:UIImage?, leftLightImage:UIImage?, lineColor:UIColor?, lineLightColor:UIColor?) {
  65. self.leftImage = leftImage
  66. self.leftLightImage = leftLightImage
  67. self.lineColor = lineColor
  68. self.lineLightColor = lineLightColor
  69. let lv = self.leftView?.subviews.first as! UIImageView
  70. lv.image = leftImage
  71. lv.highlightedImage = leftLightImage
  72. lineView.backgroundColor = lv.isHighlighted ? lineLightColor : lineColor
  73. }
  74. fileprivate func changeShowStatus(_ status:Bool = false) -> Void {
  75. if status {
  76. let lv = self.leftView?.subviews.first as! UIImageView
  77. lv.isHighlighted = true
  78. lineView.backgroundColor = lineLightColor
  79. }else{
  80. let lv = self.leftView?.subviews.first as! UIImageView
  81. lv.isHighlighted = false
  82. lineView.backgroundColor = lineColor
  83. }
  84. }
  85. }
  86. extension OOUITextField:UITextFieldDelegate {
  87. public func textFieldDidBeginEditing(_ textField: UITextField) {
  88. changeShowStatus(true)
  89. }
  90. public func textFieldDidEndEditing(_ textField: UITextField) {
  91. changeShowStatus(false)
  92. validationRule(textField)
  93. }
  94. public func textFieldShouldReturn(_ textField: UITextField) -> Bool {
  95. guard let _ = rule else {
  96. return true
  97. }
  98. validationRule(textField)
  99. if returnNextDelegate != nil {
  100. returnNextDelegate?.next()
  101. }
  102. return true
  103. }
  104. func validationRule(_ textField: UITextField) {
  105. validator.validateField(textField) { (error) in
  106. if error != nil {
  107. }
  108. }
  109. }
  110. }