CustomKeyboardTextField 0.2

CustomKeyboardTextField 0.2

测试已测试
语言语言 SwiftSwift
许可协议 MIT
发布最新发布2016年8月
SPM支持 SPM

giginet 维护。




CustomKeyboardTextField

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 中配置一些参数。

自定义键盘

您可以轻松实现自己的自定义键盘。

1. 实现自定义键盘视图

// 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 将反映在文本框中。

2. 定义 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
    }
}

3. 定义 CustomKeyboardTextField

typealias MyKeyboardTextField = CustomKeyboardTextField<MyKeyboardProvider>

有关详细说明,请参阅示例实现

安装

需求

  • iOS 8 或更高版本
  • Swift 2.2, 2.3

作者

giginet <[邮箱地址] >

许可

MIT许可协议