Skip to content

Commit 2935c03

Browse files
author
Keenan.Cassidy
committed
Added Yoshiko, need to improve the animation
1 parent fda7428 commit 2935c03

File tree

3 files changed

+273
-43
lines changed

3 files changed

+273
-43
lines changed

TextFieldEffects/TextFieldEffects.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
4CBF76771A71AF7700073B6A /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 4CBF76751A71AF7700073B6A /* Main.storyboard */; };
2525
4CBF76791A71AF7700073B6A /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 4CBF76781A71AF7700073B6A /* Images.xcassets */; };
2626
4CBF767C1A71AF7700073B6A /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = 4CBF767A1A71AF7700073B6A /* LaunchScreen.xib */; };
27+
DF983ABB1BBD1F4100BCC4C7 /* YoshikoTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = DF983ABA1BBD1F4100BCC4C7 /* YoshikoTextField.swift */; settings = {ASSET_TAGS = (); }; };
2728
/* End PBXBuildFile section */
2829

2930
/* Begin PBXContainerItemProxy section */
@@ -70,6 +71,7 @@
7071
4CBF76761A71AF7700073B6A /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
7172
4CBF76781A71AF7700073B6A /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = "<group>"; };
7273
4CBF767B1A71AF7700073B6A /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/LaunchScreen.xib; sourceTree = "<group>"; };
74+
DF983ABA1BBD1F4100BCC4C7 /* YoshikoTextField.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = YoshikoTextField.swift; sourceTree = "<group>"; };
7375
/* End PBXFileReference section */
7476

7577
/* Begin PBXFrameworksBuildPhase section */
@@ -103,6 +105,7 @@
103105
4CB3B6EC1AA8DEC3000AFA56 /* MinoruTextField.swift */,
104106
4CB3B6ED1AA8DEC3000AFA56 /* TextFieldsEffects.swift */,
105107
4CB3B6EE1AA8DEC3000AFA56 /* YokoTextField.swift */,
108+
DF983ABA1BBD1F4100BCC4C7 /* YoshikoTextField.swift */,
106109
4CB3B6D21AA8DEA7000AFA56 /* TextFieldEffects.h */,
107110
4CB3B6D01AA8DEA7000AFA56 /* Supporting Files */,
108111
);
@@ -269,6 +272,7 @@
269272
isa = PBXSourcesBuildPhase;
270273
buildActionMask = 2147483647;
271274
files = (
275+
DF983ABB1BBD1F4100BCC4C7 /* YoshikoTextField.swift in Sources */,
272276
4CB3B6F31AA8DEC3000AFA56 /* MadokaTextField.swift in Sources */,
273277
4CB3B6F61AA8DEC3000AFA56 /* YokoTextField.swift in Sources */,
274278
4CB3B6EF1AA8DEC3000AFA56 /* HoshiTextField.swift in Sources */,
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
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

Comments
 (0)