Commit 101dfcb4 by Orkhan Alikhanov

Removed duplication from TextField

parent 4ceda905
...@@ -573,28 +573,28 @@ fileprivate extension TextField { ...@@ -573,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.
...@@ -711,71 +711,28 @@ private extension TextField { ...@@ -711,71 +711,28 @@ private extension TextField {
/// The animation for the placeholder when editing begins. /// The animation for the placeholder when editing begins.
func placeholderEditingDidBeginAnimation() { func placeholderEditingDidBeginAnimation() {
guard .default == placeholderAnimation else { updatePlaceholderVisibility()
placeholderLabel.isHidden = true
return
}
updatePlaceholderLabelColor() updatePlaceholderLabelColor()
guard isPlaceholderAnimated else { guard isPlaceholderAnimated else {
updatePlaceholderTextToActiveState()
return return
} }
guard isEmpty else { updatePlaceholderTextToActiveState()
updatePlaceholderTextToActiveState() UIView.animate(withDuration: 0.15, animations: layoutPlaceholderLabel)
return
}
UIView.animate(withDuration: 0.15, animations: { [weak self] in
guard let `self` = self else {
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.
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
})
} }
} }
......
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