CEP
用法
@IBOutlet private weak var floatingLabelTextField: FloatingLabelTextField!
override func viewDidLoad() {
super.viewDidLoad()
// Add right label with color
floatingLabelTextField.addRightText("Forgot?", color: UIColor.white)
// Add right images
floatingLabelTextField.addRightImages([UIImage(named: "image")!])
// Toggle text format, whether secured or not
floatingLabelTextField.toggleTextFormat()
// Update secure line and description text
floatingLabelTextField.updateSecureLine(to: .max, text: "Some description", color: .red)
// Check if current mode is secure
floatingLabelTextField.isSecure
// Change the keyboard type
floatingLabelTextField.setKeyboardType(.emailAddress)
// Change the autocorrection type
floatingLabelTextField.setAutocorrectionType(.no)
// Configure style programmatically
floatingLabelTextField.separatorColor = .blue
floatingLabelTextField.headerText = "Header"
floatingLabelTextField.headerColor = .green
floatingLabelTextField.placeholderText = "Placeholder"
floatingLabelTextField.placeholderColor = .red
floatingLabelTextField.selectionColor = .red
floatingLabelTextField.textColor = .white
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// Update text once view has been loaded
floatingLabelTextField.text = "Some text"
// Get inputted text whenever you want
floatingLabelTextField.text
}
您还可以通过 Interface Builder 配置样式
@IBInspectable var separatorColor: UIColor
@IBInspectable var headerColor: UIColor
@IBInspectable var headerText: String?
@IBInspectable var placeholderColor: UIColor
@IBInspectable var placeholderText: String?
@IBInspectable var textColor: UIColor
@IBInspectable var selectionColor: UIColor
@IBInspectable var isSecure: Bool
代理
extension ViewController: FloatingLabelTextFieldDelegate {
func state(for floatingTextField: FloatingLabelTextField) -> (state: InputTextState, description: String?, color: UIColor) {
let textLength = floatingTextField.text.count
switch textLength {
case 1...2: return (.invalid, "Invalid", UIColor(hex: "ff3f4c"))
case 3...5: return (.unreliable, "Unreliable", UIColor(hex: "ff793f"))
case 6...8: return (.medium, "Medium", UIColor(hex: "f5a623"))
case 9...11: return (.reliable, "Reliable", UIColor(hex: "00ab80"))
case let number where number >= 12: return (.max, "Max", UIColor(hex: "00c99c"))
default: return (.idle, nil, .clear)
}
}
}
监听器
floatingLabelTextField.events.append() { event in
switch(event) {
case .didStartEditing( _): print("DidStartEditing")
case .didEndEditing( _): print("DidEndEditing")
case .didTextChanged( _): print("DidTextChanged")
case .didExtraButtonPressed(let floatingTextField, let index, let button):
if index == 0 {
// Change current text format
floatingTextField.toggleTextFormat()
// Update button if needed
button.setImage(UIImage(named: floatingTextField.isSecute() ? "eye_off" : "eye_on")!, for: .normal)
}
}
}
需求
- iOS 9.0+
- CocoaPods 1.0.0+
- Swift 5
安装
FloatingLabelTextField 通过 CocoaPods 提供。要安装它,请简单地将以下行添加到您的 Podfile 中
pod 'FloatingLabelTextField'
许可证
本项目使用 MIT 许可证授权。
版权所有 (c) 2019 Dmitriy Zhyzhko
任何人可以免费获得此软件及其相关文档(以下简称“软件”)的副本,用于在不作任何限制的情况下处理软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件副本,并允许向提供软件的个人提供使用权限,只要遵守以下条件
上述版权声明和本许可声明应包含在所有软件副本或主要部分中。
软件按“现状”提供,不提供任何明示或暗示的保证,包括但不限于适销性、适用于特定目的和不侵犯专利的保证。在任何情况下,作者或版权持有人均不对索赔、损害或其他责任承担任何责任,无论是由于合同、侵权或其他原因引起,与软件或软件的使用或以其他方式相关。