Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
Material
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Dmitriy Stepanets
Material
Commits
d3650f18
Commit
d3650f18
authored
Apr 30, 2016
by
Daniel Dahan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added color matching when updating tintColor
parent
cb996727
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
309 additions
and
1008 deletions
+309
-1008
Examples/Programmatic/TextField/TextField/ViewController.swift
+9
-6
Material.xcodeproj/project.pbxproj
+0
-4
Sources/iOS/MTextField.swift
+0
-612
Sources/iOS/TextField.swift
+300
-386
No files found.
Examples/Programmatic/TextField/TextField/ViewController.swift
View file @
d3650f18
...
...
@@ -39,9 +39,9 @@ import UIKit
import
Material
class
ViewController
:
UIViewController
,
TextFieldDelegate
{
private
var
nameField
:
M
TextField
!
private
var
emailField
:
M
TextField
!
private
var
passwordField
:
M
TextField
!
private
var
nameField
:
TextField
!
private
var
emailField
:
TextField
!
private
var
passwordField
:
TextField
!
override
func
viewDidLoad
()
{
super
.
viewDidLoad
()
...
...
@@ -85,7 +85,7 @@ class ViewController: UIViewController, TextFieldDelegate {
/// Prepares the name TextField.
private
func
prepareNameField
()
{
nameField
=
M
TextField
()
nameField
=
TextField
()
nameField
.
text
=
"Daniel Dahan"
nameField
.
placeholder
=
"Name"
nameField
.
detail
=
"Your given name"
...
...
@@ -106,7 +106,7 @@ class ViewController: UIViewController, TextFieldDelegate {
/// Prepares the email TextField.
private
func
prepareEmailField
()
{
emailField
=
M
TextField
(
frame
:
CGRectMake
(
40
,
120
,
view
.
bounds
.
width
-
80
,
32
))
emailField
=
TextField
(
frame
:
CGRectMake
(
40
,
120
,
view
.
bounds
.
width
-
80
,
32
))
emailField
.
placeholder
=
"Email"
emailField
.
detail
=
"Error, incorrect email"
emailField
.
enableClearFlatButton
=
true
...
...
@@ -122,13 +122,16 @@ class ViewController: UIViewController, TextFieldDelegate {
/// Prepares the password TextField.
private
func
preparePasswordField
()
{
passwordField
=
M
TextField
()
passwordField
=
TextField
()
passwordField
.
placeholder
=
"Password"
passwordField
.
detail
=
"At least 8 characters"
passwordField
.
clearButtonMode
=
.
WhileEditing
passwordField
.
enableVisibilityFlatButton
=
true
passwordField
.
delegate
=
self
// Setting the visibilityFlatButton color.
passwordField
.
visibilityFlatButton
?
.
tintColor
=
MaterialColor
.
green
.
base
.
colorWithAlphaComponent
(
passwordField
.
secureTextEntry
?
0.38
:
0.54
)
// The translatesAutoresizingMaskIntoConstraints property must be set to enable AutoLayout correctly.
passwordField
.
translatesAutoresizingMaskIntoConstraints
=
false
view
.
addSubview
(
passwordField
)
...
...
Material.xcodeproj/project.pbxproj
View file @
d3650f18
...
...
@@ -9,7 +9,6 @@
/* Begin PBXBuildFile section */
96334EF61C8B84660083986B
/* Assets.xcassets in Resources */
=
{
isa
=
PBXBuildFile
;
fileRef
=
96334EF51C8B84660083986B
/* Assets.xcassets */
;
};
963832421B88DFD80015F710
/* Material.framework in Frameworks */
=
{
isa
=
PBXBuildFile
;
fileRef
=
963832361B88DFD80015F710
/* Material.framework */
;
};
9658F1E51CD0229D00B902C1
/* MTextField.swift in Sources */
=
{
isa
=
PBXBuildFile
;
fileRef
=
9658F1E41CD0229C00B902C1
/* MTextField.swift */
;
};
9660161D1CB2ED6C00AAB661
/* Material.framework in Frameworks */
=
{
isa
=
PBXBuildFile
;
fileRef
=
966016131CB2ED6C00AAB661
/* Material.framework */
;
};
9660162A1CB2F04E00AAB661
/* Material.h in Headers */
=
{
isa
=
PBXBuildFile
;
fileRef
=
96D88C091C1328D800B91418
/* Material.h */
;
settings
=
{
ATTRIBUTES
=
(
Public
,
);
};
};
9679CEAB1CC69E080021685B
/* Material+UIImage+TintColor.swift in Sources */
=
{
isa
=
PBXBuildFile
;
fileRef
=
9679CEAA1CC69E080021685B
/* Material+UIImage+TintColor.swift */
;
};
...
...
@@ -197,7 +196,6 @@
963832361B88DFD80015F710
/* Material.framework */
=
{
isa
=
PBXFileReference
;
explicitFileType
=
wrapper.framework
;
includeInIndex
=
0
;
path
=
Material.framework
;
sourceTree
=
BUILT_PRODUCTS_DIR
;
};
963832411B88DFD80015F710
/* Material.xctest */
=
{
isa
=
PBXFileReference
;
explicitFileType
=
wrapper.cfbundle
;
includeInIndex
=
0
;
path
=
Material.xctest
;
sourceTree
=
BUILT_PRODUCTS_DIR
;
};
963832591B88E31A0015F710
/* Info.plist */
=
{
isa
=
PBXFileReference
;
fileEncoding
=
4
;
lastKnownFileType
=
text.plist.xml
;
path
=
Info.plist
;
sourceTree
=
"<group>"
;
};
9658F1E41CD0229C00B902C1
/* MTextField.swift */
=
{
isa
=
PBXFileReference
;
fileEncoding
=
4
;
lastKnownFileType
=
sourcecode.swift
;
path
=
MTextField.swift
;
sourceTree
=
"<group>"
;
};
966016131CB2ED6C00AAB661
/* Material.framework */
=
{
isa
=
PBXFileReference
;
explicitFileType
=
wrapper.framework
;
includeInIndex
=
0
;
path
=
Material.framework
;
sourceTree
=
BUILT_PRODUCTS_DIR
;
};
9660161C1CB2ED6C00AAB661
/* Material OSX Tests.xctest */
=
{
isa
=
PBXFileReference
;
explicitFileType
=
wrapper.cfbundle
;
includeInIndex
=
0
;
path
=
"Material OSX Tests.xctest"
;
sourceTree
=
BUILT_PRODUCTS_DIR
;
};
967887881C9777CB0037F6C9
/* MaterialViewTests.swift */
=
{
isa
=
PBXFileReference
;
fileEncoding
=
4
;
lastKnownFileType
=
sourcecode.swift
;
path
=
MaterialViewTests.swift
;
sourceTree
=
"<group>"
;
};
...
...
@@ -582,7 +580,6 @@
96BCB79C1CB40DC500C806FE
/* TextField.swift */
,
96BCB79D1CB40DC500C806FE
/* TextStorage.swift */
,
96BCB79E1CB40DC500C806FE
/* TextView.swift */
,
9658F1E41CD0229C00B902C1
/* MTextField.swift */
,
);
name
=
Text
;
sourceTree
=
"<group>"
;
...
...
@@ -885,7 +882,6 @@
96BCB7D11CB40DC500C806FE
/* MaterialSwitch.swift in Sources */
,
96BCB7BA1CB40DC500C806FE
/* MaterialCollectionView.swift in Sources */
,
96BCB7A31CB40DC500C806FE
/* CapturePreview.swift in Sources */
,
9658F1E51CD0229D00B902C1
/* MTextField.swift in Sources */
,
96BCB7BC1CB40DC500C806FE
/* MaterialCollectionViewDataSource.swift in Sources */
,
96BCB7C71CB40DC500C806FE
/* MaterialKeyframeAnimation.swift in Sources */
,
96BCB7BE1CB40DC500C806FE
/* MaterialCollectionViewLayout.swift in Sources */
,
...
...
Sources/iOS/MTextField.swift
deleted
100644 → 0
View file @
cb996727
/*
* Copyright (C) 2015 - 2016, Daniel Dahan and CosmicMind, Inc. <http://cosmicmind.io>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of Material nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import
UIKit
public
protocol
MTextFieldDelegate
:
UITextFieldDelegate
{}
@IBDesignable
public
class
MTextField
:
UITextField
{
public
private(set)
var
animating
:
Bool
=
false
/**
This property is the same as clipsToBounds. It crops any of the view's
contents from bleeding past the view's frame.
*/
@IBInspectable
public
var
masksToBounds
:
Bool
{
get
{
return
layer
.
masksToBounds
}
set
(
value
)
{
layer
.
masksToBounds
=
value
}
}
/// A property that accesses the backing layer's backgroundColor.
@IBInspectable
public
override
var
backgroundColor
:
UIColor
?
{
didSet
{
layer
.
backgroundColor
=
backgroundColor
?
.
CGColor
}
}
/// A property that accesses the layer.frame.origin.x property.
@IBInspectable
public
var
x
:
CGFloat
{
get
{
return
layer
.
frame
.
origin
.
x
}
set
(
value
)
{
layer
.
frame
.
origin
.
x
=
value
}
}
/// A property that accesses the layer.frame.origin.y property.
@IBInspectable
public
var
y
:
CGFloat
{
get
{
return
layer
.
frame
.
origin
.
y
}
set
(
value
)
{
layer
.
frame
.
origin
.
y
=
value
}
}
/// A property that accesses the layer.frame.size.width property.
@IBInspectable
public
var
width
:
CGFloat
{
get
{
return
layer
.
frame
.
size
.
width
}
set
(
value
)
{
layer
.
frame
.
size
.
width
=
value
}
}
/// A property that accesses the layer.frame.size.height property.
@IBInspectable
public
var
height
:
CGFloat
{
get
{
return
layer
.
frame
.
size
.
height
}
set
(
value
)
{
layer
.
frame
.
size
.
height
=
value
}
}
/// A property that accesses the layer.position property.
@IBInspectable
public
var
position
:
CGPoint
{
get
{
return
layer
.
position
}
set
(
value
)
{
layer
.
position
=
value
}
}
/// A property that accesses the layer.zPosition property.
@IBInspectable
public
var
zPosition
:
CGFloat
{
get
{
return
layer
.
zPosition
}
set
(
value
)
{
layer
.
zPosition
=
value
}
}
/// Reference to the divider.
public
private(set)
lazy
var
divider
:
CAShapeLayer
=
CAShapeLayer
()
/// Divider height.
@IBInspectable
public
var
dividerHeight
:
CGFloat
=
1
/// Divider active state height.
@IBInspectable
public
var
dividerActiveHeight
:
CGFloat
=
2
/// Sets the divider.
@IBInspectable
public
var
dividerColor
:
UIColor
=
MaterialColor
.
darkText
.
dividers
{
didSet
{
if
!
editing
{
divider
.
backgroundColor
=
dividerColor
.
CGColor
}
}
}
/// Sets the divider.
@IBInspectable
public
var
dividerActiveColor
:
UIColor
?
{
didSet
{
if
let
v
:
UIColor
=
dividerActiveColor
{
if
editing
{
divider
.
backgroundColor
=
v
.
CGColor
}
}
}
}
/// The placeholderLabel font value.
@IBInspectable
public
override
var
font
:
UIFont
?
{
get
{
return
placeholderLabel
.
font
}
set
(
value
)
{
placeholderLabel
.
font
=
value
}
}
/// The placeholderLabel text value.
@IBInspectable
public
override
var
placeholder
:
String
?
{
get
{
return
placeholderLabel
.
text
}
set
(
value
)
{
placeholderLabel
.
text
=
value
if
let
v
:
String
=
value
{
placeholderLabel
.
attributedText
=
NSAttributedString
(
string
:
v
,
attributes
:
[
NSForegroundColorAttributeName
:
placeholderColor
])
}
}
}
/**
The placeholderLabel UILabel that is displayed when there is text. The
placeholderLabel text value is updated with the placeholder text
value before being displayed.
*/
@IBInspectable
public
private(set)
lazy
var
placeholderLabel
:
UILabel
=
UILabel
(
frame
:
CGRectZero
)
/// Placeholder textColor.
@IBInspectable
public
var
placeholderColor
:
UIColor
=
MaterialColor
.
darkText
.
others
{
didSet
{
if
!
editing
{
if
let
v
:
String
=
placeholder
{
placeholderLabel
.
attributedText
=
NSAttributedString
(
string
:
v
,
attributes
:
[
NSForegroundColorAttributeName
:
placeholderColor
])
}
}
}
}
/// Placeholder active textColor.
@IBInspectable
public
var
placeholderActiveColor
:
UIColor
=
MaterialColor
.
blue
.
base
{
didSet
{
if
editing
{
if
let
v
:
String
=
placeholder
{
placeholderLabel
.
attributedText
=
NSAttributedString
(
string
:
v
,
attributes
:
[
NSForegroundColorAttributeName
:
placeholderActiveColor
])
}
}
tintColor
=
placeholderActiveColor
}
}
/// The detailLabel UILabel that is displayed.
@IBInspectable
public
private(set)
lazy
var
detailLabel
:
UILabel
=
UILabel
(
frame
:
CGRectZero
)
/// The detailLabel text value.
@IBInspectable
public
var
detail
:
String
?
{
get
{
return
detailLabel
.
text
}
set
(
value
)
{
detailLabel
.
text
=
value
if
let
v
:
String
=
value
{
detailLabel
.
attributedText
=
NSAttributedString
(
string
:
v
,
attributes
:
[
NSForegroundColorAttributeName
:
detailColor
])
}
layoutDetailLabel
()
}
}
/// Detail textColor.
@IBInspectable
public
var
detailColor
:
UIColor
=
MaterialColor
.
darkText
.
others
{
didSet
{
if
let
v
:
String
=
detailLabel
.
text
{
detailLabel
.
attributedText
=
NSAttributedString
(
string
:
v
,
attributes
:
[
NSForegroundColorAttributeName
:
detailColor
])
}
}
}
/// Handles the textAlignment of the placeholderLabel.
public
override
var
textAlignment
:
NSTextAlignment
{
get
{
return
super
.
textAlignment
}
set
(
value
)
{
super
.
textAlignment
=
value
placeholderLabel
.
textAlignment
=
value
detailLabel
.
textAlignment
=
value
}
}
/// Enables the clearFlatButton.
@IBInspectable
public
var
enableClearFlatButton
:
Bool
{
get
{
return
nil
!=
clearFlatButton
}
set
(
value
)
{
if
value
{
if
nil
==
clearFlatButton
{
let
image
:
UIImage
?
=
MaterialIcon
.
cm
.
clear
clearFlatButton
=
FlatButton
(
frame
:
CGRectZero
)
clearFlatButton
!.
contentEdgeInsets
=
UIEdgeInsetsZero
clearFlatButton
!.
pulseColor
=
nil
clearFlatButton
!.
pulseScale
=
false
clearFlatButton
!.
tintColor
=
placeholderColor
clearFlatButton
!.
setImage
(
image
,
forState
:
.
Normal
)
clearButtonMode
=
.
Never
rightViewMode
=
.
WhileEditing
rightView
=
clearFlatButton
clearFlatButtonAutoHandle
=
clearFlatButtonAutoHandle
?
true
:
false
}
}
else
{
clearFlatButton
?
.
removeTarget
(
self
,
action
:
#selector(
handleClearButton
)
,
forControlEvents
:
.
TouchUpInside
)
clearFlatButton
=
nil
}
}
}
/// Enables the automatic handling of the clearFlatButton.
@IBInspectable
public
var
clearFlatButtonAutoHandle
:
Bool
=
true
{
didSet
{
clearFlatButton
?
.
removeTarget
(
self
,
action
:
#selector(
handleClearButton
)
,
forControlEvents
:
.
TouchUpInside
)
if
clearFlatButtonAutoHandle
{
clearFlatButton
?
.
addTarget
(
self
,
action
:
#selector(
handleClearButton
)
,
forControlEvents
:
.
TouchUpInside
)
}
}
}
/// Enables the visibilityFlatButton.
@IBInspectable
public
var
enableVisibilityFlatButton
:
Bool
{
get
{
return
nil
!=
visibilityFlatButton
}
set
(
value
)
{
if
value
{
if
nil
==
visibilityFlatButton
{
let
image
:
UIImage
?
=
MaterialIcon
.
visibility
visibilityFlatButton
=
FlatButton
(
frame
:
CGRectZero
)
visibilityFlatButton
!.
contentEdgeInsets
=
UIEdgeInsetsZero
visibilityFlatButton
!.
pulseColor
=
nil
visibilityFlatButton
!.
pulseScale
=
false
visibilityFlatButton
!.
tintColor
=
placeholderColor
visibilityFlatButton
!.
setImage
(
image
,
forState
:
.
Normal
)
secureTextEntry
=
true
clearButtonMode
=
.
Never
rightViewMode
=
.
WhileEditing
rightView
=
visibilityFlatButton
visibilityFlatButtonAutoHandle
=
visibilityFlatButtonAutoHandle
?
true
:
false
}
}
else
{
visibilityFlatButton
?
.
removeTarget
(
self
,
action
:
#selector(
handleClearButton
)
,
forControlEvents
:
.
TouchUpInside
)
visibilityFlatButton
=
nil
}
}
}
/// Enables the automatic handling of the visibilityFlatButton.
@IBInspectable
public
var
visibilityFlatButtonAutoHandle
:
Bool
=
true
{
didSet
{
visibilityFlatButton
?
.
removeTarget
(
self
,
action
:
#selector(
handleVisibilityButton
)
,
forControlEvents
:
.
TouchUpInside
)
if
visibilityFlatButtonAutoHandle
{
visibilityFlatButton
?
.
addTarget
(
self
,
action
:
#selector(
handleVisibilityButton
)
,
forControlEvents
:
.
TouchUpInside
)
}
}
}
/// A reference to the clearFlatButton.
public
private(set)
var
clearFlatButton
:
FlatButton
?
/// A reference to the visibilityFlatButton.
public
private(set)
var
visibilityFlatButton
:
FlatButton
?
/**
An initializer that initializes the object with a NSCoder object.
- Parameter aDecoder: A NSCoder instance.
*/
public
required
init
?(
coder
aDecoder
:
NSCoder
)
{
super
.
init
(
coder
:
aDecoder
)
prepareView
()
}
/**
An initializer that initializes the object with a CGRect object.
If AutoLayout is used, it is better to initilize the instance
using the init() initializer.
- Parameter frame: A CGRect instance.
*/
public
override
init
(
frame
:
CGRect
)
{
super
.
init
(
frame
:
frame
)
prepareView
()
}
/// A convenience initializer.
public
convenience
init
()
{
self
.
init
(
frame
:
CGRectZero
)
}
public
override
func
layoutSubviews
()
{
super
.
layoutSubviews
()
layoutToSize
()
}
public
override
func
layoutSublayersOfLayer
(
layer
:
CALayer
)
{
super
.
layoutSublayersOfLayer
(
layer
)
if
self
.
layer
==
layer
{
layoutDivider
()
}
}
/// Default size when using AutoLayout.
public
override
func
intrinsicContentSize
()
->
CGSize
{
return
CGSizeMake
(
width
,
32
)
}
/**
A method that accepts CAAnimation objects and executes them on the
view's backing layer.
- Parameter animation: A CAAnimation instance.
*/
public
func
animate
(
animation
:
CAAnimation
)
{
animation
.
delegate
=
self
if
let
a
:
CABasicAnimation
=
animation
as?
CABasicAnimation
{
a
.
fromValue
=
(
nil
==
layer
.
presentationLayer
()
?
layer
:
layer
.
presentationLayer
()
as!
CALayer
)
.
valueForKeyPath
(
a
.
keyPath
!
)
}
if
let
a
:
CAPropertyAnimation
=
animation
as?
CAPropertyAnimation
{
layer
.
addAnimation
(
a
,
forKey
:
a
.
keyPath
!
)
}
else
if
let
a
:
CAAnimationGroup
=
animation
as?
CAAnimationGroup
{
layer
.
addAnimation
(
a
,
forKey
:
nil
)
}
else
if
let
a
:
CATransition
=
animation
as?
CATransition
{
layer
.
addAnimation
(
a
,
forKey
:
kCATransition
)
}
}
/**
A delegation method that is executed when the backing layer starts
running an animation.
- Parameter anim: The currently running CAAnimation instance.
*/
public
override
func
animationDidStart
(
anim
:
CAAnimation
)
{
(
delegate
as?
MaterialAnimationDelegate
)?
.
materialAnimationDidStart
?(
anim
)
}
/**
A delegation method that is executed when the backing layer stops
running an animation.
- Parameter anim: The CAAnimation instance that stopped running.
- Parameter flag: A boolean that indicates if the animation stopped
because it was completed or interrupted. True if completed, false
if interrupted.
*/
public
override
func
animationDidStop
(
anim
:
CAAnimation
,
finished
flag
:
Bool
)
{
if
let
a
:
CAPropertyAnimation
=
anim
as?
CAPropertyAnimation
{
if
let
b
:
CABasicAnimation
=
a
as?
CABasicAnimation
{
if
let
v
:
AnyObject
=
b
.
toValue
{
if
let
k
:
String
=
b
.
keyPath
{
layer
.
setValue
(
v
,
forKeyPath
:
k
)
layer
.
removeAnimationForKey
(
k
)
}
}
}
(
delegate
as?
MaterialAnimationDelegate
)?
.
materialAnimationDidStop
?(
anim
,
finished
:
flag
)
}
else
if
let
a
:
CAAnimationGroup
=
anim
as?
CAAnimationGroup
{
for
x
in
a
.
animations
!
{
animationDidStop
(
x
,
finished
:
true
)
}
}
}
/// Handles the text editing did begin state.
public
func
handleEditingDidBegin
()
{
dividerEditingDidBeginAnimation
()
placeholderEditingDidBeginAnimation
()
}
/// Handles the text editing did end state.
public
func
handleEditingDidEnd
()
{
dividerEditingDidEndAnimation
()
placeholderEditingDidEndAnimation
()
}
/// Handles the clearFlatButton TouchUpInside event.
public
func
handleClearButton
()
{
if
false
==
delegate
?
.
textFieldShouldClear
?(
self
)
{
return
}
text
=
nil
}
/// Handles the visibilityFlatButton TouchUpInside event.
public
func
handleVisibilityButton
()
{
secureTextEntry
=
!
secureTextEntry
visibilityFlatButton
?
.
tintColor
=
placeholderColor
.
colorWithAlphaComponent
(
secureTextEntry
?
0.38
:
0.54
)
}
/**
Prepares the view instance when intialized. When subclassing,
it is recommended to override the prepareView method
to initialize property values and other setup operations.
The super.prepareView method should always be called immediately
when subclassing.
*/
public
func
prepareView
()
{
masksToBounds
=
false
borderStyle
=
.
None
backgroundColor
=
nil
textColor
=
MaterialColor
.
darkText
.
primary
font
=
RobotoFont
.
regularWithSize
(
16
)
prepareDivider
()
preparePlaceholderLabel
()
prepareDetailLabel
()
prepareTargetHandlers
()
}
/// Ensures that the components are sized correctly.
public
func
layoutToSize
()
{
if
!
animating
{
layoutPlaceholderLabel
()
layoutDetailLabel
()
layoutClearFlatButton
()
layoutVisibilityFlatButton
()
}
}
/// Layout the divider.
public
func
layoutDivider
()
{
divider
.
frame
=
CGRectMake
(
0
,
height
,
width
,
editing
?
dividerActiveHeight
:
dividerHeight
)
}
/// Layout the placeholderLabel.
public
func
layoutPlaceholderLabel
()
{
if
!
editing
&&
true
==
text
?
.
isEmpty
{
placeholderLabel
.
frame
=
bounds
}
else
if
CGAffineTransformIsIdentity
(
placeholderLabel
.
transform
)
{
placeholderLabel
.
frame
=
bounds
placeholderLabel
.
transform
=
CGAffineTransformMakeScale
(
0.75
,
0.75
)
switch
textAlignment
{
case
.
Left
,
.
Natural
:
placeholderLabel
.
frame
.
origin
.
x
=
0
case
.
Right
:
placeholderLabel
.
frame
.
origin
.
x
=
width
-
placeholderLabel
.
frame
.
width
default
:
break
}
placeholderLabel
.
frame
.
origin
.
y
=
-
placeholderLabel
.
frame
.
size
.
height
placeholderLabel
.
textColor
=
placeholderColor
}
else
{
switch
textAlignment
{
case
.
Left
,
.
Natural
:
placeholderLabel
.
frame
.
origin
.
x
=
0
case
.
Right
:
placeholderLabel
.
frame
.
origin
.
x
=
width
-
placeholderLabel
.
frame
.
width
case
.
Center
:
placeholderLabel
.
center
.
x
=
width
/
2
default
:
break
}
placeholderLabel
.
frame
.
size
.
width
=
width
*
0.75
}
}
/// Layout the detailLabel.
public
func
layoutDetailLabel
()
{
var
h
:
CGFloat
=
nil
==
detail
?
12
:
detailLabel
.
font
.
stringSize
(
detail
!
,
constrainedToWidth
:
Double
(
width
))
.
height
detailLabel
.
frame
=
CGRectMake
(
0
,
divider
.
frame
.
origin
.
y
+
8
,
width
,
h
)
}
/// Layout the clearFlatButton.
public
func
layoutClearFlatButton
()
{
if
let
v
:
FlatButton
=
clearFlatButton
{
if
0
<
width
&&
0
<
height
{
v
.
frame
=
CGRectMake
(
width
-
height
,
0
,
height
,
height
)
}
}
}
/// Layout the visibilityFlatButton.
public
func
layoutVisibilityFlatButton
()
{
if
let
v
:
FlatButton
=
visibilityFlatButton
{
if
0
<
width
&&
0
<
height
{
v
.
frame
=
CGRectMake
(
width
-
height
,
0
,
height
,
height
)
}
}
}
/// The animation for the divider when editing begins.
public
func
dividerEditingDidBeginAnimation
()
{
divider
.
frame
.
size
.
height
=
dividerActiveHeight
divider
.
backgroundColor
=
nil
==
dividerActiveColor
?
placeholderActiveColor
.
CGColor
:
dividerActiveColor
!.
CGColor
}
/// The animation for the divider when editing ends.
public
func
dividerEditingDidEndAnimation
()
{
divider
.
frame
.
size
.
height
=
dividerHeight
divider
.
backgroundColor
=
dividerColor
.
CGColor
}
/// The animation for the placeholder when editing begins.
public
func
placeholderEditingDidBeginAnimation
()
{
if
CGAffineTransformIsIdentity
(
placeholderLabel
.
transform
)
{
animating
=
true
UIView
.
animateWithDuration
(
0.15
,
animations
:
{
[
weak
self
]
in
if
let
v
:
MTextField
=
self
{
v
.
placeholderLabel
.
transform
=
CGAffineTransformMakeScale
(
0.75
,
0.75
)
switch
v
.
textAlignment
{
case
.
Left
,
.
Natural
:
v
.
placeholderLabel
.
frame
.
origin
.
x
=
0
case
.
Right
:
v
.
placeholderLabel
.
frame
.
origin
.
x
=
v
.
width
-
v
.
placeholderLabel
.
frame
.
width
default
:
break
}
v
.
placeholderLabel
.
frame
.
origin
.
y
=
-
v
.
placeholderLabel
.
frame
.
size
.
height
v
.
placeholderLabel
.
textColor
=
v
.
placeholderActiveColor
}
})
{
[
weak
self
]
_
in
self
?
.
animating
=
false
}
}
else
if
editing
{
placeholderLabel
.
textColor
=
placeholderActiveColor
}
}
/// The animation for the placeholder when editing ends.
public
func
placeholderEditingDidEndAnimation
()
{
if
!
CGAffineTransformIsIdentity
(
placeholderLabel
.
transform
)
&&
true
==
text
?
.
isEmpty
{
animating
=
true
UIView
.
animateWithDuration
(
0.15
,
animations
:
{
[
weak
self
]
in
if
let
v
:
MTextField
=
self
{
v
.
placeholderLabel
.
transform
=
CGAffineTransformIdentity
v
.
placeholderLabel
.
frame
.
origin
.
x
=
0
v
.
placeholderLabel
.
frame
.
origin
.
y
=
0
v
.
placeholderLabel
.
textColor
=
v
.
placeholderColor
}
})
{
[
weak
self
]
_
in
self
?
.
animating
=
false
}
}
else
if
!
editing
{
placeholderLabel
.
textColor
=
placeholderColor
}
}
/// Prepares the divider.
private
func
prepareDivider
()
{
dividerColor
=
MaterialColor
.
darkText
.
dividers
layer
.
addSublayer
(
divider
)
}
/// Prepares the placeholderLabel.
private
func
preparePlaceholderLabel
()
{
placeholderColor
=
MaterialColor
.
darkText
.
others
addSubview
(
placeholderLabel
)
}
/// Prepares the detailLabel.
private
func
prepareDetailLabel
()
{
detailLabel
.
font
=
RobotoFont
.
regularWithSize
(
12
)
detailColor
=
MaterialColor
.
darkText
.
others
addSubview
(
detailLabel
)
}
/// Prepares the target handlers.
private
func
prepareTargetHandlers
()
{
addTarget
(
self
,
action
:
#selector(
handleEditingDidBegin
)
,
forControlEvents
:
.
EditingDidBegin
)
addTarget
(
self
,
action
:
#selector(
handleEditingDidEnd
)
,
forControlEvents
:
.
EditingDidEnd
)
}
}
\ No newline at end of file
Sources/iOS/TextField.swift
View file @
d3650f18
...
...
@@ -34,11 +34,7 @@ public protocol TextFieldDelegate : UITextFieldDelegate {}
@IBDesignable
public
class
TextField
:
UITextField
{
/// A boolean that indicates if the titleLabel is animating.
private
var
titleLabelAnimating
:
Bool
=
false
/// A reference to the placeholder value.
private
var
placeholderText
:
String
?
public
private(set)
var
animating
:
Bool
=
false
/**
This property is the same as clipsToBounds. It crops any of the view's
...
...
@@ -100,310 +96,230 @@ public class TextField : UITextField {
}
}
/// A property that accesses the backing layer's shadowColor.
@IBInspectable
public
var
shadowColor
:
UIColor
?
{
didSet
{
layer
.
shadowColor
=
shadowColor
?
.
CGColor
}
}
/// A property that accesses the backing layer's shadowOffset.
@IBInspectable
public
var
shadowOffset
:
CGSize
{
/// A property that accesses the layer.position property.
@IBInspectable
public
var
position
:
CGPoint
{
get
{
return
layer
.
shadowOffset
return
layer
.
position
}
set
(
value
)
{
layer
.
shadowOffset
=
value
layer
.
position
=
value
}
}
/// A property that accesses the
backing layer's shadowOpaci
ty.
@IBInspectable
public
var
shadowOpacity
:
Float
{
/// A property that accesses the
layer.zPosition proper
ty.
@IBInspectable
public
var
zPosition
:
CG
Float
{
get
{
return
layer
.
shadowOpacity
return
layer
.
zPosition
}
set
(
value
)
{
layer
.
shadowOpacity
=
value
layer
.
zPosition
=
value
}
}
/// A property that accesses the backing layer's shadowRadius.
@IBInspectable
public
var
shadowRadius
:
CGFloat
{
get
{
return
layer
.
shadowRadius
}
set
(
value
)
{
layer
.
shadowRadius
=
value
}
}
/// Reference to the divider.
public
private(set)
lazy
var
divider
:
CAShapeLayer
=
CAShapeLayer
()
/// A property that accesses the backing layer's shadowPath.
@IBInspectable
public
var
shadowPath
:
CGPath
?
{
get
{
return
layer
.
shadowPath
}
set
(
value
)
{
layer
.
shadowPath
=
value
}
}
/// Divider height.
@IBInspectable
public
var
dividerHeight
:
CGFloat
=
1
/// Enables automatic shadowPath sizing.
@IBInspectable
public
var
shadowPathAutoSizeEnabled
:
Bool
=
true
{
didSet
{
if
shadowPathAutoSizeEnabled
{
layoutShadowPath
()
}
else
{
shadowPath
=
nil
}
}
}
/**
A property that sets the shadowOffset, shadowOpacity, and shadowRadius
for the backing layer. This is the preferred method of setting depth
in order to maintain consitency across UI objects.
*/
public
var
depth
:
MaterialDepth
=
.
None
{
didSet
{
let
value
:
MaterialDepthType
=
MaterialDepthToValue
(
depth
)
shadowOffset
=
value
.
offset
shadowOpacity
=
value
.
opacity
shadowRadius
=
value
.
radius
layoutShadowPath
()
}
}
/// Divider active state height.
@IBInspectable
public
var
dividerActiveHeight
:
CGFloat
=
2
///
A property that sets the cornerRadius of the backing lay
er.
public
var
cornerRadiusPreset
:
MaterialRadius
=
.
None
{
///
Sets the divid
er.
@IBInspectable
public
var
dividerColor
:
UIColor
=
MaterialColor
.
darkText
.
dividers
{
didSet
{
if
let
v
:
MaterialRadius
=
cornerRadiusPreset
{
cornerRadius
=
MaterialRadiusToValue
(
v
)
if
!
editing
{
divider
.
backgroundColor
=
dividerColor
.
CGColor
}
}
}
/// A property that accesses the layer.cornerRadius.
@IBInspectable
public
var
cornerRadius
:
CGFloat
{
get
{
return
layer
.
cornerRadius
}
set
(
value
)
{
layer
.
cornerRadius
=
value
layoutShadowPath
()
}
}
/// A preset property to set the borderWidth.
public
var
borderWidthPreset
:
MaterialBorder
=
.
None
{
/// Sets the divider.
@IBInspectable
public
var
dividerActiveColor
:
UIColor
?
{
didSet
{
borderWidth
=
MaterialBorderToValue
(
borderWidthPreset
)
}
}
/// A property that accesses the layer.borderWith.
@IBInspectable
public
var
borderWidth
:
CGFloat
{
get
{
return
layer
.
borderWidth
}
set
(
value
)
{
layer
.
borderWidth
=
value
if
let
v
:
UIColor
=
dividerActiveColor
{
if
editing
{
divider
.
backgroundColor
=
v
.
CGColor
}
}
}
}
///
A property that accesses the layer.borderColor property
.
@IBInspectable
public
var
borderColor
:
UIColor
?
{
///
The placeholderLabel font value
.
@IBInspectable
public
override
var
font
:
UIFont
?
{
get
{
return
nil
==
layer
.
borderColor
?
nil
:
UIColor
(
CGColor
:
layer
.
borderColor
!
)
return
placeholderLabel
.
font
}
set
(
value
)
{
layer
.
borderColor
=
value
?
.
CGColor
placeholderLabel
.
font
=
value
}
}
///
A property that accesses the layer.position property
.
@IBInspectable
public
var
position
:
CGPoint
{
///
The placeholderLabel text value
.
@IBInspectable
public
override
var
placeholder
:
String
?
{
get
{
return
layer
.
position
return
placeholderLabel
.
text
}
set
(
value
)
{
layer
.
position
=
value
placeholderLabel
.
text
=
value
if
let
v
:
String
=
value
{
placeholderLabel
.
attributedText
=
NSAttributedString
(
string
:
v
,
attributes
:
[
NSForegroundColorAttributeName
:
placeholderColor
])
}
}
}
/// A property that accesses the layer.zPosition property.
@IBInspectable
public
var
zPosition
:
CGFloat
{
get
{
return
layer
.
zPosition
}
set
(
value
)
{
layer
.
zPosition
=
value
}
}
/**
The placeholderLabel UILabel that is displayed when there is text. The
placeholderLabel text value is updated with the placeholder text
value before being displayed.
*/
@IBInspectable
public
private(set)
lazy
var
placeholderLabel
:
UILabel
=
UILabel
(
frame
:
CGRectZero
)
///
Handle the clearButton manually
.
@IBInspectable
public
var
clearButtonAutoHandleEnabled
:
Bool
=
true
{
///
Placeholder textColor
.
@IBInspectable
public
var
placeholderColor
:
UIColor
=
MaterialColor
.
darkText
.
others
{
didSet
{
clearButton
.
removeTarget
(
self
,
action
:
#selector(
handleClearButton
)
,
forControlEvents
:
.
TouchUpInside
)
if
clearButtonAutoHandleEnabled
{
clearButton
.
addTarget
(
self
,
action
:
#selector(
handleClearButton
)
,
forControlEvents
:
.
TouchUpInside
)
if
!
editing
{
if
let
v
:
String
=
placeholder
{
placeholderLabel
.
attributedText
=
NSAttributedString
(
string
:
v
,
attributes
:
[
NSForegroundColorAttributeName
:
placeholderColor
])
}
}
}
}
/// Reference to the clearButton.
public
private(set)
var
clearButton
:
FlatButton
!
/// The bottom border layer.
public
private(set)
lazy
var
lineLayer
:
CAShapeLayer
=
CAShapeLayer
()
/**
A property that sets the distance between the textField and
lineLayer.
*/
@IBInspectable
public
var
lineLayerDistance
:
CGFloat
=
4
/// The height of the line when not active.
@IBInspectable
public
var
lineLayerThickness
:
CGFloat
=
1
/// The height of the line when active.
@IBInspectable
public
var
lineLayerActiveThickness
:
CGFloat
=
2
/// The lineLayer color when inactive.
@IBInspectable
public
var
lineLayerColor
:
UIColor
?
{
/// Placeholder active textColor.
@IBInspectable
public
var
placeholderActiveColor
:
UIColor
=
MaterialColor
.
blue
.
base
{
didSet
{
lineLayer
.
backgroundColor
=
lineLayerColor
?
.
CGColor
if
editing
{
if
let
v
:
String
=
placeholder
{
placeholderLabel
.
attributedText
=
NSAttributedString
(
string
:
v
,
attributes
:
[
NSForegroundColorAttributeName
:
placeholderActiveColor
])
}
}
tintColor
=
placeholderActiveColor
}
}
/// The
lineLayer active color
.
@IBInspectable
public
var
lineLayerActiveColor
:
UIColor
?
/// The
detailLabel UILabel that is displayed
.
@IBInspectable
public
private(set)
lazy
var
detailLabel
:
UILabel
=
UILabel
(
frame
:
CGRectZero
)
/// The lineLayer detail color when inactive.
@IBInspectable
public
var
lineLayerDetailColor
:
UIColor
?
/// The lineLayer detail active color.
@IBInspectable
public
var
lineLayerDetailActiveColor
:
UIColor
?
/**
The title UILabel that is displayed when there is text. The
titleLabel text value is updated with the placeholder text
value before being displayed.
*/
@IBInspectable
public
private(set)
var
titleLabel
:
UILabel
!
/// The color of the titleLabel text when the textField is not active.
@IBInspectable
public
var
titleLabelColor
:
UIColor
?
{
didSet
{
titleLabel
.
textColor
=
titleLabelColor
if
nil
==
lineLayerColor
{
lineLayerColor
=
titleLabelColor
/// The detailLabel text value.
@IBInspectable
public
var
detail
:
String
?
{
get
{
return
detailLabel
.
text
}
set
(
value
)
{
detailLabel
.
text
=
value
if
let
v
:
String
=
value
{
detailLabel
.
attributedText
=
NSAttributedString
(
string
:
v
,
attributes
:
[
NSForegroundColorAttributeName
:
detailColor
])
}
layoutDetailLabel
()
}
}
///
The color of the titleLabel text when the textField is active
.
@IBInspectable
public
var
titleLabelActiveColor
:
UIColor
?
{
///
Detail textColor
.
@IBInspectable
public
var
detailColor
:
UIColor
=
MaterialColor
.
darkText
.
others
{
didSet
{
if
nil
==
lineLayerActiveColor
{
lineLayerActiveColor
=
titleLabelActiveColor
if
let
v
:
String
=
detailLabel
.
text
{
detailLabel
.
attributedText
=
NSAttributedString
(
string
:
v
,
attributes
:
[
NSForegroundColorAttributeName
:
detailColor
])
}
tintColor
=
titleLabelActiveColor
}
}
/
**
A property that sets the distance between the textField and
titleLabel.
*/
@IBInspectable
public
var
titleLabelAnimationDistance
:
CGFloat
=
4
/**
The detail UILabel that is displayed when the detailLabelHidden property
is set to false.
*/
@IBInspectable
public
private(set)
var
detailLabel
:
UILabel
!
/
// Handles the textAlignment of the placeholderLabel.
public
override
var
textAlignment
:
NSTextAlignment
{
get
{
return
super
.
textAlignment
}
set
(
value
)
{
super
.
textAlignment
=
value
placeholderLabel
.
textAlignment
=
value
detailLabel
.
textAlignment
=
value
}
}
/**
The color of the detailLabel text when the detailLabelHidden property
is set to false.
*/
@IBInspectable
public
var
detailLabelActiveColor
:
UIColor
?
{
didSet
{
if
!
detailLabelHidden
{
detailLabel
.
textColor
=
detailLabelActiveColor
if
nil
==
lineLayerDetailActiveColor
{
lineLayerDetailActiveColor
=
detailLabelActiveColor
/// Enables the clearFlatButton.
@IBInspectable
public
var
enableClearFlatButton
:
Bool
{
get
{
return
nil
!=
clearFlatButton
}
set
(
value
)
{
if
value
{
if
nil
==
clearFlatButton
{
let
image
:
UIImage
?
=
MaterialIcon
.
cm
.
clear
clearFlatButton
=
FlatButton
(
frame
:
CGRectZero
)
clearFlatButton
!.
contentEdgeInsets
=
UIEdgeInsetsZero
clearFlatButton
!.
pulseColor
=
nil
clearFlatButton
!.
pulseScale
=
false
clearFlatButton
!.
tintColor
=
placeholderColor
clearFlatButton
!.
setImage
(
image
,
forState
:
.
Normal
)
clearFlatButton
!.
setImage
(
image
,
forState
:
.
Highlighted
)
clearButtonMode
=
.
Never
rightViewMode
=
.
WhileEditing
rightView
=
clearFlatButton
clearFlatButtonAutoHandle
=
clearFlatButtonAutoHandle
?
true
:
false
}
}
else
{
clearFlatButton
?
.
removeTarget
(
self
,
action
:
#selector(
handleClearButton
)
,
forControlEvents
:
.
TouchUpInside
)
clearFlatButton
=
nil
}
}
}
/**
A property that sets the distance between the textField and
detailLabel.
*/
@IBInspectable
public
var
detailLabelAnimationDistance
:
CGFloat
=
8
/**
A Boolean that indicates the detailLabel should hide
automatically when text changes.
*/
@IBInspectable
public
var
detailLabelAutoHideEnabled
:
Bool
=
true
/// A boolean that indicates to hide or not hide the detailLabel.
@IBInspectable
public
var
detailLabelHidden
:
Bool
=
true
{
/// Enables the automatic handling of the clearFlatButton.
@IBInspectable
public
var
clearFlatButtonAutoHandle
:
Bool
=
true
{
didSet
{
if
detailLabelHidden
{
detailLabel
.
textColor
=
titleLabelColor
lineLayer
.
backgroundColor
=
(
editing
?
lineLayerActiveColor
:
lineLayerColor
)?
.
CGColor
hideDetailLabel
()
}
else
{
detailLabel
.
textColor
=
detailLabelActiveColor
lineLayer
.
backgroundColor
=
(
nil
==
lineLayerDetailActiveColor
?
detailLabelActiveColor
:
lineLayerDetailActiveColor
)?
.
CGColor
showDetailLabel
()
clearFlatButton
?
.
removeTarget
(
self
,
action
:
#selector(
handleClearButton
)
,
forControlEvents
:
.
TouchUpInside
)
if
clearFlatButtonAutoHandle
{
clearFlatButton
?
.
addTarget
(
self
,
action
:
#selector(
handleClearButton
)
,
forControlEvents
:
.
TouchUpInside
)
}
}
}
///
Sets the placeholder value
.
@IBInspectable
public
override
var
placeholder
:
String
?
{
///
Enables the visibilityFlatButton
.
@IBInspectable
public
var
enableVisibilityFlatButton
:
Bool
{
get
{
return
editing
?
nil
:
placeholderText
return
nil
!=
visibilityFlatButton
}
set
(
value
)
{
if
let
v
:
String
=
value
{
placeholderText
=
v
attributedPlaceholder
=
NSAttributedString
(
string
:
v
,
attributes
:
[
NSForegroundColorAttributeName
:
placeholderTextColor
])
}
else
{
super
.
placeholder
=
nil
if
!
editing
{
placeholderText
=
nil
if
value
{
if
nil
==
visibilityFlatButton
{
let
image
:
UIImage
?
=
MaterialIcon
.
visibility
visibilityFlatButton
=
FlatButton
(
frame
:
CGRectZero
)
visibilityFlatButton
!.
contentEdgeInsets
=
UIEdgeInsetsZero
visibilityFlatButton
!.
pulseColor
=
nil
visibilityFlatButton
!.
pulseScale
=
false
visibilityFlatButton
!.
tintColor
=
placeholderColor
visibilityFlatButton
!.
setImage
(
image
,
forState
:
.
Normal
)
visibilityFlatButton
!.
setImage
(
image
,
forState
:
.
Highlighted
)
visibilityFlatButton
!.
tintColor
=
placeholderColor
.
colorWithAlphaComponent
(
secureTextEntry
?
0.38
:
0.54
)
secureTextEntry
=
true
clearButtonMode
=
.
Never
rightViewMode
=
.
WhileEditing
rightView
=
visibilityFlatButton
visibilityFlatButtonAutoHandle
=
visibilityFlatButtonAutoHandle
?
true
:
false
}
}
if
false
==
text
?
.
isEmpty
{
titleLabel
.
text
=
placeholderText
}
else
{
visibilityFlatButton
?
.
removeTarget
(
self
,
action
:
#selector(
handleClearButton
)
,
forControlEvents
:
.
TouchUpInside
)
visibilityFlatButton
=
nil
}
}
}
///
Placeholder textColor
.
@IBInspectable
public
var
placeholderTextColor
:
UIColor
=
MaterialColor
.
darkText
.
others
{
///
Enables the automatic handling of the visibilityFlatButton
.
@IBInspectable
public
var
visibilityFlatButtonAutoHandle
:
Bool
=
true
{
didSet
{
if
let
v
:
String
=
placeholder
{
attributedPlaceholder
=
NSAttributedString
(
string
:
v
,
attributes
:
[
NSForegroundColorAttributeName
:
placeholderTextColor
])
visibilityFlatButton
?
.
removeTarget
(
self
,
action
:
#selector(
handleVisibilityButton
)
,
forControlEvents
:
.
TouchUpInside
)
if
visibilityFlatButtonAutoHandle
{
visibilityFlatButton
?
.
addTarget
(
self
,
action
:
#selector(
handleVisibilityButton
)
,
forControlEvents
:
.
TouchUpInside
)
}
}
}
/// An override to the text property.
@IBInspectable
public
override
var
text
:
String
?
{
didSet
{
sendActionsForControlEvents
(
.
ValueChanged
)
if
nil
!=
oldValue
&&
true
==
text
?
.
isEmpty
&&
!
editing
{
handleEditingDidEnd
()
}
}
}
/// A reference to the clearFlatButton.
public
private(set)
var
clearFlatButton
:
FlatButton
?
/// A reference to the visibilityFlatButton.
public
private(set)
var
visibilityFlatButton
:
FlatButton
?
/**
An initializer that initializes the object with a NSCoder object.
...
...
@@ -432,17 +348,21 @@ public class TextField : UITextField {
public
override
func
layoutSubviews
()
{
super
.
layoutSubviews
()
layout
ClearButton
()
layout
ToSize
()
}
public
override
func
layoutSublayersOfLayer
(
layer
:
CALayer
)
{
super
.
layoutSublayersOfLayer
(
layer
)
if
self
.
layer
==
layer
{
layoutLineLayer
()
layoutShadowPath
()
layoutDivider
()
}
}
/// Default size when using AutoLayout.
public
override
func
intrinsicContentSize
()
->
CGSize
{
return
CGSizeMake
(
width
,
32
)
}
/**
A method that accepts CAAnimation objects and executes them on the
view's backing layer.
...
...
@@ -497,6 +417,32 @@ public class TextField : UITextField {
}
}
/// Handles the text editing did begin state.
public
func
handleEditingDidBegin
()
{
dividerEditingDidBeginAnimation
()
placeholderEditingDidBeginAnimation
()
}
/// Handles the text editing did end state.
public
func
handleEditingDidEnd
()
{
dividerEditingDidEndAnimation
()
placeholderEditingDidEndAnimation
()
}
/// Handles the clearFlatButton TouchUpInside event.
public
func
handleClearButton
()
{
if
false
==
delegate
?
.
textFieldShouldClear
?(
self
)
{
return
}
text
=
nil
}
/// Handles the visibilityFlatButton TouchUpInside event.
public
func
handleVisibilityButton
()
{
secureTextEntry
=
!
secureTextEntry
visibilityFlatButton
?
.
tintColor
=
visibilityFlatButton
?
.
tintColor
.
colorWithAlphaComponent
(
secureTextEntry
?
0.38
:
0.54
)
}
/**
Prepares the view instance when intialized. When subclassing,
it is recommended to override the prepareView method
...
...
@@ -507,195 +453,162 @@ public class TextField : UITextField {
public
func
prepareView
()
{
masksToBounds
=
false
borderStyle
=
.
None
backgroundColor
=
MaterialColor
.
white
backgroundColor
=
nil
textColor
=
MaterialColor
.
darkText
.
primary
font
=
RobotoFont
.
regularWithSize
(
16
)
prepareClearButton
()
prepareTitleLabel
()
prepareLineLayer
()
prepareDivider
()
preparePlaceholderLabel
()
prepareDetailLabel
()
addTarget
(
self
,
action
:
#selector(
handleEditingDidBegin
)
,
forControlEvents
:
.
EditingDidBegin
)
addTarget
(
self
,
action
:
#selector(
handleEditingChanged
)
,
forControlEvents
:
.
EditingChanged
)
addTarget
(
self
,
action
:
#selector(
handleEditingDidEnd
)
,
forControlEvents
:
.
EditingDidEnd
)
addTarget
(
self
,
action
:
#selector(
handleValueChanged
)
,
forControlEvents
:
.
ValueChanged
)
prepareTargetHandlers
()
}
/// Handler for editing changed.
internal
func
handleEditingChanged
()
{
sendActionsForControlEvents
(
.
ValueChanged
)
/// Ensures that the components are sized correctly.
public
func
layoutToSize
()
{
if
!
animating
{
layoutPlaceholderLabel
()
layoutDetailLabel
()
layoutClearFlatButton
()
layoutVisibilityFlatButton
()
}
}
/// Clears the textField text.
internal
func
handleClearButton
()
{
if
false
==
delegate
?
.
textFieldShouldClear
?(
self
)
{
return
}
text
=
nil
/// Layout the divider.
public
func
layoutDivider
()
{
divider
.
frame
=
CGRectMake
(
0
,
height
,
width
,
editing
?
dividerActiveHeight
:
dividerHeight
)
}
/// Ahdnler when text value changed.
internal
func
handleValueChanged
()
{
if
detailLabelAutoHideEnabled
&&
!
detailLabelHidden
{
detailLabelHidden
=
true
lineLayer
.
backgroundColor
=
(
nil
==
lineLayerActiveColor
?
titleLabelActiveColor
:
lineLayerActiveColor
)?
.
CGColor
/// Layout the placeholderLabel.
public
func
layoutPlaceholderLabel
()
{
if
!
editing
&&
true
==
text
?
.
isEmpty
{
placeholderLabel
.
frame
=
bounds
}
else
if
CGAffineTransformIsIdentity
(
placeholderLabel
.
transform
)
{
placeholderLabel
.
frame
=
bounds
placeholderLabel
.
transform
=
CGAffineTransformMakeScale
(
0.75
,
0.75
)
switch
textAlignment
{
case
.
Left
,
.
Natural
:
placeholderLabel
.
frame
.
origin
.
x
=
0
case
.
Right
:
placeholderLabel
.
frame
.
origin
.
x
=
width
-
placeholderLabel
.
frame
.
width
default
:
break
}
placeholderLabel
.
frame
.
origin
.
y
=
-
placeholderLabel
.
frame
.
size
.
height
placeholderLabel
.
textColor
=
placeholderColor
}
else
{
switch
textAlignment
{
case
.
Left
,
.
Natural
:
placeholderLabel
.
frame
.
origin
.
x
=
0
case
.
Right
:
placeholderLabel
.
frame
.
origin
.
x
=
width
-
placeholderLabel
.
frame
.
width
case
.
Center
:
placeholderLabel
.
center
.
x
=
width
/
2
default
:
break
}
placeholderLabel
.
frame
.
size
.
width
=
width
*
0.75
}
}
/// Handler for text editing began.
internal
func
handleEditingDidBegin
()
{
showTitleLabel
()
placeholder
=
nil
titleLabel
.
textColor
=
titleLabelActiveColor
lineLayer
.
frame
.
size
.
height
=
lineLayerActiveThickness
lineLayer
.
backgroundColor
=
(
detailLabelHidden
?
nil
==
lineLayerActiveColor
?
titleLabelActiveColor
:
lineLayerActiveColor
:
nil
==
lineLayerDetailActiveColor
?
detailLabelActiveColor
:
lineLayerDetailActiveColor
)?
.
CGColor
/// Layout the detailLabel.
public
func
layoutDetailLabel
()
{
var
h
:
CGFloat
=
nil
==
detail
?
12
:
detailLabel
.
font
.
stringSize
(
detail
!
,
constrainedToWidth
:
Double
(
width
))
.
height
detailLabel
.
frame
=
CGRectMake
(
0
,
divider
.
frame
.
origin
.
y
+
8
,
width
,
h
)
}
/// Handler for text editing ended.
internal
func
handleEditingDidEnd
()
{
if
true
==
text
?
.
isEmpty
&&
!
titleLabelAnimating
{
hideTitleLabel
()
/// Layout the clearFlatButton.
public
func
layoutClearFlatButton
()
{
if
let
v
:
FlatButton
=
clearFlatButton
{
if
0
<
width
&&
0
<
height
{
v
.
frame
=
CGRectMake
(
width
-
height
,
0
,
height
,
height
)
}
}
titleLabel
.
textColor
=
titleLabelColor
lineLayer
.
frame
.
size
.
height
=
lineLayerThickness
lineLayer
.
backgroundColor
=
(
detailLabelHidden
?
nil
==
lineLayerColor
?
titleLabelColor
:
lineLayerColor
:
nil
==
lineLayerDetailColor
?
detailLabelActiveColor
:
lineLayerDetailColor
)?
.
CGColor
}
/// Sets the shadow path.
internal
func
layoutShadowPath
()
{
if
shadowPathAutoSizeEnabled
{
if
.
None
==
depth
{
shadowPath
=
nil
}
else
if
nil
==
shadowPath
{
shadowPath
=
UIBezierPath
(
roundedRect
:
bounds
,
cornerRadius
:
cornerRadius
)
.
CGPath
}
else
{
animate
(
MaterialAnimation
.
shadowPath
(
UIBezierPath
(
roundedRect
:
bounds
,
cornerRadius
:
cornerRadius
)
.
CGPath
,
duration
:
0
))
/// Layout the visibilityFlatButton.
public
func
layoutVisibilityFlatButton
()
{
if
let
v
:
FlatButton
=
visibilityFlatButton
{
if
0
<
width
&&
0
<
height
{
v
.
frame
=
CGRectMake
(
width
-
height
,
0
,
height
,
height
)
}
}
}
/// Prepares the titleLabel.
private
func
prepareTitleLabel
()
{
titleLabel
=
UILabel
(
frame
:
CGRectZero
)
titleLabel
.
hidden
=
true
titleLabel
.
font
=
RobotoFont
.
mediumWithSize
(
12
)
addSubview
(
titleLabel
)
titleLabelColor
=
placeholderTextColor
titleLabelActiveColor
=
MaterialColor
.
blue
.
accent3
if
false
==
text
?
.
isEmpty
{
showTitleLabel
()
}
/// The animation for the divider when editing begins.
public
func
dividerEditingDidBeginAnimation
()
{
divider
.
frame
.
size
.
height
=
dividerActiveHeight
divider
.
backgroundColor
=
nil
==
dividerActiveColor
?
placeholderActiveColor
.
CGColor
:
dividerActiveColor
!.
CGColor
}
/// Prepares the detailLabel.
private
func
prepareDetailLabel
()
{
detailLabel
=
UILabel
(
frame
:
CGRectZero
)
detailLabel
.
hidden
=
true
addSubview
(
detailLabel
)
if
detailLabelHidden
{
detailLabel
.
alpha
=
0
}
else
{
showDetailLabel
()
}
/// The animation for the divider when editing ends.
public
func
dividerEditingDidEndAnimation
()
{
divider
.
frame
.
size
.
height
=
dividerHeight
divider
.
backgroundColor
=
dividerColor
.
CGColor
}
/// Prepares the lineLayer.
private
func
prepareLineLayer
()
{
layoutLineLayer
()
layer
.
addSublayer
(
lineLayer
)
/// The animation for the placeholder when editing begins.
public
func
placeholderEditingDidBeginAnimation
()
{
if
CGAffineTransformIsIdentity
(
placeholderLabel
.
transform
)
{
animating
=
true
UIView
.
animateWithDuration
(
0.15
,
animations
:
{
[
weak
self
]
in
if
let
v
:
TextField
=
self
{
v
.
placeholderLabel
.
transform
=
CGAffineTransformMakeScale
(
0.75
,
0.75
)
switch
v
.
textAlignment
{
case
.
Left
,
.
Natural
:
v
.
placeholderLabel
.
frame
.
origin
.
x
=
0
case
.
Right
:
v
.
placeholderLabel
.
frame
.
origin
.
x
=
v
.
width
-
v
.
placeholderLabel
.
frame
.
width
default
:
break
}
v
.
placeholderLabel
.
frame
.
origin
.
y
=
-
v
.
placeholderLabel
.
frame
.
size
.
height
v
.
placeholderLabel
.
textColor
=
v
.
placeholderActiveColor
}
})
{
[
weak
self
]
_
in
self
?
.
animating
=
false
}
}
else
if
editing
{
placeholderLabel
.
textColor
=
placeholderActiveColor
}
}
/// Layout the lineLayer.
private
func
layoutLineLayer
()
{
let
h
:
CGFloat
=
lineLayerActiveThickness
==
lineLayer
.
frame
.
height
?
lineLayerActiveThickness
:
lineLayerThickness
lineLayer
.
frame
=
CGRectMake
(
0
,
bounds
.
height
+
lineLayerDistance
,
bounds
.
width
,
h
)
/// The animation for the placeholder when editing ends.
public
func
placeholderEditingDidEndAnimation
()
{
if
!
CGAffineTransformIsIdentity
(
placeholderLabel
.
transform
)
&&
true
==
text
?
.
isEmpty
{
animating
=
true
UIView
.
animateWithDuration
(
0.15
,
animations
:
{
[
weak
self
]
in
if
let
v
:
TextField
=
self
{
v
.
placeholderLabel
.
transform
=
CGAffineTransformIdentity
v
.
placeholderLabel
.
frame
.
origin
.
x
=
0
v
.
placeholderLabel
.
frame
.
origin
.
y
=
0
v
.
placeholderLabel
.
textColor
=
v
.
placeholderColor
}
})
{
[
weak
self
]
_
in
self
?
.
animating
=
false
}
}
else
if
!
editing
{
placeholderLabel
.
textColor
=
placeholderColor
}
}
/// Prepares the clearButton.
private
func
prepareClearButton
()
{
let
image
:
UIImage
?
=
MaterialIcon
.
cm
.
close
clearButton
=
FlatButton
()
clearButton
.
contentEdgeInsets
=
UIEdgeInsetsZero
clearButton
.
pulseColor
=
MaterialColor
.
black
clearButton
.
pulseOpacity
=
0.12
clearButton
.
pulseScale
=
false
clearButton
.
tintColor
=
placeholderTextColor
clearButton
.
setImage
(
image
,
forState
:
.
Normal
)
clearButton
.
setImage
(
image
,
forState
:
.
Highlighted
)
clearButtonAutoHandleEnabled
=
true
clearButtonMode
=
.
Never
rightViewMode
=
.
WhileEditing
rightView
=
clearButton
/// Prepares the divider.
private
func
prepareDivider
()
{
dividerColor
=
MaterialColor
.
darkText
.
dividers
layer
.
addSublayer
(
divider
)
}
/// Layout the clearButton.
private
func
layoutClearButton
()
{
if
0
<
width
&&
0
<
height
{
// clearButton.frame = CGRectMake(width - height, 0, height, height)
}
/// Prepares the placeholderLabel.
private
func
preparePlaceholderLabel
()
{
placeholderColor
=
MaterialColor
.
darkText
.
others
addSubview
(
placeholderLabel
)
}
/// Shows and animates the titleLabel property.
private
func
showTitleLabel
()
{
if
titleLabel
.
hidden
{
let
h
:
CGFloat
=
ceil
(
titleLabel
.
font
.
lineHeight
)
titleLabel
.
frame
=
bounds
titleLabel
.
font
=
font
titleLabel
.
text
=
placeholderText
titleLabel
.
hidden
=
false
UIView
.
animateWithDuration
(
0.15
,
animations
:
{
[
weak
self
]
in
if
let
s
:
TextField
=
self
{
s
.
titleLabel
.
transform
=
CGAffineTransformScale
(
s
.
titleLabel
.
transform
,
0.75
,
0.75
)
s
.
titleLabel
.
frame
=
CGRectMake
(
0
,
-
(
s
.
titleLabelAnimationDistance
+
h
),
s
.
bounds
.
width
,
h
)
}
})
}
}
/// Hides and animates the titleLabel property.
private
func
hideTitleLabel
()
{
titleLabelAnimating
=
true
UIView
.
animateWithDuration
(
0.15
,
animations
:
{
[
weak
self
]
in
if
let
s
:
TextField
=
self
{
s
.
titleLabel
.
transform
=
CGAffineTransformIdentity
s
.
titleLabel
.
frame
=
s
.
bounds
}
})
{
[
weak
self
]
_
in
if
let
s
:
TextField
=
self
{
s
.
titleLabel
.
hidden
=
true
s
.
placeholder
=
s
.
placeholderText
s
.
titleLabelAnimating
=
false
}
}
}
/// Shows and animates the detailLabel property.
private
func
showDetailLabel
()
{
if
detailLabel
.
hidden
{
let
h
:
CGFloat
=
ceil
(
detailLabel
.
font
.
lineHeight
)
detailLabel
.
frame
=
CGRectMake
(
0
,
bounds
.
height
+
lineLayerDistance
,
bounds
.
width
,
h
)
detailLabel
.
hidden
=
false
UIView
.
animateWithDuration
(
0.15
,
animations
:
{
[
weak
self
]
in
if
let
s
:
TextField
=
self
{
s
.
detailLabel
.
frame
.
origin
.
y
=
s
.
frame
.
height
+
s
.
lineLayerDistance
+
s
.
detailLabelAnimationDistance
s
.
detailLabel
.
alpha
=
1
}
})
}
/// Prepares the detailLabel.
private
func
prepareDetailLabel
()
{
detailLabel
.
font
=
RobotoFont
.
regularWithSize
(
12
)
detailColor
=
MaterialColor
.
darkText
.
others
addSubview
(
detailLabel
)
}
/// Hides and animates the detailLabel property.
private
func
hideDetailLabel
()
{
UIView
.
animateWithDuration
(
0.15
,
animations
:
{
[
weak
self
]
in
if
let
s
:
TextField
=
self
{
s
.
detailLabel
.
alpha
=
0
s
.
detailLabel
.
frame
.
origin
.
y
-=
s
.
detailLabelAnimationDistance
}
})
{
[
weak
self
]
_
in
if
let
s
:
TextField
=
self
{
s
.
detailLabel
.
hidden
=
true
}
}
/// Prepares the target handlers.
private
func
prepareTargetHandlers
()
{
addTarget
(
self
,
action
:
#selector(
handleEditingDidBegin
)
,
forControlEvents
:
.
EditingDidBegin
)
addTarget
(
self
,
action
:
#selector(
handleEditingDidEnd
)
,
forControlEvents
:
.
EditingDidEnd
)
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment