CustomKeyboardTextField 提供了一种简单的方法来创建具有自定义键盘的 TextField。
此库处于不稳定状态。因此,接口可能会改变。
在 Appetize.io 上可用的示例应用程序:Demo 应用程序。
import CustomKeyboardTextField
struct SimplePickerKeyboardDataSource: UIPickerViewKeyboardDataSource {
let elements = ["Bulbasaur", "Charmander", "Squirtle"]
}
typealias SimplePickerTextField = PickerKeyboardTextField<SimplePickerKeyboardDataSource>
let pickerTextField = SimplePickerTextField()
addSubView(pickerTextField)
如果您想更改输入文本,请覆盖 inputText(for row: Int)
。
您还可以实现丰富选择器视图。有关详细说明,请参阅示例实现。
import CustomKeyboardTextField
struct DatePickerKeyboardDataSource: UIDatePickerKeyboardDataSource {
let datePickerMode: UIDatePickerMode = .Date
let timeFormat: String = "yyyy/MM/dd"
}
typealias DatePickerTextField = UIDatePickerKeyboardTextField<DatePickerKeyboardDataSource>
您可以在 UIDatePickerKeyboardDataSource
中配置一些参数。
您可以轻松实现自己的自定义键盘。
// Your custom view
class MyCustomKeyboardView: UIView, CustomKeyboardView {
weak var textField: UITextField?
// current input text
var inputText: String = "input text"
// do something when keyboards are restored to default (optional)
func reset() {
}
}
您可以在您的键盘上放置一些控件(按钮、开关、文本框等)。当这些控件被交互时,您将执行 updateTextField()
,然后 inputText
将反映在文本框中。
CustomKeyboardProvider
struct MyKeyboardProvider: CustomKeyboardProvider {
weak var textField: UITextField!
init(with textField: UITextField) {
self.textField = textField
}
func inputView(with textField: UITextField) -> CustomKeyboardView? {
// Return keyboard view
let myKeyboardView = MyCustomKeyboardView()
myKeyboardView.textField = textField
return myKeyboardView
}
func inputAccessoryView(with textField: UITextField) -> CustomKeyboardAccessoryView? {
// Return CustomKeyboardAccessoryView or nil
// ToolbarKeyboardAccessoryView is built-in
// Of cource, you can implement your own toolbars
// CustomKeyboardAccessoryView has same interface with CustomKeyboardView
let accessoryView = ToolbarKeyboardAccessoryView(with: textField)
return accessoryView
}
}
CustomKeyboardTextField
typealias MyKeyboardTextField = CustomKeyboardTextField<MyKeyboardProvider>
有关详细说明,请参阅示例实现。
giginet <[邮箱地址] >
MIT许可协议