我需要使用文本字段让用户输入电话号码,所以我使用PhoneNumberTextField
来自项目PhoneNumberKit的。
默认情况下,它只是一个无边界文本字段。我通过添加一个带有圆角半径的边框并将其UIButton
添加到它的leftView
. 问题是文本/占位符显示在边框上。
我尝试子类PhoneNumberTextField
化并添加这样的文本插入功能。
@IBDesignable
class CNPhoneNumberTextField: PhoneNumberTextField {
@IBInspectable var inset: CGFloat = 0
override func textRect(forBounds bounds: CGRect) -> CGRect {
return bounds.insetBy(dx: inset, dy: inset)
}
override func editingRect(forBounds bounds: CGRect) -> CGRect {
return bounds.insetBy(dx: inset, dy: inset)
}
override func placeholderRect(forBounds bounds: CGRect) -> CGRect {
return bounds.insetBy(dx: inset, dy: inset)
}
}
这里的问题是它现在没有考虑leftView
到。文本/占位符部分被leftView
.
有没有其他方法可以解决这个问题?