KeyboardLayoutEngine 0.9.9

KeyboardLayoutEngine 0.9.9

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布上次发布2016年7月
SPM支持SPM

Cem Olcay维护。



  • cemolcay

KeyboardLayoutEngine

⌨️ 最简单的iOS自定义键盘生成器!

alt tag

KeyboardLayoutEngine通过在矩形中动态布局键盘按钮并提供自定义样式而著称,易于使用。为了提高灵活性,KeyboardLayoutEngine提供了以下特性:

  • KeyboardLayout:用于排列带有自定义填充和颜色的行。
  • KeyboardRow:用于在内部排列按钮或另一组KeyboardRow
  • KeyboardButton:用于在行中渲染按钮。还提供了灵活的宽度、类型和其他非常有用的API。
  • 它们也是UIView,并在它们的layoutSubviews函数中处理其布局。
  • 它们的速度比自动布局快,但可以完美地适应任何您想要应用的键盘布局的CGFrame
  • 这意味着它们与方向变化配合得很好。(正在开发对大小类和/或方向的支持。)
  • KeyboardLayoutStyleKeyboardRowStyleKeyboardButtonStyle结构体处理几乎所有与样式相关的事情。
  • KeyboardLayoutDelegate用于获取按钮按下信息。
  • 此外,还提供了CustomKeyboard,它是了解它如何工作的一个很好的起点,而不仅仅是一个功能齐全的原生键盘。

用法

  • 使用自定义样式、行和按钮(其中包含文本或图像)描述您的键盘。
  • 请参阅CustomKeyboardLayout以获取详细用法。
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

  • 实现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>?)
}

KeyboardButtonWidth

public enum KeyboardButtonWidth {
  case Dynamic
  case Static(width: CGFloat)
  case Relative(percent: CGFloat)
}
  • 在行中排列按钮很重要。由于行可以有子行,因此按钮和行的正确尺寸是由按钮类型计算的。
  • 如果您保留默认的.Dynamic,则行中的每个按钮都会根据KeyboardRowStyle.buttonPadding和行的总宽度计算其宽度,并确定具有相同填充的等宽宽度。(默认情况下是.Dynamic
  • 静态将是显然的静态宽度。
  • 相对是其中一个有趣的选择,它接受[0, 1]之间的值,填充父行的百分比,智能计算。

KeyboardButtonType

public enum KeyboardButtonType {
  case Key(String)
  case Text(String)
  case Image(UIImage?)
}
  • 按钮可以是KeyTextImage
  • 键形态可能对textDocumentProxy.insertText操作有用。
  • 文本大小写可能对“空格”、“回车”、“ABC”、“123”或任何包含emoji的字符串按钮很有用。
  • 图像大小写可能对“shift”、“退格”、“切换键盘”等按钮很有用。

样式

  • 每个样式结构都有其原始键盘的默认值。
  • 如果你在样式结构的init函数中没有分配值,它将使用其默认值。

KeyboardLayoutStyle

定义

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))

KeyboardRowStyle

定义

public struct KeyboardRowStyle {
  public var leadingPadding: CGFloat
  public var trailingPadding: CGFloat
  public var buttonsPadding: CGFloat
}

示例

let CustomKeyboardRowStyle = KeyboardRowStyle(
  leadingPadding: 5,
  trailingPadding: 5,
  buttonsPadding: 6)

KeyboardButtonStyle

定义

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)

CustomKeyboard

使用KeyboardLayoutEngine的默认iOS键盘实现。

  • Shift切换机制
  • 退格机制
  • 键按钮弹出菜单
  • textDocumentProxyCustomKeyboardDelegate的集成
  • KeyboardViewController中的简单实现
  • 在初始化之前更改默认样式,你就有了一个完全功能化的自定义标准英语QWERTY键盘!
override func viewDidLoad() {
    super.viewDidLoad()
    CustomKeyboardLayoutStyle.backgroundColor = UIColor.redColor()
    CustomKeyboardRowStyle.buttonsPadding = 5
    customKeyboard = CustomKeyboard()
    customKeyboard.delegate = self
    view.addSubview(customKeyboard)
}

自定义键盘样式

  • CustomKeyboardLayoutStyle:KeyboardLayoutStyle
  • CustomKeyboardRowStyle:KeyboardRowStyle
  • CustomKeyboardSecondRowStyle:KeyboardRowStyle
  • CustomKeyboardChildRowStyle:KeyboardRowStyle
  • CustomKeyboardSpaceButtonStyle:KeyboardButtonStyle
  • CustomKeyboardBackspaceButtonStyle:KeyboardButtonStyle
  • CustomKeyboardShiftButtonStyle:KeyboardButtonStyle
  • CustomKeyboardGlobeButtonStyle:KeyboardButtonStyle
  • CustomKeyboardReturnButtonStyle:KeyboardButtonStyle
  • CustomKeyboardNumbersButtonStyle:KeyboardButtonStyle
  • CustomKeyboardKeyButtonStyle:KeyboardButtonStyle

CustomKeyboardDelegate

  • 提供有关按键和特殊按钮按下的信息。
@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)
}