/// left, right Inset.
var textInset: UIEdgeInsets = .zero
/// clearBtn right inset.
var clearInset: CGFloat = 0
private var fRects = FieldRects()
private struct FieldRects {
var editBox: CGRect = .zero
var clearBtn: CGRect = .zero
var leftView: CGRect = .zero
var rightView: CGRect = .zero
}
override var leftView: UIView? {
didSet {
leftViewMode = .always
fRects.leftView = super.leftViewRect(forBounds: bounds)
}
}
override var rightView: UIView? {
didSet {
rightViewMode = .unlessEditing
fRects.rightView = super.rightViewRect(forBounds: bounds)
}
}
override func awakeFromNib() {
super.awakeFromNib()
clearButtonMode = .whileEditing
fRects.editBox = super.editingRect(forBounds: bounds)
fRects.clearBtn = super.clearButtonRect(forBounds: bounds)
}
override func clearButtonRect(forBounds bounds: CGRect) -> CGRect {
var rect = fRects.clearBtn
rect.origin.x -= clearInset
return rect
}
override func editingRect(forBounds bounds: CGRect) -> CGRect {
var rect = fRects.editBox
rect.origin.x += (fRects.leftView.width + textInset.left)
rect.size.width -= (fRects.rightView.width - textInset.right - rect.origin.x)
return rect
}
override func textRect(forBounds bounds: CGRect) -> CGRect {
var rect = bounds
rect.origin.x += (fRects.leftView.width + textInset.left)
rect.size.width -= (fRects.rightView.width - textInset.right - rect.origin.x)
return rect
}