⌨️ 最简单的iOS自定义键盘生成器!
KeyboardLayoutEngine
通过在矩形中动态布局键盘按钮并提供自定义样式而著称,易于使用。为了提高灵活性,KeyboardLayoutEngine提供了以下特性:
KeyboardLayout
:用于排列带有自定义填充和颜色的行。KeyboardRow
:用于在内部排列按钮或另一组KeyboardRow
。KeyboardButton
:用于在行中渲染按钮。还提供了灵活的宽度、类型和其他非常有用的API。UIView
,并在它们的layoutSubviews
函数中处理其布局。CGFrame
。KeyboardLayoutStyle
、KeyboardRowStyle
和KeyboardButtonStyle
结构体处理几乎所有与样式相关的事情。KeyboardLayoutDelegate
用于获取按钮按下信息。CustomKeyboard
,它是了解它如何工作的一个很好的起点,而不仅仅是一个功能齐全的原生键盘。let keyboardLayout = KeyboardLayout(
style: CustomKeyboardLayoutStyle,
rows: [
KeyboardRow(
style: CustomKeyboardRowStyle,
characters: [
KeyboardButton(type: .Key("Q"), style: CustomKeyboardKeyButtonStyle),
KeyboardButton(type: .Key("W"), style: CustomKeyboardKeyButtonStyle),
KeyboardButton(type: .Key("E"), style: CustomKeyboardKeyButtonStyle),
KeyboardButton(type: .Key("R"), style: CustomKeyboardKeyButtonStyle),
KeyboardButton(type: .Key("T"), style: CustomKeyboardKeyButtonStyle),
KeyboardButton(type: .Key("Y"), style: CustomKeyboardKeyButtonStyle),
KeyboardButton(type: .Key("U"), style: CustomKeyboardKeyButtonStyle),
KeyboardButton(type: .Key("I"), style: CustomKeyboardKeyButtonStyle),
KeyboardButton(type: .Key("O"), style: CustomKeyboardKeyButtonStyle),
KeyboardButton(type: .Key("P"), style: CustomKeyboardKeyButtonStyle),
]
)
]
)
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(keyboardLayout)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
keyboardLayout.setNeedsLayout()
}
KeyboardLayoutDelegate
以获取有关按钮按下的信息。@objc public protocol KeyboardLayoutDelegate {
// Key Press Events
optional func keyboardLayout(keyboardLayout: KeyboardLayout, didKeyPressStart keyboardButton: KeyboardButton)
optional func keyboardLayout(keyboardLayout: KeyboardLayout, didKeyPressEnd keyboardButton: KeyboardButton)
optional func keyboardLayout(keyboardLayout: KeyboardLayout, didDraggedIn fromKeyboardButton: KeyboardButton, toKeyboardButton: KeyboardButton)
// Touch Events
optional func keyboardLayout(keyboardLayout: KeyboardLayout, didTouchesBegin touches: Set<UITouch>)
optional func keyboardLayout(keyboardLayout: KeyboardLayout, didTouchesMove touches: Set<UITouch>)
optional func keyboardLayout(keyboardLayout: KeyboardLayout, didTouchesEnd touches: Set<UITouch>?)
optional func keyboardLayout(keyboardLayout: KeyboardLayout, didTouchesCancel touches: Set<UITouch>?)
}
public enum KeyboardButtonWidth {
case Dynamic
case Static(width: CGFloat)
case Relative(percent: CGFloat)
}
.Dynamic
,则行中的每个按钮都会根据KeyboardRowStyle.buttonPadding
和行的总宽度计算其宽度,并确定具有相同填充的等宽宽度。(默认情况下是.Dynamic
)public enum KeyboardButtonType {
case Key(String)
case Text(String)
case Image(UIImage?)
}
Key
、Text
或Image
。textDocumentProxy.insertText
操作有用。init
函数中没有分配值,它将使用其默认值。定义
public struct KeyboardLayoutStyle {
public var topPadding: CGFloat
public var bottomPadding: CGFloat
public var rowPadding: CGFloat
public var backgroundColor: UIColor
}
示例
let CustomKeyboardLayoutStyle = KeyboardLayoutStyle(
topPadding: 10,
bottomPadding: 5,
rowPadding: 13,
backgroundColor: UIColor(red: 208.0/255.0, green: 213.0/255.0, blue: 219.0/255.0, alpha: 1))
定义
public struct KeyboardRowStyle {
public var leadingPadding: CGFloat
public var trailingPadding: CGFloat
public var buttonsPadding: CGFloat
}
示例
let CustomKeyboardRowStyle = KeyboardRowStyle(
leadingPadding: 5,
trailingPadding: 5,
buttonsPadding: 6)
定义
public struct KeyboardButtonStyle {
public var backgroundColor: UIColor
public var cornerRadius: CGFloat
// Border
public var borderColor: UIColor
public var borderWidth: CGFloat
// Shadow
public var shadowColor: UIColor
public var shadowOpacity: Float
public var shadowOffset: CGSize
public var shadowRadius: CGFloat
public var shadowPath: UIBezierPath?
// Text
public var textColor: UIColor
public var font: UIFont
// Image
public var imageSize: CGFloat?
// Popup
public var showsPopup: Bool
public var popupWidthMultiplier: CGFloat
public var popupHeightMultiplier: CGFloat
}
示例
let CustomKeyboardDarkImageButtonStyle = KeyboardButtonStyle(
backgroundColor: UIColor(red: 180.0/255.0, green: 188.0/255.0, blue: 201.0/255.0, alpha: 1),
imageSize: 18,
showsPopup: false)
使用KeyboardLayoutEngine
的默认iOS键盘实现。
textDocumentProxy
与CustomKeyboardDelegate
的集成KeyboardViewController
中的简单实现override func viewDidLoad() {
super.viewDidLoad()
CustomKeyboardLayoutStyle.backgroundColor = UIColor.redColor()
CustomKeyboardRowStyle.buttonsPadding = 5
customKeyboard = CustomKeyboard()
customKeyboard.delegate = self
view.addSubview(customKeyboard)
}
KeyboardLayoutStyle
KeyboardRowStyle
KeyboardRowStyle
KeyboardRowStyle
KeyboardButtonStyle
KeyboardButtonStyle
KeyboardButtonStyle
KeyboardButtonStyle
KeyboardButtonStyle
KeyboardButtonStyle
KeyboardButtonStyle
@objc public protocol CustomKeyboardDelegate {
optional func customKeyboard(customKeyboard: CustomKeyboard, keyboardButtonPressed keyboardButton: KeyboardButton)
optional func customKeyboard(customKeyboard: CustomKeyboard, keyButtonPressed key: String)
optional func customKeyboardSpaceButtonPressed(customKeyboard: CustomKeyboard)
optional func customKeyboardBackspaceButtonPressed(customKeyboard: CustomKeyboard)
optional func customKeyboardGlobeButtonPressed(customKeyboard: CustomKeyboard)
optional func customKeyboardReturnButtonPressed(customKeyboard: CustomKeyboard)
}