Commit 4309c160 by Daniel Dahan Committed by GitHub

Merge pull request #662 from slxl/master

TextField inset added
parents 0bac0578 58eca684
...@@ -387,6 +387,17 @@ open class TextField: UITextField { ...@@ -387,6 +387,17 @@ open class TextField: UITextField {
return super.becomeFirstResponder() return super.becomeFirstResponder()
} }
open var textInset: CGFloat = 0
open override func textRect(forBounds bounds: CGRect) -> CGRect {
return bounds.insetBy(dx: textInset, dy: 0)
}
open override func editingRect(forBounds bounds: CGRect) -> CGRect {
return textRect(forBounds: bounds)
}
/** /**
Prepares the view instance when intialized. When subclassing, Prepares the view instance when intialized. When subclassing,
it is recommended to override the prepare method it is recommended to override the prepare method
...@@ -482,24 +493,24 @@ extension TextField { ...@@ -482,24 +493,24 @@ extension TextField {
extension TextField { extension TextField {
/// Layout the placeholderLabel. /// Layout the placeholderLabel.
fileprivate func layoutPlaceholderLabel() { fileprivate func layoutPlaceholderLabel() {
let w = leftViewWidth let w = leftViewWidth + textInset
let h = 0 == height ? intrinsicContentSize.height : height let h = 0 == height ? intrinsicContentSize.height : height
placeholderLabel.transform = CGAffineTransform.identity placeholderLabel.transform = CGAffineTransform.identity
guard isEditing || !isEmpty || !isPlaceholderAnimated else { guard isEditing || !isEmpty || !isPlaceholderAnimated else {
placeholderLabel.frame = CGRect(x: w, y: 0, width: width - w, height: h) placeholderLabel.frame = CGRect(x: w, y: 0, width: width - leftViewWidth - 2 * textInset, height: h)
return return
} }
placeholderLabel.frame = CGRect(x: w, y: 0, width: width - w, height: h) placeholderLabel.frame = CGRect(x: w, y: 0, width: width - leftViewWidth - 2 * textInset, height: h)
placeholderLabel.transform = CGAffineTransform(scaleX: 0.75, y: 0.75) placeholderLabel.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
switch textAlignment { switch textAlignment {
case .left, .natural: case .left, .natural:
placeholderLabel.x = w placeholderLabel.x = w
case .right: case .right:
placeholderLabel.x = width - placeholderLabel.width placeholderLabel.x = width - placeholderLabel.width - textInset
default:break default:break
} }
...@@ -640,9 +651,9 @@ extension TextField { ...@@ -640,9 +651,9 @@ extension TextField {
switch s.textAlignment { switch s.textAlignment {
case .left, .natural: case .left, .natural:
s.placeholderLabel.x = s.leftViewWidth s.placeholderLabel.x = s.leftViewWidth + s.textInset
case .right: case .right:
s.placeholderLabel.x = s.width - s.placeholderLabel.width s.placeholderLabel.x = s.width - s.placeholderLabel.width - s.textInset
default:break default:break
} }
...@@ -673,7 +684,7 @@ extension TextField { ...@@ -673,7 +684,7 @@ extension TextField {
} }
s.placeholderLabel.transform = CGAffineTransform.identity s.placeholderLabel.transform = CGAffineTransform.identity
s.placeholderLabel.x = s.leftViewWidth s.placeholderLabel.x = s.leftViewWidth + s.textInset
s.placeholderLabel.y = 0 s.placeholderLabel.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