|
| 1 | +// |
| 2 | +// MinoruTextField.swift |
| 3 | +// TextFieldEffects |
| 4 | +// |
| 5 | +// Created by Raúl Riera on 27/01/2015. |
| 6 | +// Copyright (c) 2015 Raul Riera. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import UIKit |
| 10 | + |
| 11 | +@IBDesignable class MinoruTextField: UITextField, UITextFieldDelegate { |
| 12 | + |
| 13 | + @IBInspectable var placeholderColor: UIColor = UIColor(red: 106, green: 121, blue: 137, alpha: 1) { |
| 14 | + didSet { |
| 15 | + updatePlaceholder() |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + override var backgroundColor: UIColor? { |
| 20 | + set { |
| 21 | + backgroundLayerColor = newValue! |
| 22 | + } |
| 23 | + get { |
| 24 | + return backgroundLayerColor |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + override var placeholder: String? { |
| 29 | + didSet { |
| 30 | + updatePlaceholder() |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + override var bounds: CGRect { |
| 35 | + didSet { |
| 36 | + updateBorder() |
| 37 | + updatePlaceholder() |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + private let borderThickness: CGFloat = 1 |
| 42 | + private let placeholderLabel = UILabel() |
| 43 | + private let placeholderInsets = CGPoint(x: 6, y: 6) |
| 44 | + private let textFieldInsets = CGPoint(x: 6, y: 6) |
| 45 | + private let borderLayer = CALayer() |
| 46 | + private var backgroundLayerColor: UIColor? |
| 47 | + |
| 48 | + /** |
| 49 | + Draws all the requires view on top of the textfield |
| 50 | + |
| 51 | + :param: rect to based the views from |
| 52 | + */ |
| 53 | + private func drawViewsForRect(rect: CGRect) { |
| 54 | + let frame = CGRect(origin: CGPointZero, size: CGSize(width: rect.size.width, height: rect.size.height)) |
| 55 | + |
| 56 | + placeholderLabel.frame = CGRectInset(frame, placeholderInsets.x, placeholderInsets.y) |
| 57 | + placeholderLabel.font = placeholderFontFromFont(self.font) |
| 58 | + |
| 59 | + updateBorder() |
| 60 | + updatePlaceholder() |
| 61 | + |
| 62 | + layer.addSublayer(borderLayer) |
| 63 | + addSubview(placeholderLabel) |
| 64 | + |
| 65 | + delegate = self |
| 66 | + } |
| 67 | + |
| 68 | + private func updateBorder() { |
| 69 | + borderLayer.frame = rectForBorder() |
| 70 | + borderLayer.backgroundColor = backgroundColor?.CGColor |
| 71 | + } |
| 72 | + |
| 73 | + private func updatePlaceholder() { |
| 74 | + placeholderLabel.text = placeholder |
| 75 | + placeholderLabel.textColor = placeholderColor |
| 76 | + placeholderLabel.sizeToFit() |
| 77 | + layoutPlaceholderInTextRect() |
| 78 | + |
| 79 | + if isFirstResponder() || !text.isEmpty { |
| 80 | + animateViewsForTextEntry() |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private func placeholderFontFromFont(font: UIFont) -> UIFont! { |
| 85 | + let smallerFont = UIFont(name: font.fontName, size: font.pointSize * 0.65) |
| 86 | + return smallerFont |
| 87 | + } |
| 88 | + |
| 89 | + private func rectForBorder() -> CGRect { |
| 90 | + var newRect = CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height - 15) |
| 91 | + |
| 92 | + return CGRectInset(newRect, textFieldInsets.x, textFieldInsets.y) |
| 93 | + } |
| 94 | + |
| 95 | + private func layoutPlaceholderInTextRect() { |
| 96 | + |
| 97 | + if !text.isEmpty { |
| 98 | + return |
| 99 | + } |
| 100 | + |
| 101 | + let textRect = textRectForBounds(bounds) |
| 102 | + var originX = textRect.origin.x |
| 103 | + switch self.textAlignment { |
| 104 | + case .Center: |
| 105 | + originX += textRect.size.width/2 - placeholderLabel.bounds.width/2 |
| 106 | + case .Right: |
| 107 | + originX += textRect.size.width - placeholderLabel.bounds.width |
| 108 | + default: |
| 109 | + break |
| 110 | + } |
| 111 | + placeholderLabel.frame = CGRect(x: originX, y: bounds.height - placeholderLabel.frame.height, |
| 112 | + width: placeholderLabel.frame.size.width, height: placeholderLabel.frame.size.height) |
| 113 | + } |
| 114 | + |
| 115 | + private func animateViewsForTextEntry() { |
| 116 | + borderLayer.borderColor = textColor?.CGColor |
| 117 | + borderLayer.shadowOffset = CGSizeZero |
| 118 | + borderLayer.borderWidth = borderThickness |
| 119 | + borderLayer.shadowColor = textColor?.CGColor |
| 120 | + borderLayer.shadowOpacity = 0.5 |
| 121 | + borderLayer.shadowRadius = 1 |
| 122 | + } |
| 123 | + |
| 124 | + private func animateViewsForTextDisplay() { |
| 125 | + borderLayer.borderColor = nil |
| 126 | + borderLayer.shadowOffset = CGSizeZero |
| 127 | + borderLayer.borderWidth = 0 |
| 128 | + borderLayer.shadowColor = nil |
| 129 | + borderLayer.shadowOpacity = 0 |
| 130 | + borderLayer.shadowRadius = 0 |
| 131 | + } |
| 132 | + |
| 133 | + // MARK: - Overrides |
| 134 | + |
| 135 | + override func drawRect(rect: CGRect) { |
| 136 | + drawViewsForRect(rect) |
| 137 | + } |
| 138 | + |
| 139 | + override func drawPlaceholderInRect(rect: CGRect) { |
| 140 | + // Don't draw any placeholders |
| 141 | + } |
| 142 | + |
| 143 | + override func editingRectForBounds(bounds: CGRect) -> CGRect { |
| 144 | + let newBounds = CGRect(origin: bounds.origin, size: CGSize(width: bounds.size.width, height: bounds.size.height - placeholderLabel.frame.size.height)) |
| 145 | + return CGRectInset(newBounds, textFieldInsets.x * 2, 0) |
| 146 | + } |
| 147 | + |
| 148 | + override func textRectForBounds(bounds: CGRect) -> CGRect { |
| 149 | + let newBounds = CGRect(origin: bounds.origin, size: CGSize(width: bounds.size.width, height: bounds.size.height - placeholderLabel.frame.size.height)) |
| 150 | + |
| 151 | + return CGRectInset(newBounds, textFieldInsets.x * 2, 0) |
| 152 | + } |
| 153 | + |
| 154 | + override func prepareForInterfaceBuilder() { |
| 155 | + drawViewsForRect(frame) |
| 156 | + } |
| 157 | + |
| 158 | + // MARK: - UITextFieldDelegate |
| 159 | + |
| 160 | + func textFieldDidBeginEditing(textField: UITextField) { |
| 161 | + animateViewsForTextEntry() |
| 162 | + } |
| 163 | + |
| 164 | + func textFieldDidEndEditing(textField: UITextField) { |
| 165 | + animateViewsForTextDisplay() |
| 166 | + } |
| 167 | +} |
0 commit comments