美丽文本字段
关于
美丽文本字段只是好看的UITextField。易于定制!易于使用!现在试试吧!
示例
要运行示例项目,请先克隆仓库,然后从 Example 目录运行 pod install
要求
在 Interface Builder (Storyboard / XIB) 中将边框样式设置为 None 并设置 textField 的自定义高度约为 40pt ~ 50pt (取决于 fontSize 和 placeholderFontScale)
自定义
使用默认属性设置textField
// Readonly views
var borderView: UIView!
var bottomBorderView: UIView!
var placeholderLabel: UILabel!
// Border
var borderInactiveColor: UIColor = .lightGray
var borderActiveColor: UIColor = .red
var borderWidth: CGFloat = 2.0
// Placeholder
var placeholderFontScale: CGFloat = 0.7
var placeholderColor: UIColor = .black
var placeholder: String // Setup placeholder inside Inteface Builder or right inside code!
// Error
var errorColor: UIColor = .red
var errorValidationHandler: (String) -> (String?)
var isValid: Bool // (readonly) Anytime you could check textField validation status
使用自己的验证器
emailTextField.errorValidationHandler = { text in
let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}"
let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
let match = emailTest.evaluate(with: text)
return (match ? "Invalid email" : nil)
}
设置自定义的文本颜色动画!(或者任何你想要的东西!)
open override func configureTextField(forTextFieldStateType textFieldStateType: BaseBeautifulTextField.TextFieldStateType, forTextStateType textStateType: BaseBeautifulTextField.TextStateType, animated: Bool, animations: (() -> ())?, completion: ((Bool) -> ())?) {
super.configureTextField(forTextFieldStateType: textFieldStateType, forTextStateType: textStateType, animated: animated, animations: {
switch textFieldStateType {
case .display:
self.textColor = self.textColor?.withAlphaComponent(0.6)
case .entry:
self.textColor = self.textColor?.withAlphaComponent(1.0)
}
}) { (finished) in
// ...
}
}
安装
BeautifulTextField可通过CocoaPods获得。要安装它,只需将以下行添加到您的Podfile中
pod "BeautifulTextField"
作者
Dmitry Utmanov,[email protected]
许可证
BeautifulTextField可在MIT许可证下使用。请参阅LICENSE文件获取更多信息。