|
| 1 | +// |
| 2 | +// YoshikoTextField.swift |
| 3 | +// TextFieldEffects |
| 4 | +// |
| 5 | +// Created by Keenan Cassidy on 01/10/2015. |
| 6 | +// Copyright © 2015 Raul Riera. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import UIKit |
| 10 | + |
| 11 | +@IBDesignable public class YoshikoTextField: TextFieldEffects { |
| 12 | + |
| 13 | + private let borderLayer = CALayer() |
| 14 | + private let textFieldInsets = CGPoint(x: 6, y: 0) |
| 15 | + private let placeHolderInsets = CGPoint(x: 6, y: 0) |
| 16 | + private let outerBackgroundView = UIView() |
| 17 | + |
| 18 | + @IBInspectable public var borderSize: CGFloat = 2.0 { |
| 19 | + didSet { |
| 20 | + updateBorder() |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + @IBInspectable dynamic public var activeColor: UIColor = .blueColor() { |
| 25 | + didSet { |
| 26 | + updateBorder() |
| 27 | + updateBackground() |
| 28 | + updatePlaceholder() |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + @IBInspectable dynamic public var inActiveColor: UIColor = .lightGrayColor() { |
| 33 | + didSet { |
| 34 | + updateBorder() |
| 35 | + updateBackground() |
| 36 | + updatePlaceholder() |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + @IBInspectable dynamic public var outerBackgroundColor: UIColor = .lightGrayColor() { |
| 41 | + didSet { |
| 42 | + updateBorder() |
| 43 | + updateBackground() |
| 44 | + updatePlaceholder() |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + @IBInspectable dynamic public var activeInnerBackgroundColor: UIColor = .clearColor() { |
| 49 | + didSet { |
| 50 | + updateBorder() |
| 51 | + updateBackground() |
| 52 | + updatePlaceholder() |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + @IBInspectable dynamic public var placeholderColor: UIColor = .darkGrayColor() { |
| 57 | + didSet { |
| 58 | + updateBorder() |
| 59 | + updateBackground() |
| 60 | + updatePlaceholder() |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + override public var placeholder: String? { |
| 65 | + didSet { |
| 66 | + updatePlaceholder() |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + // MARK: Private |
| 71 | + |
| 72 | + private func updateBorder() { |
| 73 | + borderLayer.frame = rectForBounds(bounds) |
| 74 | + borderLayer.borderWidth = borderSize |
| 75 | + borderLayer.borderColor = (isFirstResponder() || text!.isNotEmpty) ? activeColor.CGColor : inActiveColor.CGColor |
| 76 | + } |
| 77 | + |
| 78 | + private func updateBackground() { |
| 79 | + outerBackgroundView.frame = CGRect(origin: bounds.origin, size: CGSize(width: bounds.width, height: placeholderHeight)) |
| 80 | + outerBackgroundView.backgroundColor = outerBackgroundColor |
| 81 | + |
| 82 | + if !subviews.contains(outerBackgroundView) { |
| 83 | + addSubview(outerBackgroundView) |
| 84 | + } |
| 85 | + |
| 86 | + if isFirstResponder() || text!.isNotEmpty { |
| 87 | + backgroundColor = activeInnerBackgroundColor |
| 88 | + } else { |
| 89 | + backgroundColor = inActiveColor |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private func updatePlaceholder() { |
| 94 | + placeholderLabel.frame = placeholderRectForBounds(bounds) |
| 95 | + placeholderLabel.text = placeholder |
| 96 | +// placeholderLabel.font = placeholderFontFromFontWithPercentageOfSize(font!, percentageOfSize: 0.7) |
| 97 | + placeholderLabel.textAlignment = textAlignment |
| 98 | + |
| 99 | + if isFirstResponder() || text!.isNotEmpty { |
| 100 | + placeholderLabel.textColor = activeColor |
| 101 | + } else { |
| 102 | + placeholderLabel.textColor = placeholderColor |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + private func placeholderFontFromFontWithPercentageOfSize(font: UIFont, percentageOfSize: CGFloat) -> UIFont! { |
| 107 | + let smallerFont = UIFont(name: font.fontName, size: font.pointSize * percentageOfSize) |
| 108 | + return smallerFont |
| 109 | + } |
| 110 | + |
| 111 | + private func rectForBounds(bounds: CGRect) -> CGRect { |
| 112 | + return CGRect(x: bounds.origin.x, y: bounds.origin.y + placeholderHeight, width: bounds.size.width, height: bounds.size.height - placeholderHeight) |
| 113 | + } |
| 114 | + |
| 115 | + private var placeholderHeight : CGFloat { |
| 116 | + return placeHolderInsets.y + placeholderFontFromFontWithPercentageOfSize(font!, percentageOfSize: 0.7).lineHeight; |
| 117 | + } |
| 118 | + |
| 119 | + // MARK: - Overrides |
| 120 | + |
| 121 | + override public var bounds: CGRect { |
| 122 | + didSet { |
| 123 | + updateBorder() |
| 124 | + updatePlaceholder() |
| 125 | + updateBackground() |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + override public func drawViewsForRect(rect: CGRect) { |
| 130 | + updatePlaceholder() |
| 131 | + updateBorder() |
| 132 | + updateBackground() |
| 133 | + |
| 134 | + addSubview(placeholderLabel) |
| 135 | + layer.addSublayer(borderLayer) |
| 136 | + } |
| 137 | + |
| 138 | + override public func animateViewsForTextEntry() { |
| 139 | +// placeholderLabel.font = placeholderFontFromFontWithPercentageOfSize(font!, percentageOfSize: 0.5) |
| 140 | + UIView.animateWithDuration(0.3) { () -> Void in |
| 141 | + |
| 142 | + self.placeholderLabel.font = self.placeholderFontFromFontWithPercentageOfSize(self.font!, percentageOfSize: 0.5) |
| 143 | + self.updatePlaceholder() |
| 144 | + self.updateBorder() |
| 145 | + self.updateBackground() |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + override public func animateViewsForTextDisplay() { |
| 150 | +// placeholderLabel.font = placeholderFontFromFontWithPercentageOfSize(font!, percentageOfSize: 0.7) |
| 151 | + UIView.animateWithDuration(0.3) { () -> Void in |
| 152 | + self.placeholderLabel.font = self.placeholderFontFromFontWithPercentageOfSize(self.font!, percentageOfSize: 0.7) |
| 153 | + self.updatePlaceholder() |
| 154 | + self.updateBorder() |
| 155 | + self.updateBackground() |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + // MARK: - TextFieldEffects Overrides |
| 160 | + |
| 161 | + /*public override func placeholderRectForBounds(bounds: CGRect) -> CGRect { |
| 162 | + if isFirstResponder() || text!.isNotEmpty { |
| 163 | + return CGRectMake(placeHolderInsets.x, placeHolderInsets.y, bounds.width, placeholderHeight) |
| 164 | + } else { |
| 165 | + return textRectForBounds(bounds) |
| 166 | + } |
| 167 | + }*/ |
| 168 | + |
| 169 | + public override func editingRectForBounds(bounds: CGRect) -> CGRect { |
| 170 | + return textRectForBounds(bounds) |
| 171 | + } |
| 172 | + |
| 173 | + override public func textRectForBounds(bounds: CGRect) -> CGRect { |
| 174 | + return CGRectOffset(bounds, textFieldInsets.x, textFieldInsets.y + placeholderHeight/2) |
| 175 | + } |
| 176 | +} |
0 commit comments