AnimatableTextView.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 AnimatableTextView: UITextView, CornerDesignable, FillDesignable, BorderDesignable, Animatable, PlaceholderDesignable {
  8. // MARK: - CornerDesignable
  9. @IBInspectable open var cornerRadius: CGFloat = CGFloat.nan {
  10. didSet {
  11. configureCornerRadius()
  12. }
  13. }
  14. open var cornerSides: CornerSides = .allSides {
  15. didSet {
  16. configureCornerRadius()
  17. }
  18. }
  19. @IBInspectable var _cornerSides: String? {
  20. didSet {
  21. cornerSides = CornerSides(rawValue: _cornerSides)
  22. }
  23. }
  24. // MARK: - FillDesignable
  25. @IBInspectable open var fillColor: UIColor? {
  26. didSet {
  27. configureFillColor()
  28. }
  29. }
  30. open var predefinedColor: ColorType? {
  31. didSet {
  32. configureFillColor()
  33. }
  34. }
  35. @IBInspectable var _predefinedColor: String? {
  36. didSet {
  37. predefinedColor = ColorType(string: _predefinedColor)
  38. }
  39. }
  40. @IBInspectable open var opacity: CGFloat = CGFloat.nan {
  41. didSet {
  42. configureOpacity()
  43. }
  44. }
  45. // MARK: - BorderDesignable
  46. open var borderType: BorderType = .solid {
  47. didSet {
  48. configureBorder()
  49. }
  50. }
  51. @IBInspectable var _borderType: String? {
  52. didSet {
  53. borderType = BorderType(string: _borderType)
  54. }
  55. }
  56. @IBInspectable open var borderColor: UIColor? {
  57. didSet {
  58. configureBorder()
  59. }
  60. }
  61. @IBInspectable open var borderWidth: CGFloat = CGFloat.nan {
  62. didSet {
  63. configureBorder()
  64. }
  65. }
  66. open var borderSides: BorderSides = .AllSides {
  67. didSet {
  68. configureBorder()
  69. }
  70. }
  71. @IBInspectable var _borderSides: String? {
  72. didSet {
  73. borderSides = BorderSides(rawValue: _borderSides)
  74. }
  75. }
  76. // MARK: - PlaceholderDesignable
  77. @IBInspectable open var placeholderText: String? {
  78. didSet {
  79. placeholderLabel.text = placeholderText
  80. }
  81. }
  82. @IBInspectable open var placeholderColor: UIColor? {
  83. didSet {
  84. placeholderLabel.textColor = placeholderColor
  85. }
  86. }
  87. // MARK: - Animatable
  88. open var animationType: AnimationType = .none
  89. @IBInspectable var _animationType: String? {
  90. didSet {
  91. animationType = AnimationType(string: _animationType)
  92. }
  93. }
  94. @IBInspectable open var autoRun: Bool = true
  95. @IBInspectable open var duration: Double = Double.nan
  96. @IBInspectable open var delay: Double = Double.nan
  97. @IBInspectable open var damping: CGFloat = CGFloat.nan
  98. @IBInspectable open var velocity: CGFloat = CGFloat.nan
  99. @IBInspectable open var force: CGFloat = CGFloat.nan
  100. @IBInspectable var _timingFunction: String = "" {
  101. didSet {
  102. timingFunction = TimingFunctionType(string: _timingFunction)
  103. }
  104. }
  105. open var timingFunction: TimingFunctionType = .none
  106. // MARK: Override properties
  107. override open var font: UIFont! {
  108. didSet {
  109. placeholderLabel.font = font
  110. }
  111. }
  112. override open var textAlignment: NSTextAlignment {
  113. didSet {
  114. placeholderLabel.textAlignment = textAlignment
  115. }
  116. }
  117. open override var text: String! {
  118. didSet {
  119. textDidChange()
  120. }
  121. }
  122. override open var attributedText: NSAttributedString! {
  123. didSet {
  124. textDidChange()
  125. }
  126. }
  127. override open var textContainerInset: UIEdgeInsets {
  128. didSet {
  129. update(placeholderLabel, using: &placeholderLabelConstraints)
  130. }
  131. }
  132. // MARK: Private properties
  133. fileprivate let placeholderLabel: UILabel = UILabel()
  134. fileprivate var placeholderLabelConstraints = [NSLayoutConstraint]()
  135. // MARK: - Lifecycle
  136. open override func prepareForInterfaceBuilder() {
  137. super.prepareForInterfaceBuilder()
  138. configureInspectableProperties()
  139. }
  140. open override func awakeFromNib() {
  141. super.awakeFromNib()
  142. configureInspectableProperties()
  143. }
  144. open override func layoutSubviews() {
  145. super.layoutSubviews()
  146. configureAfterLayoutSubviews()
  147. autoRunAnimation()
  148. }
  149. deinit {
  150. NotificationCenter.default.removeObserver(self, name: UITextView.textDidChangeNotification, object: nil)
  151. }
  152. // MARK: - Private
  153. private func configureInspectableProperties() {
  154. configureAnimatableProperties()
  155. configure(placeholderLabel: placeholderLabel, placeholderLabelConstraints: &placeholderLabelConstraints)
  156. NotificationCenter.default.addObserver(self, selector: #selector(textDidChange), name: UITextView.textDidChangeNotification, object: nil)
  157. }
  158. @objc
  159. private func textDidChange() {
  160. placeholderLabel.isHidden = !text.isEmpty
  161. }
  162. private func configureAfterLayoutSubviews() {
  163. configureCornerRadius()
  164. configureBorder()
  165. placeholderLabel.preferredMaxLayoutWidth = textContainer.size.width - textContainer.lineFragmentPadding * 2.0
  166. }
  167. }