Commit f172f0fc by Orkhan Alikhanov Committed by OrkhanAlikhanov

Added TextField.textInsets and fixed placeholder layout issues

parent 8a9d0902
...@@ -420,13 +420,17 @@ open class TextField: UITextField { ...@@ -420,13 +420,17 @@ open class TextField: UITextField {
/// EdgeInsets for text. /// EdgeInsets for text.
@objc @objc
open var textInset: CGFloat = 0 open var textInsets: EdgeInsets = .zero
/// EdgeInsets preset property for text.
open var textInsetsPreset = EdgeInsetsPreset.none {
didSet {
textInsets = EdgeInsetsPresetToValue(preset: textInsetsPreset)
}
}
open override func textRect(forBounds bounds: CGRect) -> CGRect { open override func textRect(forBounds bounds: CGRect) -> CGRect {
var b = super.textRect(forBounds: bounds) return super.textRect(forBounds: bounds).inset(by: textInsets)
b.origin.x += textInset
b.size.width -= textInset
return b
} }
open override func editingRect(forBounds bounds: CGRect) -> CGRect { open override func editingRect(forBounds bounds: CGRect) -> CGRect {
...@@ -573,26 +577,27 @@ fileprivate extension TextField { ...@@ -573,26 +577,27 @@ fileprivate extension TextField {
fileprivate extension TextField { fileprivate extension TextField {
/// Layout the placeholderLabel. /// Layout the placeholderLabel.
func layoutPlaceholderLabel() { func layoutPlaceholderLabel() {
let x = leftViewWidth + textInset let leftPadding = leftViewWidth + textInsets.left
let h = 0 == bounds.height ? intrinsicContentSize.height : bounds.height let w = bounds.width - leftPadding - textInsets.right
let w = bounds.width - leftViewWidth - 2 * textInset var h = placeholderLabel.sizeThatFits(CGSize(width: w, height: .greatestFiniteMagnitude)).height
placeholderLabel.frame.size = CGSize(width: w, height: h) h = min(h, bounds.height - textInsets.top - textInsets.bottom)
guard isEditing || !isEmpty || !isPlaceholderAnimated else { guard isEditing || !isEmpty || !isPlaceholderAnimated else {
placeholderLabel.transform = CGAffineTransform.identity placeholderLabel.transform = CGAffineTransform.identity
placeholderLabel.frame.origin = CGPoint(x: x, y: 0) placeholderLabel.frame = CGRect(x: leftPadding, y: textInsets.top, width: w, height: h)
return return
} }
placeholderLabel.transform = CGAffineTransform(scaleX: placeholderActiveScale, y: placeholderActiveScale) placeholderLabel.transform = CGAffineTransform(scaleX: placeholderActiveScale, y: placeholderActiveScale)
placeholderLabel.frame.size = CGSize(width: w * placeholderActiveScale, height: h * placeholderActiveScale)
placeholderLabel.frame.origin.y = -placeholderLabel.frame.height + placeholderVerticalOffset placeholderLabel.frame.origin.y = -placeholderLabel.frame.height + placeholderVerticalOffset
switch textAlignment { switch placeholderLabel.textAlignment {
case .left, .natural: case .left, .natural:
placeholderLabel.frame.origin.x = x + placeholderHorizontalOffset placeholderLabel.frame.origin.x = leftPadding + placeholderHorizontalOffset
case .right: case .right:
placeholderLabel.frame.origin.x = (bounds.width * (1.0 - placeholderActiveScale)) - textInset + placeholderHorizontalOffset let scaledWidth = w * placeholderActiveScale
placeholderLabel.frame.origin.x = bounds.width - scaledWidth - textInsets.right + placeholderHorizontalOffset
default:break default:break
} }
} }
......
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