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
ae240903
Commit
ae240903
authored
Apr 30, 2016
by
Daniel Dahan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
MTextField alignment now supports resizing
parent
82a26399
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
63 additions
and
23 deletions
+63
-23
Examples/Programmatic/TextField/TextField/ViewController.swift
+7
-10
Sources/iOS/MTextField.swift
+56
-13
No files found.
Examples/Programmatic/TextField/TextField/ViewController.swift
View file @
ae240903
...
...
@@ -80,18 +80,19 @@ class ViewController: UIViewController, TextFieldDelegate {
internal
func
handleResignResponderButton
()
{
nameField
?
.
resignFirstResponder
()
emailField
?
.
resignFirstResponder
()
passwordField
?
.
resignFirstResponder
()
}
/// Prepares the name TextField.
private
func
prepareNameField
()
{
nameField
=
MTextField
()
nameField
.
text
=
"Daniel Dahan"
nameField
.
placeholder
=
"Name"
nameField
.
detail
=
"Your given name"
nameField
.
clearButtonMode
=
.
WhileEditing
nameField
.
textAlignment
=
.
Center
nameField
.
text
=
"Daniel Dahan"
nameField
.
backgroundColor
=
MaterialColor
.
green
.
accent1
nameField
.
placeholderLabel
.
backgroundColor
=
MaterialColor
.
green
.
accent3
nameField
.
clearButtonMode
=
.
WhileEditing
nameField
.
dividerActiveColor
=
MaterialColor
.
green
.
base
nameField
.
delegate
=
self
// The translatesAutoresizingMaskIntoConstraints property must be set to enable AutoLayout correctly.
nameField
.
translatesAutoresizingMaskIntoConstraints
=
false
...
...
@@ -108,17 +109,14 @@ class ViewController: UIViewController, TextFieldDelegate {
emailField
=
MTextField
(
frame
:
CGRectMake
(
40
,
120
,
view
.
bounds
.
width
-
80
,
32
))
emailField
.
placeholder
=
"Email"
emailField
.
detail
=
"Error, incorrect email.yyppggg"
emailField
.
delegate
=
self
emailField
.
clearButtonMode
=
.
WhileEditing
emailField
.
delegate
=
self
emailField
.
placeholderColor
=
MaterialColor
.
amber
.
darken4
emailField
.
placeholderActiveColor
=
MaterialColor
.
pink
.
base
emailField
.
dividerColor
=
MaterialColor
.
cyan
.
base
emailField
.
detailColor
=
MaterialColor
.
indigo
.
accent1
emailField
.
backgroundColor
=
MaterialColor
.
green
.
accent1
emailField
.
placeholderLabel
.
backgroundColor
=
MaterialColor
.
green
.
accent3
view
.
addSubview
(
emailField
)
}
...
...
@@ -130,8 +128,7 @@ class ViewController: UIViewController, TextFieldDelegate {
passwordField
.
clearButtonMode
=
.
WhileEditing
passwordField
.
textAlignment
=
.
Right
passwordField
.
secureTextEntry
=
true
passwordField
.
backgroundColor
=
MaterialColor
.
green
.
accent1
passwordField
.
placeholderLabel
.
backgroundColor
=
MaterialColor
.
green
.
accent3
passwordField
.
delegate
=
self
// The translatesAutoresizingMaskIntoConstraints property must be set to enable AutoLayout correctly.
passwordField
.
translatesAutoresizingMaskIntoConstraints
=
false
...
...
Sources/iOS/MTextField.swift
View file @
ae240903
...
...
@@ -125,7 +125,7 @@ public class MTextField : UITextField {
/// Divider active state height.
@IBInspectable
public
var
dividerActiveHeight
:
CGFloat
=
2
/// Sets the divider
and tintColor
.
/// Sets the divider.
@IBInspectable
public
var
dividerColor
:
UIColor
=
MaterialColor
.
darkText
.
dividers
{
didSet
{
if
!
editing
{
...
...
@@ -134,6 +134,17 @@ public class MTextField : UITextField {
}
}
/// 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
{
...
...
@@ -252,10 +263,7 @@ public class MTextField : UITextField {
public
override
func
layoutSubviews
()
{
super
.
layoutSubviews
()
if
!
editing
&&
!
animating
{
layoutPlaceholderLabel
()
layoutDetailLabel
()
}
layoutToSize
()
}
public
override
func
layoutSublayersOfLayer
(
layer
:
CALayer
)
{
...
...
@@ -355,6 +363,45 @@ public class MTextField : UITextField {
prepareTargetHandlers
()
}
/// Ensures that the components are sized correctly.
public
func
layoutToSize
()
{
if
!
animating
{
if
editing
{
switch
textAlignment
{
case
.
Left
,
.
Natural
:
placeholderLabel
.
frame
.
origin
.
x
=
0
detailLabel
.
frame
.
origin
.
x
=
0
case
.
Right
:
placeholderLabel
.
frame
.
origin
.
x
=
width
-
placeholderLabel
.
frame
.
width
detailLabel
.
frame
.
origin
.
x
=
placeholderLabel
.
frame
.
origin
.
x
case
.
Center
:
placeholderLabel
.
center
.
x
=
width
/
2
detailLabel
.
center
.
x
=
placeholderLabel
.
center
.
x
default
:
break
}
placeholderLabel
.
frame
.
size
.
width
=
width
*
0.75
detailLabel
.
frame
.
size
.
width
=
placeholderLabel
.
frame
.
width
}
else
{
layoutPlaceholderLabel
()
layoutDetailLabel
()
switch
textAlignment
{
case
.
Left
,
.
Natural
:
placeholderLabel
.
frame
.
origin
.
x
=
0
detailLabel
.
frame
.
origin
.
x
=
0
case
.
Right
:
placeholderLabel
.
frame
.
origin
.
x
=
width
-
placeholderLabel
.
frame
.
width
detailLabel
.
frame
.
origin
.
x
=
placeholderLabel
.
frame
.
origin
.
x
case
.
Center
:
placeholderLabel
.
center
.
x
=
width
/
2
detailLabel
.
center
.
x
=
placeholderLabel
.
center
.
x
default
:
break
}
placeholderLabel
.
frame
.
size
.
width
=
true
==
text
?
.
isEmpty
?
width
:
width
*
0.75
detailLabel
.
frame
.
size
.
width
=
placeholderLabel
.
frame
.
width
}
}
}
/// Layout the divider.
public
func
layoutDivider
()
{
divider
.
frame
=
CGRectMake
(
0
,
height
,
width
,
editing
?
dividerActiveHeight
:
dividerHeight
)
...
...
@@ -372,7 +419,7 @@ public class MTextField : UITextField {
placeholderLabel
.
frame
.
origin
.
x
=
0
case
.
Right
:
placeholderLabel
.
frame
.
origin
.
x
=
width
-
placeholderLabel
.
frame
.
width
default
:
break
;
default
:
break
}
placeholderLabel
.
frame
.
origin
.
y
=
-
placeholderLabel
.
frame
.
size
.
height
placeholderLabel
.
textColor
=
placeholderColor
...
...
@@ -381,18 +428,14 @@ public class MTextField : UITextField {
/// Layout the detailLabel.
public
func
layoutDetailLabel
()
{
var
h
:
CGFloat
=
12
if
let
v
:
String
=
detail
{
let
size
:
CGSize
=
detailLabel
.
font
.
stringSize
(
v
,
constrainedToWidth
:
Double
(
width
))
h
=
size
.
height
}
var
h
:
CGFloat
=
nil
==
detail
?
12
:
detailLabel
.
font
.
stringSize
(
detail
!
,
constrainedToWidth
:
Double
(
width
))
.
height
detailLabel
.
frame
=
CGRectMake
(
0
,
height
+
8
,
width
,
h
)
}
/// The animation for the divider when editing begins.
public
func
dividerEditingDidBeginAnimation
()
{
divider
.
frame
.
size
.
height
=
dividerActiveHeight
divider
.
backgroundColor
=
placeholderActiveColor
.
CGColor
divider
.
backgroundColor
=
nil
==
dividerActiveColor
?
placeholderActiveColor
.
CGColor
:
dividerActiveColor
!
.
CGColor
}
/// The animation for the divider when editing ends.
...
...
@@ -413,7 +456,7 @@ public class MTextField : UITextField {
v
.
placeholderLabel
.
frame
.
origin
.
x
=
0
case
.
Right
:
v
.
placeholderLabel
.
frame
.
origin
.
x
=
v
.
width
-
v
.
placeholderLabel
.
frame
.
width
default
:
break
;
default
:
break
}
v
.
placeholderLabel
.
frame
.
origin
.
y
=
-
v
.
placeholderLabel
.
frame
.
size
.
height
v
.
placeholderLabel
.
textColor
=
v
.
placeholderActiveColor
...
...
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