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
e031dee6
Unverified
Commit
e031dee6
authored
Aug 03, 2018
by
Daniel Dahan
Committed by
GitHub
Aug 03, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1126 from OrkhanAlikhanov/clean-up
Cleaned up TextField
parents
0a98c755
101dfcb4
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
51 additions
and
96 deletions
+51
-96
Sources/iOS/TextField.swift
+51
-96
No files found.
Sources/iOS/TextField.swift
View file @
e031dee6
...
@@ -76,12 +76,7 @@ open class TextField: UITextField {
...
@@ -76,12 +76,7 @@ open class TextField: UITextField {
/// Set the placeholder animation value.
/// Set the placeholder animation value.
open
var
placeholderAnimation
=
TextFieldPlaceholderAnimation
.
default
{
open
var
placeholderAnimation
=
TextFieldPlaceholderAnimation
.
default
{
didSet
{
didSet
{
guard
isEditing
else
{
updatePlaceholderVisibility
()
placeholderLabel
.
isHidden
=
!
isEmpty
&&
.
hidden
==
placeholderAnimation
return
}
placeholderLabel
.
isHidden
=
.
hidden
==
placeholderAnimation
}
}
}
}
...
@@ -92,7 +87,7 @@ open class TextField: UITextField {
...
@@ -92,7 +87,7 @@ open class TextField: UITextField {
open
override
var
text
:
String
?
{
open
override
var
text
:
String
?
{
didSet
{
didSet
{
placeholderAnimation
=
{
placeholderAnimation
}
()
updatePlaceholderVisibility
()
}
}
}
}
...
@@ -135,24 +130,15 @@ open class TextField: UITextField {
...
@@ -135,24 +130,15 @@ open class TextField: UITextField {
@IBInspectable
@IBInspectable
open
var
dividerNormalHeight
:
CGFloat
=
1
{
open
var
dividerNormalHeight
:
CGFloat
=
1
{
didSet
{
didSet
{
guard
!
isEditing
else
{
updateDividerHeight
()
return
}
dividerThickness
=
dividerNormalHeight
}
}
}
}
/// Divider active height.
/// Divider active height.
@IBInspectable
@IBInspectable
open
var
dividerActiveHeight
:
CGFloat
=
2
{
open
var
dividerActiveHeight
:
CGFloat
=
2
{
didSet
{
didSet
{
guard
isEditing
else
{
updateDividerHeight
()
return
}
dividerThickness
=
dividerActiveHeight
}
}
}
}
...
@@ -160,11 +146,7 @@ open class TextField: UITextField {
...
@@ -160,11 +146,7 @@ open class TextField: UITextField {
@IBInspectable
@IBInspectable
open
var
dividerNormalColor
=
Color
.
grey
.
lighten2
{
open
var
dividerNormalColor
=
Color
.
grey
.
lighten2
{
didSet
{
didSet
{
guard
!
isEditing
else
{
updateDividerColor
()
return
}
dividerColor
=
dividerNormalColor
}
}
}
}
...
@@ -172,11 +154,7 @@ open class TextField: UITextField {
...
@@ -172,11 +154,7 @@ open class TextField: UITextField {
@IBInspectable
@IBInspectable
open
var
dividerActiveColor
=
Color
.
blue
.
base
{
open
var
dividerActiveColor
=
Color
.
blue
.
base
{
didSet
{
didSet
{
guard
isEditing
else
{
updateDividerColor
()
return
}
dividerColor
=
dividerActiveColor
}
}
}
}
...
@@ -540,6 +518,26 @@ fileprivate extension TextField {
...
@@ -540,6 +518,26 @@ fileprivate extension TextField {
placeholderLabel
.
textColor
=
isEditing
?
placeholderActiveColor
:
placeholderNormalColor
placeholderLabel
.
textColor
=
isEditing
?
placeholderActiveColor
:
placeholderNormalColor
}
}
/// Updates the placeholder visibility.
func
updatePlaceholderVisibility
()
{
guard
isEditing
else
{
placeholderLabel
.
isHidden
=
!
isEmpty
&&
.
hidden
==
placeholderAnimation
return
}
placeholderLabel
.
isHidden
=
.
hidden
==
placeholderAnimation
}
/// Updates the dividerColor.
func
updateDividerColor
()
{
dividerColor
=
isEditing
?
dividerActiveColor
:
dividerNormalColor
}
/// Updates the dividerThickness.
func
updateDividerHeight
()
{
dividerThickness
=
isEditing
?
dividerActiveHeight
:
dividerNormalHeight
}
/// Update the placeholder text to the active state.
/// Update the placeholder text to the active state.
func
updatePlaceholderTextToActiveState
()
{
func
updatePlaceholderTextToActiveState
()
{
guard
isPlaceholderUppercasedWhenEditing
else
{
guard
isPlaceholderUppercasedWhenEditing
else
{
...
@@ -575,28 +573,28 @@ fileprivate extension TextField {
...
@@ -575,28 +573,28 @@ fileprivate extension TextField {
fileprivate
extension
TextField
{
fileprivate
extension
TextField
{
/// Layout the placeholderLabel.
/// Layout the placeholderLabel.
func
layoutPlaceholderLabel
()
{
func
layoutPlaceholderLabel
()
{
let
w
=
leftViewWidth
+
textInset
let
x
=
leftViewWidth
+
textInset
let
h
=
0
==
bounds
.
height
?
intrinsicContentSize
.
height
:
bounds
.
height
let
h
=
0
==
bounds
.
height
?
intrinsicContentSize
.
height
:
bounds
.
height
let
w
=
bounds
.
width
-
leftViewWidth
-
2
*
textInset
placeholderLabel
.
transform
=
CGAffineTransform
.
identity
placeholderLabel
.
frame
.
size
=
CGSize
(
width
:
w
,
height
:
h
)
guard
isEditing
||
!
isEmpty
||
!
isPlaceholderAnimated
else
{
guard
isEditing
||
!
isEmpty
||
!
isPlaceholderAnimated
else
{
placeholderLabel
.
frame
=
CGRect
(
x
:
w
,
y
:
0
,
width
:
bounds
.
width
-
leftViewWidth
-
2
*
textInset
,
height
:
h
)
placeholderLabel
.
transform
=
CGAffineTransform
.
identity
placeholderLabel
.
frame
.
origin
=
CGPoint
(
x
:
x
,
y
:
0
)
return
return
}
}
placeholderLabel
.
frame
=
CGRect
(
x
:
w
,
y
:
0
,
width
:
bounds
.
width
-
leftViewWidth
-
2
*
textInset
,
height
:
h
)
placeholderLabel
.
transform
=
CGAffineTransform
(
scaleX
:
placeholderActiveScale
,
y
:
placeholderActiveScale
)
placeholderLabel
.
transform
=
CGAffineTransform
(
scaleX
:
placeholderActiveScale
,
y
:
placeholderActiveScale
)
placeholderLabel
.
frame
.
origin
.
y
=
-
placeholderLabel
.
frame
.
height
+
placeholderVerticalOffset
switch
textAlignment
{
switch
textAlignment
{
case
.
left
,
.
natural
:
case
.
left
,
.
natural
:
placeholderLabel
.
frame
.
origin
.
x
=
w
+
placeholderHorizontalOffset
placeholderLabel
.
frame
.
origin
.
x
=
x
+
placeholderHorizontalOffset
case
.
right
:
case
.
right
:
placeholderLabel
.
frame
.
origin
.
x
=
(
bounds
.
width
*
(
1.0
-
placeholderActiveScale
))
-
textInset
+
placeholderHorizontalOffset
placeholderLabel
.
frame
.
origin
.
x
=
(
bounds
.
width
*
(
1.0
-
placeholderActiveScale
))
-
textInset
+
placeholderHorizontalOffset
default
:
break
default
:
break
}
}
placeholderLabel
.
frame
.
origin
.
y
=
-
placeholderLabel
.
frame
.
height
+
placeholderVerticalOffset
}
}
/// Layout the leftView.
/// Layout the leftView.
...
@@ -688,96 +686,53 @@ fileprivate extension TextField {
...
@@ -688,96 +686,53 @@ fileprivate extension TextField {
}
}
}
}
extension
TextField
{
private
extension
TextField
{
/// The animation for leftView when editing begins.
/// The animation for leftView when editing begins.
f
ileprivate
f
unc
leftViewEditingBeginAnimation
()
{
func
leftViewEditingBeginAnimation
()
{
updateLeftViewColor
()
updateLeftViewColor
()
}
}
/// The animation for leftView when editing ends.
/// The animation for leftView when editing ends.
f
ileprivate
f
unc
leftViewEditingEndAnimation
()
{
func
leftViewEditingEndAnimation
()
{
updateLeftViewColor
()
updateLeftViewColor
()
}
}
/// The animation for the divider when editing begins.
/// The animation for the divider when editing begins.
f
ileprivate
f
unc
dividerEditingDidBeginAnimation
()
{
func
dividerEditingDidBeginAnimation
()
{
dividerThickness
=
dividerActiveHeight
updateDividerHeight
()
dividerColor
=
dividerActiveColor
updateDividerColor
()
}
}
/// The animation for the divider when editing ends.
/// The animation for the divider when editing ends.
f
ileprivate
f
unc
dividerEditingDidEndAnimation
()
{
func
dividerEditingDidEndAnimation
()
{
dividerThickness
=
dividerNormalHeight
updateDividerHeight
()
dividerColor
=
dividerNormalColor
updateDividerColor
()
}
}
/// The animation for the placeholder when editing begins.
/// The animation for the placeholder when editing begins.
fileprivate
func
placeholderEditingDidBeginAnimation
()
{
func
placeholderEditingDidBeginAnimation
()
{
guard
.
default
==
placeholderAnimation
else
{
updatePlaceholderVisibility
()
placeholderLabel
.
isHidden
=
true
return
}
updatePlaceholderLabelColor
()
updatePlaceholderLabelColor
()
guard
isPlaceholderAnimated
else
{
guard
isPlaceholderAnimated
else
{
updatePlaceholderTextToActiveState
()
return
}
guard
isEmpty
else
{
updatePlaceholderTextToActiveState
()
return
return
}
}
UIView
.
animate
(
withDuration
:
0.15
,
animations
:
{
[
weak
self
]
in
updatePlaceholderTextToActiveState
()
guard
let
`
self
`
=
self
else
{
UIView
.
animate
(
withDuration
:
0.15
,
animations
:
layoutPlaceholderLabel
)
return
}
self
.
placeholderLabel
.
transform
=
CGAffineTransform
(
scaleX
:
self
.
placeholderActiveScale
,
y
:
self
.
placeholderActiveScale
)
self
.
updatePlaceholderTextToActiveState
()
switch
self
.
textAlignment
{
case
.
left
,
.
natural
:
self
.
placeholderLabel
.
frame
.
origin
.
x
=
self
.
leftViewWidth
+
self
.
textInset
+
self
.
placeholderHorizontalOffset
case
.
right
:
self
.
placeholderLabel
.
frame
.
origin
.
x
=
(
self
.
bounds
.
width
*
(
1.0
-
self
.
placeholderActiveScale
))
-
self
.
textInset
+
self
.
placeholderHorizontalOffset
default
:
break
}
self
.
placeholderLabel
.
frame
.
origin
.
y
=
-
self
.
placeholderLabel
.
bounds
.
height
+
self
.
placeholderVerticalOffset
})
}
}
/// The animation for the placeholder when editing ends.
/// The animation for the placeholder when editing ends.
fileprivate
func
placeholderEditingDidEndAnimation
()
{
func
placeholderEditingDidEndAnimation
()
{
guard
.
default
==
placeholderAnimation
else
{
updatePlaceholderVisibility
()
placeholderLabel
.
isHidden
=
!
isEmpty
return
}
updatePlaceholderLabelColor
()
updatePlaceholderLabelColor
()
updatePlaceholderTextToNormalState
()
guard
isPlaceholderAnimated
else
{
guard
isPlaceholderAnimated
else
{
return
return
}
}
guard
isEmpty
else
{
updatePlaceholderTextToNormalState
()
return
UIView
.
animate
(
withDuration
:
0.15
,
animations
:
layoutPlaceholderLabel
)
}
UIView
.
animate
(
withDuration
:
0.15
,
animations
:
{
[
weak
self
]
in
guard
let
`
self
`
=
self
else
{
return
}
self
.
placeholderLabel
.
transform
=
CGAffineTransform
.
identity
self
.
placeholderLabel
.
frame
.
origin
.
x
=
self
.
leftViewWidth
+
self
.
textInset
self
.
placeholderLabel
.
frame
.
origin
.
y
=
0
})
}
}
}
}
...
...
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