Commit f946055a by Daniel Dahan

fixed MTextField alignment issues when using resignFirstResponder

parent ae240903
...@@ -108,7 +108,7 @@ class ViewController: UIViewController, TextFieldDelegate { ...@@ -108,7 +108,7 @@ class ViewController: UIViewController, TextFieldDelegate {
private func prepareEmailField() { private func prepareEmailField() {
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.yyppggg" emailField.detail = "Error, incorrect email"
emailField.clearButtonMode = .WhileEditing emailField.clearButtonMode = .WhileEditing
emailField.delegate = self emailField.delegate = self
......
...@@ -366,39 +366,8 @@ public class MTextField : UITextField { ...@@ -366,39 +366,8 @@ public class MTextField : UITextField {
/// Ensures that the components are sized correctly. /// Ensures that the components are sized correctly.
public func layoutToSize() { public func layoutToSize() {
if !animating { if !animating {
if editing { layoutPlaceholderLabel()
switch textAlignment { layoutDetailLabel()
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
}
} }
} }
...@@ -409,7 +378,7 @@ public class MTextField : UITextField { ...@@ -409,7 +378,7 @@ public class MTextField : UITextField {
/// Layout the placeholderLabel. /// Layout the placeholderLabel.
public func layoutPlaceholderLabel() { public func layoutPlaceholderLabel() {
if true == text?.isEmpty { if !editing && true == text?.isEmpty {
placeholderLabel.frame = bounds placeholderLabel.frame = bounds
} else if CGAffineTransformIsIdentity(placeholderLabel.transform) { } else if CGAffineTransformIsIdentity(placeholderLabel.transform) {
placeholderLabel.frame = bounds placeholderLabel.frame = bounds
...@@ -423,13 +392,24 @@ public class MTextField : UITextField { ...@@ -423,13 +392,24 @@ public class MTextField : UITextField {
} }
placeholderLabel.frame.origin.y = -placeholderLabel.frame.size.height placeholderLabel.frame.origin.y = -placeholderLabel.frame.size.height
placeholderLabel.textColor = placeholderColor 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. /// Layout the detailLabel.
public func layoutDetailLabel() { public func layoutDetailLabel() {
var h: CGFloat = nil == detail ? 12 : detailLabel.font.stringSize(detail!, constrainedToWidth: Double(width)).height var h: CGFloat = nil == detail ? 12 : detailLabel.font.stringSize(detail!, constrainedToWidth: Double(width)).height
detailLabel.frame = CGRectMake(0, height + 8, width, h) detailLabel.frame = CGRectMake(0, divider.frame.origin.y + 8, width, h)
} }
/// The animation for the divider when editing begins. /// The animation for the divider when editing begins.
......
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