|  | 
|  | 1 | +// | 
|  | 2 | +// TextFieldEffects.swift | 
|  | 3 | +// TextFieldEffects | 
|  | 4 | +// | 
|  | 5 | +// Created by Raúl Riera on 24/01/2015. | 
|  | 6 | +// Copyright (c) 2015 Raul Riera. All rights reserved. | 
|  | 7 | +// | 
|  | 8 | + | 
|  | 9 | +import UIKit | 
|  | 10 | + | 
|  | 11 | +extension String { | 
|  | 12 | + /** | 
|  | 13 | + true if self contains characters. | 
|  | 14 | + */ | 
|  | 15 | + public var isNotEmpty: Bool { | 
|  | 16 | + return !isEmpty | 
|  | 17 | + } | 
|  | 18 | +} | 
|  | 19 | + | 
|  | 20 | +/** | 
|  | 21 | +A TextFieldEffects object is a control that displays editable text and contains the boilerplates to setup unique animations for text entry and display. You typically use this class the same way you use UITextField. | 
|  | 22 | +*/ | 
|  | 23 | +open class TextFieldEffects : UITextField { | 
|  | 24 | +  | 
|  | 25 | + /** | 
|  | 26 | + The type of animation a TextFieldEffect can perform. | 
|  | 27 | +  | 
|  | 28 | + - TextEntry: animation that takes effect when the textfield has focus. | 
|  | 29 | + - TextDisplay: animation that takes effect when the textfield loses focus. | 
|  | 30 | + */ | 
|  | 31 | + public enum AnimationType: Int { | 
|  | 32 | + case textEntry | 
|  | 33 | + case textDisplay | 
|  | 34 | + } | 
|  | 35 | +  | 
|  | 36 | + /** | 
|  | 37 | + Closure executed when an animation has been completed. | 
|  | 38 | + */ | 
|  | 39 | + public typealias AnimationCompletionHandler = (_ type: AnimationType)->() | 
|  | 40 | +  | 
|  | 41 | + /** | 
|  | 42 | + UILabel that holds all the placeholder information | 
|  | 43 | + */ | 
|  | 44 | + open let placeholderLabel = UILabel() | 
|  | 45 | +  | 
|  | 46 | + /** | 
|  | 47 | + Creates all the animations that are used to leave the textfield in the "entering text" state. | 
|  | 48 | + */ | 
|  | 49 | + open func animateViewsForTextEntry() { | 
|  | 50 | + fatalError("\(#function) must be overridden") | 
|  | 51 | + } | 
|  | 52 | +  | 
|  | 53 | + /** | 
|  | 54 | + Creates all the animations that are used to leave the textfield in the "display input text" state. | 
|  | 55 | + */ | 
|  | 56 | + open func animateViewsForTextDisplay() { | 
|  | 57 | + fatalError("\(#function) must be overridden") | 
|  | 58 | + } | 
|  | 59 | +  | 
|  | 60 | + /** | 
|  | 61 | + The animation completion handler is the best place to be notified when the text field animation has ended. | 
|  | 62 | + */ | 
|  | 63 | + open var animationCompletionHandler: AnimationCompletionHandler? | 
|  | 64 | +  | 
|  | 65 | + /** | 
|  | 66 | + Draws the receiver’s image within the passed-in rectangle. | 
|  | 67 | +  | 
|  | 68 | + - parameter rect:	The portion of the view’s bounds that needs to be updated. | 
|  | 69 | + */ | 
|  | 70 | + open func drawViewsForRect(_ rect: CGRect) { | 
|  | 71 | + fatalError("\(#function) must be overridden") | 
|  | 72 | + } | 
|  | 73 | +  | 
|  | 74 | + open func updateViewsForBoundsChange(_ bounds: CGRect) { | 
|  | 75 | + fatalError("\(#function) must be overridden") | 
|  | 76 | + } | 
|  | 77 | +  | 
|  | 78 | + // MARK: - Overrides | 
|  | 79 | +  | 
|  | 80 | + override open func draw(_ rect: CGRect) { | 
|  | 81 | + drawViewsForRect(rect) | 
|  | 82 | + } | 
|  | 83 | +  | 
|  | 84 | + override open func drawPlaceholder(in rect: CGRect) { | 
|  | 85 | + // Don't draw any placeholders | 
|  | 86 | + } | 
|  | 87 | +  | 
|  | 88 | + override open var text: String? { | 
|  | 89 | + didSet { | 
|  | 90 | + if let text = text, text.isNotEmpty { | 
|  | 91 | + animateViewsForTextEntry() | 
|  | 92 | + } else { | 
|  | 93 | + animateViewsForTextDisplay() | 
|  | 94 | + } | 
|  | 95 | + } | 
|  | 96 | + } | 
|  | 97 | +  | 
|  | 98 | + // MARK: - UITextField Observing | 
|  | 99 | +  | 
|  | 100 | + override open func willMove(toSuperview newSuperview: UIView!) { | 
|  | 101 | + if newSuperview != nil { | 
|  | 102 | + NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidEndEditing), name: NSNotification.Name.UITextFieldTextDidEndEditing, object: self) | 
|  | 103 | +  | 
|  | 104 | + NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidBeginEditing), name: NSNotification.Name.UITextFieldTextDidBeginEditing, object: self) | 
|  | 105 | + } else { | 
|  | 106 | + NotificationCenter.default.removeObserver(self) | 
|  | 107 | + } | 
|  | 108 | + } | 
|  | 109 | +  | 
|  | 110 | + /** | 
|  | 111 | + The textfield has started an editing session. | 
|  | 112 | + */ | 
|  | 113 | + open func textFieldDidBeginEditing() { | 
|  | 114 | + animateViewsForTextEntry() | 
|  | 115 | + } | 
|  | 116 | +  | 
|  | 117 | + /** | 
|  | 118 | + The textfield has ended an editing session. | 
|  | 119 | + */ | 
|  | 120 | + open func textFieldDidEndEditing() { | 
|  | 121 | + animateViewsForTextDisplay() | 
|  | 122 | + } | 
|  | 123 | +  | 
|  | 124 | + // MARK: - Interface Builder | 
|  | 125 | +  | 
|  | 126 | + override open func prepareForInterfaceBuilder() { | 
|  | 127 | + drawViewsForRect(frame) | 
|  | 128 | + } | 
|  | 129 | +} | 
0 commit comments