Commit cb996727 by Daniel Dahan

added visbility and clear button without conflict to iOS internal clearButton

parent f946055a
...@@ -71,7 +71,7 @@ class ViewController: UIViewController { ...@@ -71,7 +71,7 @@ class ViewController: UIViewController {
icons.append(Item(name: "arrowDownward", mdIcon: MaterialIcon.arrowDownward, cmIcon: MaterialIcon.cm.arrowDownward)) icons.append(Item(name: "arrowDownward", mdIcon: MaterialIcon.arrowDownward, cmIcon: MaterialIcon.cm.arrowDownward))
icons.append(Item(name: "audio", mdIcon: MaterialIcon.audio, cmIcon: MaterialIcon.cm.audio)) icons.append(Item(name: "audio", mdIcon: MaterialIcon.audio, cmIcon: MaterialIcon.cm.audio))
icons.append(Item(name: "bell", mdIcon: nil, cmIcon: MaterialIcon.cm.bell)) icons.append(Item(name: "bell", mdIcon: nil, cmIcon: MaterialIcon.cm.bell))
icons.append(Item(name: "check", mdIcon: nil, cmIcon: MaterialIcon.cm.check)) icons.append(Item(name: "check", mdIcon: MaterialIcon.check, cmIcon: MaterialIcon.cm.check))
icons.append(Item(name: "clear", mdIcon: MaterialIcon.clear, cmIcon: MaterialIcon.cm.clear)) icons.append(Item(name: "clear", mdIcon: MaterialIcon.clear, cmIcon: MaterialIcon.cm.clear))
icons.append(Item(name: "close", mdIcon: MaterialIcon.close, cmIcon: MaterialIcon.cm.close)) icons.append(Item(name: "close", mdIcon: MaterialIcon.close, cmIcon: MaterialIcon.cm.close))
icons.append(Item(name: "edit", mdIcon: MaterialIcon.edit, cmIcon: MaterialIcon.cm.edit)) icons.append(Item(name: "edit", mdIcon: MaterialIcon.edit, cmIcon: MaterialIcon.cm.edit))
...@@ -98,6 +98,7 @@ class ViewController: UIViewController { ...@@ -98,6 +98,7 @@ class ViewController: UIViewController {
icons.append(Item(name: "starBorder", mdIcon: MaterialIcon.starBorder, cmIcon: nil)) icons.append(Item(name: "starBorder", mdIcon: MaterialIcon.starBorder, cmIcon: nil))
icons.append(Item(name: "starHalf", mdIcon: MaterialIcon.starHalf, cmIcon: nil)) icons.append(Item(name: "starHalf", mdIcon: MaterialIcon.starHalf, cmIcon: nil))
icons.append(Item(name: "videocam", mdIcon: MaterialIcon.videocam, cmIcon: MaterialIcon.cm.videocam)) icons.append(Item(name: "videocam", mdIcon: MaterialIcon.videocam, cmIcon: MaterialIcon.cm.videocam))
icons.append(Item(name: "visibility", mdIcon: MaterialIcon.visibility, cmIcon: nil))
icons.append(Item(name: "volumeHigh", mdIcon: nil, cmIcon: MaterialIcon.cm.volumeHigh)) icons.append(Item(name: "volumeHigh", mdIcon: nil, cmIcon: MaterialIcon.cm.volumeHigh))
icons.append(Item(name: "volumeMedium", mdIcon: nil, cmIcon: MaterialIcon.cm.volumeMedium)) icons.append(Item(name: "volumeMedium", mdIcon: nil, cmIcon: MaterialIcon.cm.volumeMedium))
icons.append(Item(name: "volumeOff", mdIcon: nil, cmIcon: MaterialIcon.cm.volumeOff)) icons.append(Item(name: "volumeOff", mdIcon: nil, cmIcon: MaterialIcon.cm.volumeOff))
......
...@@ -109,7 +109,7 @@ class ViewController: UIViewController, TextFieldDelegate { ...@@ -109,7 +109,7 @@ class ViewController: UIViewController, TextFieldDelegate {
emailField = MTextField(frame: CGRectMake(40, 120, view.bounds.width - 80, 32)) emailField = MTextField(frame: CGRectMake(40, 120, view.bounds.width - 80, 32))
emailField.placeholder = "Email" emailField.placeholder = "Email"
emailField.detail = "Error, incorrect email" emailField.detail = "Error, incorrect email"
emailField.clearButtonMode = .WhileEditing emailField.enableClearFlatButton = true
emailField.delegate = self emailField.delegate = self
emailField.placeholderColor = MaterialColor.amber.darken4 emailField.placeholderColor = MaterialColor.amber.darken4
...@@ -126,8 +126,7 @@ class ViewController: UIViewController, TextFieldDelegate { ...@@ -126,8 +126,7 @@ class ViewController: UIViewController, TextFieldDelegate {
passwordField.placeholder = "Password" passwordField.placeholder = "Password"
passwordField.detail = "At least 8 characters" passwordField.detail = "At least 8 characters"
passwordField.clearButtonMode = .WhileEditing passwordField.clearButtonMode = .WhileEditing
passwordField.textAlignment = .Right passwordField.enableVisibilityFlatButton = true
passwordField.secureTextEntry = true
passwordField.delegate = self passwordField.delegate = self
// The translatesAutoresizingMaskIntoConstraints property must be set to enable AutoLayout correctly. // The translatesAutoresizingMaskIntoConstraints property must be set to enable AutoLayout correctly.
......
{
"images" : [
{
"idiom" : "universal",
"filename" : "ic_check_white.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "ic_check_white_2x.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "ic_check_white_3x.png",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
\ No newline at end of file
{
"images" : [
{
"idiom" : "universal",
"filename" : "ic_visibility_white.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "ic_visibility_white_2x.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "ic_visibility_white_3x.png",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
\ No newline at end of file
...@@ -194,6 +194,7 @@ public class MTextField : UITextField { ...@@ -194,6 +194,7 @@ public class MTextField : UITextField {
placeholderLabel.attributedText = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: placeholderActiveColor]) placeholderLabel.attributedText = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: placeholderActiveColor])
} }
} }
tintColor = placeholderActiveColor
} }
} }
...@@ -236,6 +237,87 @@ public class MTextField : UITextField { ...@@ -236,6 +237,87 @@ public class MTextField : UITextField {
} }
} }
/// 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. An initializer that initializes the object with a NSCoder object.
- Parameter aDecoder: A NSCoder instance. - Parameter aDecoder: A NSCoder instance.
...@@ -344,6 +426,20 @@ public class MTextField : UITextField { ...@@ -344,6 +426,20 @@ public class MTextField : UITextField {
placeholderEditingDidEndAnimation() 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, Prepares the view instance when intialized. When subclassing,
it is recommended to override the prepareView method it is recommended to override the prepareView method
...@@ -368,6 +464,8 @@ public class MTextField : UITextField { ...@@ -368,6 +464,8 @@ public class MTextField : UITextField {
if !animating { if !animating {
layoutPlaceholderLabel() layoutPlaceholderLabel()
layoutDetailLabel() layoutDetailLabel()
layoutClearFlatButton()
layoutVisibilityFlatButton()
} }
} }
...@@ -412,6 +510,24 @@ public class MTextField : UITextField { ...@@ -412,6 +510,24 @@ public class MTextField : UITextField {
detailLabel.frame = CGRectMake(0, divider.frame.origin.y + 8, width, h) 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. /// The animation for the divider when editing begins.
public func dividerEditingDidBeginAnimation() { public func dividerEditingDidBeginAnimation() {
divider.frame.size.height = dividerActiveHeight divider.frame.size.height = dividerActiveHeight
......
...@@ -62,6 +62,7 @@ public struct MaterialIcon { ...@@ -62,6 +62,7 @@ public struct MaterialIcon {
public static let arrowDownward: UIImage? = MaterialIcon.icon("ic_arrow_downward_white") public static let arrowDownward: UIImage? = MaterialIcon.icon("ic_arrow_downward_white")
public static let audio: UIImage? = MaterialIcon.icon("ic_audiotrack_white") public static let audio: UIImage? = MaterialIcon.icon("ic_audiotrack_white")
public static let bell: UIImage? = MaterialIcon.icon("cm_bell_white") public static let bell: UIImage? = MaterialIcon.icon("cm_bell_white")
public static let check: UIImage? = MaterialIcon.icon("ic_check_white")
public static let clear: UIImage? = MaterialIcon.icon("ic_close_white") public static let clear: UIImage? = MaterialIcon.icon("ic_close_white")
public static let close: UIImage? = MaterialIcon.icon("ic_close_white") public static let close: UIImage? = MaterialIcon.icon("ic_close_white")
public static let edit: UIImage? = MaterialIcon.icon("ic_edit_white") public static let edit: UIImage? = MaterialIcon.icon("ic_edit_white")
...@@ -82,7 +83,7 @@ public struct MaterialIcon { ...@@ -82,7 +83,7 @@ public struct MaterialIcon {
public static let starBorder: UIImage? = MaterialIcon.icon("ic_star_border_white") public static let starBorder: UIImage? = MaterialIcon.icon("ic_star_border_white")
public static let starHalf: UIImage? = MaterialIcon.icon("ic_star_half_white") public static let starHalf: UIImage? = MaterialIcon.icon("ic_star_half_white")
public static let videocam: UIImage? = MaterialIcon.icon("ic_videocam_white") public static let videocam: UIImage? = MaterialIcon.icon("ic_videocam_white")
public static let video: UIImage? = MaterialIcon.icon("cm_video_white") public static let visibility: UIImage? = MaterialIcon.icon("ic_visibility_white")
/// CosmicMind icons. /// CosmicMind icons.
public struct cm { public struct cm {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment