Skip to content

Commit 0681786

Browse files
committed
Merge pull request #15 from cemolcay/feature/cemolcay-append-button
[KeyboardLayout] Get, Add, Remove KeyboardButtons
2 parents 28aa8f5 + 9b8aa0f commit 0681786

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

Keyboard/DefaultKeyboard/DefaultKeyboard.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import UIKit
1515
optional func defaultKeyboardDidPressBackspaceButton(defaultKeyboard: DefaultKeyboard)
1616
optional func defaultKeyboardDidPressGlobeButton(defaultKeyboard: DefaultKeyboard)
1717
optional func defaultKeyboardDidPressReturnButton(defaultKeyboard: DefaultKeyboard)
18+
optional func defaultKeyboardDidPressKeyboardButton(defaultKeyboard: DefaultKeyboard, keyboardButton: KeyboardButton)
1819
}
1920

2021
// MARK: - DefaultKeyboard
@@ -35,6 +36,7 @@ public class DefaultKeyboard: UIView, KeyboardLayoutDelegate {
3536
private var backspaceDeleteTimer: NSTimer?
3637
private var backspaceAutoDeleteModeTimer: NSTimer?
3738

39+
private var lastLetterLayout: KeyboardLayout?
3840
private(set) var currentLayout: KeyboardLayout! {
3941
didSet {
4042
oldValue?.delegate = nil
@@ -74,7 +76,7 @@ public class DefaultKeyboard: UIView, KeyboardLayoutDelegate {
7476
lowercaseLayout = DefaultKeyboardLayout.Lowercase.keyboardLayout
7577
numbersLayout = DefaultKeyboardLayout.Numbers.keyboardLayout
7678
symbolsLayout = DefaultKeyboardLayout.Symbols.keyboardLayout
77-
79+
7880
currentLayout = uppercaseLayout
7981
addSubview(currentLayout)
8082
}
@@ -136,6 +138,7 @@ public class DefaultKeyboard: UIView, KeyboardLayoutDelegate {
136138

137139
// MARK: KeyboardLayoutDelegate
138140
public func keyboardLayoutDidPressButton(keyboardLayout: KeyboardLayout, keyboardButton: KeyboardButton) {
141+
delegate?.defaultKeyboardDidPressKeyboardButton?(self, keyboardButton: keyboardButton)
139142
invalidateBackspaceAutoDeleteModeTimer()
140143
invalidateBackspaceDeleteTimer()
141144
if keyboardLayout == currentLayout {

Keyboard/KeyboardLayoutEngine/KeyboardLayout.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88

99
import UIKit
1010

11+
// MARK: - Array Extension
12+
extension CollectionType {
13+
/// Returns the element at the specified index iff it is within bounds, otherwise nil.
14+
subscript (safe index: Index) -> Generator.Element? {
15+
return indices.contains(index) ? self[index] : nil
16+
}
17+
}
18+
1119
// MARK: - KeyboardLayoutDelegate
1220
@objc public protocol KeyboardLayoutDelegate {
1321
optional func keyboardLayoutDidStartPressingButton(keyboardLayout: KeyboardLayout, keyboardButton: KeyboardButton)
@@ -83,6 +91,34 @@ public class KeyboardLayout: UIView {
8391
return max(0, (height - totalPaddings) / CGFloat(rows.count))
8492
}
8593

94+
// MARK: Manage Buttons
95+
public func getKeyboardButton(atRowIndex rowIndex: Int, buttonIndex: Int) -> KeyboardButton? {
96+
if let row = rows[safe: rowIndex], let button = row.characters[safe: buttonIndex] as? KeyboardButton {
97+
return button
98+
}
99+
return nil
100+
}
101+
102+
public func addKeyboardButton(keyboardButton button: KeyboardButton, rowAtIndex: Int, buttonIndex: Int?) {
103+
if let row = rows[safe: rowAtIndex] {
104+
if let index = buttonIndex where buttonIndex < row.characters.count {
105+
row.characters.insert(button, atIndex: index)
106+
} else {
107+
row.characters.append(button)
108+
}
109+
row.addSubview(button)
110+
}
111+
}
112+
113+
public func removeKeyboardButton(atRowIndex rowIndex: Int, buttonIndex: Int) -> Bool {
114+
if let row = rows[safe: rowIndex], let button = row.characters[safe: buttonIndex] {
115+
row.characters.removeAtIndex(buttonIndex)
116+
button.removeFromSuperview()
117+
return true
118+
}
119+
return false
120+
}
121+
86122
// MARK: Touch Handling
87123
public override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
88124
super.touchesBegan(touches, withEvent: event)

Keyboard/KeyboardViewController.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ class KeyboardViewController: UIInputViewController, DefaultKeyboardDelegate {
1818
defaultKeyboard = DefaultKeyboard()
1919
defaultKeyboard.delegate = self
2020
view.addSubview(defaultKeyboard)
21+
22+
// This is how you add extra buttons to layouts for customising DefaultKeyboard without even subclass it!
23+
let customButton = KeyboardButton(
24+
type: .Text("🕶"),
25+
style: DefaultKeyboardKeyButtonStyle,
26+
width: .Static(width: 40),
27+
identifier: "customButton")
28+
defaultKeyboard.numbersLayout.addKeyboardButton(
29+
keyboardButton: customButton,
30+
rowAtIndex: 3,
31+
buttonIndex: 2)
2132
}
2233

2334
override func viewDidLayoutSubviews() {
@@ -55,4 +66,12 @@ class KeyboardViewController: UIInputViewController, DefaultKeyboardDelegate {
5566
advanceToNextInputMode()
5667
}
5768
}
69+
70+
func defaultKeyboardDidPressKeyboardButton(defaultKeyboard: DefaultKeyboard, keyboardButton: KeyboardButton) {
71+
if defaultKeyboard == self.defaultKeyboard {
72+
if keyboardButton.identifier == "customButton" {
73+
print("custom button pressed")
74+
}
75+
}
76+
}
5877
}

0 commit comments

Comments
 (0)