AnimatableTextField.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. //
  2. // Created by Jake Lin on 11/19/15.
  3. // Copyright © 2015 IBAnimatable. All rights reserved.
  4. //
  5. import UIKit
  6. @IBDesignable
  7. open class AnimatableTextField: UITextField, CornerDesignable, FillDesignable, BorderDesignable,
  8. ShadowDesignable, PaddingDesignable, SideImageDesignable, PlaceholderDesignable,
  9. Animatable {
  10. // MARK: - CornerDesignable
  11. @IBInspectable open var cornerRadius: CGFloat = CGFloat.nan {
  12. didSet {
  13. configureCornerRadius()
  14. }
  15. }
  16. open var cornerSides: CornerSides = .allSides {
  17. didSet {
  18. configureCornerRadius()
  19. }
  20. }
  21. @IBInspectable var _cornerSides: String? {
  22. didSet {
  23. cornerSides = CornerSides(rawValue: _cornerSides)
  24. }
  25. }
  26. // MARK: - FillDesignable
  27. @IBInspectable open var fillColor: UIColor? {
  28. didSet {
  29. configureFillColor()
  30. }
  31. }
  32. open var predefinedColor: ColorType? {
  33. didSet {
  34. configureFillColor()
  35. }
  36. }
  37. @IBInspectable var _predefinedColor: String? {
  38. didSet {
  39. predefinedColor = ColorType(string: _predefinedColor)
  40. }
  41. }
  42. @IBInspectable open var opacity: CGFloat = CGFloat.nan {
  43. didSet {
  44. configureOpacity()
  45. }
  46. }
  47. // MARK: - BorderDesignable
  48. open var borderType: BorderType = .solid {
  49. didSet {
  50. configureBorder()
  51. }
  52. }
  53. @IBInspectable var _borderType: String? {
  54. didSet {
  55. borderType = BorderType(string: _borderType)
  56. }
  57. }
  58. @IBInspectable open var borderColor: UIColor? {
  59. didSet {
  60. configureBorder()
  61. }
  62. }
  63. @IBInspectable open var borderWidth: CGFloat = CGFloat.nan {
  64. didSet {
  65. configureBorder()
  66. }
  67. }
  68. open var borderSides: BorderSides = .AllSides {
  69. didSet {
  70. configureBorder()
  71. }
  72. }
  73. @IBInspectable var _borderSides: String? {
  74. didSet {
  75. borderSides = BorderSides(rawValue: _borderSides)
  76. }
  77. }
  78. // MARK: - ShadowDesignable
  79. @IBInspectable open var shadowColor: UIColor? {
  80. didSet {
  81. configureShadowColor()
  82. }
  83. }
  84. @IBInspectable open var shadowRadius: CGFloat = CGFloat.nan {
  85. didSet {
  86. configureShadowRadius()
  87. }
  88. }
  89. @IBInspectable open var shadowOpacity: CGFloat = CGFloat.nan {
  90. didSet {
  91. configureShadowOpacity()
  92. }
  93. }
  94. @IBInspectable open var shadowOffset: CGPoint = CGPoint(x: CGFloat.nan, y: CGFloat.nan) {
  95. didSet {
  96. configureShadowOffset()
  97. }
  98. }
  99. // MARK: - PaddingDesignable
  100. @IBInspectable open var paddingLeft: CGFloat = CGFloat.nan
  101. @IBInspectable open var paddingRight: CGFloat = CGFloat.nan
  102. @IBInspectable open var paddingSide: CGFloat = CGFloat.nan
  103. override open func textRect(forBounds bounds: CGRect) -> CGRect {
  104. return paddedRect(forBounds: bounds)
  105. }
  106. override open func editingRect(forBounds bounds: CGRect) -> CGRect {
  107. return paddedRect(forBounds: bounds)
  108. }
  109. override open func placeholderRect(forBounds bounds: CGRect) -> CGRect {
  110. return paddedRect(forBounds: bounds)
  111. }
  112. // MARK: - SideImageDesignable
  113. @IBInspectable open var leftImage: UIImage? {
  114. didSet {
  115. configureImages()
  116. }
  117. }
  118. @IBInspectable open var leftImageLeftPadding: CGFloat = CGFloat.nan {
  119. didSet {
  120. configureImages()
  121. }
  122. }
  123. @IBInspectable open var leftImageRightPadding: CGFloat = CGFloat.nan {
  124. didSet {
  125. configureImages()
  126. }
  127. }
  128. @IBInspectable open var leftImageTopPadding: CGFloat = CGFloat.nan {
  129. didSet {
  130. configureImages()
  131. }
  132. }
  133. @IBInspectable open var rightImage: UIImage? {
  134. didSet {
  135. configureImages()
  136. }
  137. }
  138. @IBInspectable open var rightImageLeftPadding: CGFloat = CGFloat.nan {
  139. didSet {
  140. configureImages()
  141. }
  142. }
  143. @IBInspectable open var rightImageRightPadding: CGFloat = CGFloat.nan {
  144. didSet {
  145. configureImages()
  146. }
  147. }
  148. @IBInspectable open var rightImageTopPadding: CGFloat = CGFloat.nan {
  149. didSet {
  150. configureImages()
  151. }
  152. }
  153. // MARK: - CSSPlaceholderable
  154. @IBInspectable open var placeholderColor: UIColor? {
  155. didSet {
  156. configurePlaceholderColor()
  157. }
  158. }
  159. // MARK: - Animatable
  160. open var animationType: AnimationType = .none
  161. @IBInspectable var _animationType: String? {
  162. didSet {
  163. animationType = AnimationType(string: _animationType)
  164. }
  165. }
  166. @IBInspectable open var autoRun: Bool = true
  167. @IBInspectable open var duration: Double = Double.nan
  168. @IBInspectable open var delay: Double = Double.nan
  169. @IBInspectable open var damping: CGFloat = CGFloat.nan
  170. @IBInspectable open var velocity: CGFloat = CGFloat.nan
  171. @IBInspectable open var force: CGFloat = CGFloat.nan
  172. @IBInspectable var _timingFunction: String = "" {
  173. didSet {
  174. timingFunction = TimingFunctionType(string: _timingFunction)
  175. }
  176. }
  177. open var timingFunction: TimingFunctionType = .none
  178. // MARK: - Lifecycle
  179. open override func prepareForInterfaceBuilder() {
  180. super.prepareForInterfaceBuilder()
  181. configureInspectableProperties()
  182. }
  183. open override func awakeFromNib() {
  184. super.awakeFromNib()
  185. configureInspectableProperties()
  186. }
  187. open override func layoutSubviews() {
  188. super.layoutSubviews()
  189. configureAfterLayoutSubviews()
  190. autoRunAnimation()
  191. }
  192. // MARK: - Private
  193. fileprivate func configureInspectableProperties() {
  194. configureAnimatableProperties()
  195. configureImages()
  196. }
  197. fileprivate func configureAfterLayoutSubviews() {
  198. configureCornerRadius()
  199. configureBorder()
  200. }
  201. }