Skip to content

Commit f9ecbf6

Browse files
committed
Minoru support
1 parent 8f05279 commit f9ecbf6

File tree

5 files changed

+236
-2
lines changed

5 files changed

+236
-2
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Currently it features the following effects from the article:
88
- [x] Hoshi
99
- [x] Jiro
1010
- [ ] Isao **(in development)**
11-
- [ ] Minoru **(in development)**
11+
- [x] Minoru
1212

1313
## How they look
1414

@@ -21,6 +21,9 @@ Currently it features the following effects from the article:
2121
### Jiro
2222
<img src="https://raw.githubusercontent.com/raulriera/TextFieldEffects/master/Screenshots/Jiro.gif" />
2323

24+
### Minoru
25+
<img src="https://raw.githubusercontent.com/raulriera/TextFieldEffects/master/Screenshots/Minoru.gif" />
26+
2427
## Installation
2528

2629
The easiest way to install this framework is via [Carthage](https://github.com/Carthage/Carthage)

Screenshots/Minoru.gif

26.1 KB
Loading

TextFieldEffects/TextFieldEffects.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
4C25E5C61A7845CA0008379F /* MinoruTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C25E5C51A7845CA0008379F /* MinoruTextField.swift */; };
1011
4CBF76511A71AE4500073B6A /* TextFieldEffects.h in Headers */ = {isa = PBXBuildFile; fileRef = 4CBF76501A71AE4500073B6A /* TextFieldEffects.h */; settings = {ATTRIBUTES = (Public, ); }; };
1112
4CBF76681A71AE6700073B6A /* KaedeTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CBF76671A71AE6700073B6A /* KaedeTextField.swift */; };
1213
4CBF76721A71AF7700073B6A /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CBF76711A71AF7700073B6A /* AppDelegate.swift */; };
@@ -45,6 +46,7 @@
4546
/* End PBXCopyFilesBuildPhase section */
4647

4748
/* Begin PBXFileReference section */
49+
4C25E5C51A7845CA0008379F /* MinoruTextField.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MinoruTextField.swift; sourceTree = "<group>"; };
4850
4CBF764B1A71AE4500073B6A /* TextFieldEffects.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = TextFieldEffects.framework; sourceTree = BUILT_PRODUCTS_DIR; };
4951
4CBF764F1A71AE4500073B6A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
5052
4CBF76501A71AE4500073B6A /* TextFieldEffects.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TextFieldEffects.h; sourceTree = "<group>"; };
@@ -103,6 +105,7 @@
103105
4CBF76501A71AE4500073B6A /* TextFieldEffects.h */,
104106
4CE365851A73C59B0021A842 /* TextFieldsEffects.swift */,
105107
4CBF76671A71AE6700073B6A /* KaedeTextField.swift */,
108+
4C25E5C51A7845CA0008379F /* MinoruTextField.swift */,
106109
4CE365831A7327CD0021A842 /* HoshiTextField.swift */,
107110
4CE3658C1A7464D80021A842 /* JiroTextField.swift */,
108111
4CBF764E1A71AE4500073B6A /* Supporting Files */,
@@ -252,6 +255,7 @@
252255
files = (
253256
4CE365841A7327CD0021A842 /* HoshiTextField.swift in Sources */,
254257
4CE365861A73C59B0021A842 /* TextFieldsEffects.swift in Sources */,
258+
4C25E5C61A7845CA0008379F /* MinoruTextField.swift in Sources */,
255259
4CE3658D1A7464D80021A842 /* JiroTextField.swift in Sources */,
256260
4CBF76681A71AE6700073B6A /* KaedeTextField.swift in Sources */,
257261
);
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
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+
}

TextFieldEffects/TextFieldsDemo/Base.lproj/Main.storyboard

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6724" systemVersion="14B25" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="kzy-mN-NiP">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6724" systemVersion="14C109" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="kzy-mN-NiP">
33
<dependencies>
44
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6711"/>
55
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
@@ -210,6 +210,66 @@
210210
</tableViewCell>
211211
</cells>
212212
</tableViewSection>
213+
<tableViewSection headerTitle="Minoru" id="r7g-8X-gCL" userLabel="Minoru">
214+
<cells>
215+
<tableViewCell contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" id="dmM-Gc-ifx">
216+
<autoresizingMask key="autoresizingMask"/>
217+
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="dmM-Gc-ifx" id="xq7-nD-P5i">
218+
<autoresizingMask key="autoresizingMask"/>
219+
<subviews>
220+
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" placeholder="First name" adjustsFontSizeToFit="NO" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="7wa-S2-hoJ" customClass="MinoruTextField" customModule="TextFieldEffects">
221+
<rect key="frame" x="8" y="0.0" width="584" height="66"/>
222+
<color key="backgroundColor" name="alternateSelectedControlTextColor" catalog="System" colorSpace="catalog"/>
223+
<color key="tintColor" name="alternateSelectedControlTextColor" catalog="System" colorSpace="catalog"/>
224+
<color key="textColor" red="0.40400000000000003" green="0.58799999999999997" blue="0.012" alpha="1" colorSpace="calibratedRGB"/>
225+
<fontDescription key="fontDescription" name="AvenirNext-DemiBold" family="Avenir Next" pointSize="20"/>
226+
<textInputTraits key="textInputTraits"/>
227+
<userDefinedRuntimeAttributes>
228+
<userDefinedRuntimeAttribute type="color" keyPath="placeholderColor">
229+
<color key="value" red="0.41568627450000001" green="0.47450980390000003" blue="0.53725490200000003" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
230+
</userDefinedRuntimeAttribute>
231+
</userDefinedRuntimeAttributes>
232+
</textField>
233+
</subviews>
234+
<color key="backgroundColor" cocoaTouchSystemColor="groupTableViewBackgroundColor"/>
235+
<constraints>
236+
<constraint firstAttribute="bottomMargin" secondItem="7wa-S2-hoJ" secondAttribute="bottom" constant="-8" id="c5U-Yr-lSU"/>
237+
<constraint firstItem="7wa-S2-hoJ" firstAttribute="top" secondItem="xq7-nD-P5i" secondAttribute="topMargin" constant="-8" id="jsa-vg-mu5"/>
238+
<constraint firstItem="7wa-S2-hoJ" firstAttribute="leading" secondItem="xq7-nD-P5i" secondAttribute="leadingMargin" id="wXz-E7-5uP"/>
239+
<constraint firstAttribute="trailingMargin" secondItem="7wa-S2-hoJ" secondAttribute="trailing" id="xcl-QV-oRN"/>
240+
</constraints>
241+
</tableViewCellContentView>
242+
</tableViewCell>
243+
<tableViewCell contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" id="QDk-yb-Wrx">
244+
<autoresizingMask key="autoresizingMask"/>
245+
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="QDk-yb-Wrx" id="eXe-jG-StF">
246+
<autoresizingMask key="autoresizingMask"/>
247+
<subviews>
248+
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" placeholder="Last name" adjustsFontSizeToFit="NO" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="3Eb-3h-6i8" customClass="MinoruTextField" customModule="TextFieldEffects">
249+
<rect key="frame" x="8" y="0.0" width="584" height="66"/>
250+
<color key="backgroundColor" name="alternateSelectedControlTextColor" catalog="System" colorSpace="catalog"/>
251+
<color key="tintColor" name="alternateSelectedControlTextColor" catalog="System" colorSpace="catalog"/>
252+
<color key="textColor" red="0.40400000000000003" green="0.58799999999999997" blue="0.012" alpha="1" colorSpace="calibratedRGB"/>
253+
<fontDescription key="fontDescription" name="AvenirNext-DemiBold" family="Avenir Next" pointSize="20"/>
254+
<textInputTraits key="textInputTraits"/>
255+
<userDefinedRuntimeAttributes>
256+
<userDefinedRuntimeAttribute type="color" keyPath="placeholderColor">
257+
<color key="value" red="0.41568627450000001" green="0.47450980390000003" blue="0.53725490200000003" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
258+
</userDefinedRuntimeAttribute>
259+
</userDefinedRuntimeAttributes>
260+
</textField>
261+
</subviews>
262+
<color key="backgroundColor" cocoaTouchSystemColor="groupTableViewBackgroundColor"/>
263+
<constraints>
264+
<constraint firstAttribute="trailingMargin" secondItem="3Eb-3h-6i8" secondAttribute="trailing" id="4sW-31-Yhl"/>
265+
<constraint firstAttribute="bottomMargin" secondItem="3Eb-3h-6i8" secondAttribute="bottom" constant="-8" id="D5U-kB-zhu"/>
266+
<constraint firstItem="3Eb-3h-6i8" firstAttribute="top" secondItem="eXe-jG-StF" secondAttribute="topMargin" constant="-8" id="IlR-SY-Ngb"/>
267+
<constraint firstItem="3Eb-3h-6i8" firstAttribute="leading" secondItem="eXe-jG-StF" secondAttribute="leadingMargin" id="y7T-ei-tNy"/>
268+
</constraints>
269+
</tableViewCellContentView>
270+
</tableViewCell>
271+
</cells>
272+
</tableViewSection>
213273
</sections>
214274
<connections>
215275
<outlet property="dataSource" destination="kzy-mN-NiP" id="QcT-K3-9yn"/>

0 commit comments

Comments
 (0)