1
1
//
2
- // DefaultKeyboard .swift
2
+ // CustomKeyboard .swift
3
3
// KeyboardLayoutEngine
4
4
//
5
5
// Created by Cem Olcay on 11/05/16.
8
8
9
9
import UIKit
10
10
11
- // MARK: - DefaultKeyboardDelegate
12
- @objc public protocol DefaultKeyboardDelegate {
13
- optional func defaultKeyboardDidPressKeyButton ( defaultKeyboard : DefaultKeyboard , key: String )
14
- optional func defaultKeyboardDidPressSpaceButton ( defaultKeyboard : DefaultKeyboard )
15
- optional func defaultKeyboardDidPressBackspaceButton ( defaultKeyboard : DefaultKeyboard )
16
- optional func defaultKeyboardDidPressGlobeButton ( defaultKeyboard : DefaultKeyboard )
17
- optional func defaultKeyboardDidPressReturnButton ( defaultKeyboard : DefaultKeyboard )
18
- optional func defaultKeyboardDidPressKeyboardButton ( defaultKeyboard : DefaultKeyboard , keyboardButton: KeyboardButton )
11
+ // MARK: - CustomKeyboardDelegate
12
+ @objc public protocol CustomKeyboardDelegate {
13
+ optional func customKeyboardKeyButtonPressed ( customKeyboard : CustomKeyboard , key: String )
14
+ optional func customKeyboardSpaceButtonPressed ( customKeyboard : CustomKeyboard )
15
+ optional func customKeyboardBackspaceButtonPressed ( customKeyboard : CustomKeyboard )
16
+ optional func customKeyboardGlobeButtonPressed ( customKeyboard : CustomKeyboard )
17
+ optional func customKeyboardReturnButtonPressed ( customKeyboard : CustomKeyboard )
18
+ optional func customKeyboardButtonPressed ( customKeyboard : CustomKeyboard , keyboardButton: KeyboardButton )
19
19
}
20
20
21
- // MARK: - DefaultKeyboard
22
- public class DefaultKeyboard : UIView , KeyboardLayoutDelegate {
23
- public var uppercaseToggledLayout : KeyboardLayout !
24
- public var uppercaseLayout : KeyboardLayout !
25
- public var lowercaseLayout : KeyboardLayout !
26
- public var numbersLayout : KeyboardLayout !
27
- public var symbolsLayout : KeyboardLayout !
28
-
21
+ // MARK: - CustomKeyboard
22
+ public class CustomKeyboard : UIView , KeyboardLayoutDelegate {
29
23
public var shiftToggleInterval : NSTimeInterval = 0.5
30
24
private var shiftToggleTimer : NSTimer ?
31
25
private var shiftCanBeToggled : Bool = false
@@ -36,6 +30,51 @@ public class DefaultKeyboard: UIView, KeyboardLayoutDelegate {
36
30
private var backspaceDeleteTimer : NSTimer ?
37
31
private var backspaceAutoDeleteModeTimer : NSTimer ?
38
32
33
+ public var uppercaseToggledLayout : KeyboardLayout ! {
34
+ didSet {
35
+ guard currentLayout != nil && oldValue != nil else { return }
36
+ if currentLayout == oldValue {
37
+ currentLayout = uppercaseToggledLayout
38
+ }
39
+ }
40
+ }
41
+
42
+ public var uppercaseLayout : KeyboardLayout ! {
43
+ didSet {
44
+ guard currentLayout != nil && oldValue != nil else { return }
45
+ if currentLayout == oldValue {
46
+ currentLayout = uppercaseLayout
47
+ }
48
+ }
49
+ }
50
+
51
+ public var lowercaseLayout : KeyboardLayout ! {
52
+ didSet {
53
+ guard currentLayout != nil && oldValue != nil else { return }
54
+ if currentLayout == oldValue {
55
+ currentLayout = lowercaseLayout
56
+ }
57
+ }
58
+ }
59
+
60
+ public var numbersLayout : KeyboardLayout ! {
61
+ didSet {
62
+ guard currentLayout != nil && oldValue != nil else { return }
63
+ if currentLayout == oldValue {
64
+ currentLayout = numbersLayout
65
+ }
66
+ }
67
+ }
68
+
69
+ public var symbolsLayout : KeyboardLayout ! {
70
+ didSet {
71
+ guard currentLayout != nil && oldValue != nil else { return }
72
+ if currentLayout == oldValue {
73
+ currentLayout = symbolsLayout
74
+ }
75
+ }
76
+ }
77
+
39
78
private var lastLetterLayout : KeyboardLayout ?
40
79
private( set) var currentLayout : KeyboardLayout ! {
41
80
didSet {
@@ -46,13 +85,7 @@ public class DefaultKeyboard: UIView, KeyboardLayoutDelegate {
46
85
}
47
86
}
48
87
49
- public override var frame : CGRect {
50
- didSet {
51
- currentLayout? . frame = frame
52
- }
53
- }
54
-
55
- public weak var delegate : DefaultKeyboardDelegate ?
88
+ public weak var delegate : CustomKeyboardDelegate ?
56
89
57
90
// MARK: Init
58
91
public init ( ) {
@@ -71,22 +104,36 @@ public class DefaultKeyboard: UIView, KeyboardLayoutDelegate {
71
104
}
72
105
73
106
private func defaultInit( ) {
74
- uppercaseToggledLayout = DefaultKeyboardLayout . UppercaseToggled. keyboardLayout
75
- uppercaseLayout = DefaultKeyboardLayout . Uppercase. keyboardLayout
76
- lowercaseLayout = DefaultKeyboardLayout . Lowercase. keyboardLayout
77
- numbersLayout = DefaultKeyboardLayout . Numbers. keyboardLayout
78
- symbolsLayout = DefaultKeyboardLayout . Symbols. keyboardLayout
79
-
107
+ reload ( )
80
108
currentLayout = uppercaseLayout
81
109
addSubview ( currentLayout)
82
110
}
83
111
112
+ // MARK: Layout
113
+ public override func layoutSubviews( ) {
114
+ super. layoutSubviews ( )
115
+ currentLayout? . frame = CGRect (
116
+ x: 0 ,
117
+ y: 0 ,
118
+ width: frame. size. width,
119
+ height: frame. size. height)
120
+ }
121
+
122
+ // MARK: Reload
123
+ public func reload( ) {
124
+ uppercaseToggledLayout = CustomKeyboardLayout . UppercaseToggled. keyboardLayout
125
+ uppercaseLayout = CustomKeyboardLayout . Uppercase. keyboardLayout
126
+ lowercaseLayout = CustomKeyboardLayout . Lowercase. keyboardLayout
127
+ numbersLayout = CustomKeyboardLayout . Numbers. keyboardLayout
128
+ symbolsLayout = CustomKeyboardLayout . Symbols. keyboardLayout
129
+ }
130
+
84
131
// MARK: Backspace Auto Delete
85
132
private func startBackspaceAutoDeleteModeTimer( ) {
86
133
backspaceAutoDeleteModeTimer = NSTimer . scheduledTimerWithTimeInterval (
87
134
backspaceAutoDeleteModeInterval,
88
135
target: self ,
89
- selector: #selector( DefaultKeyboard . startBackspaceAutoDeleteMode) ,
136
+ selector: #selector( CustomKeyboard . startBackspaceAutoDeleteMode) ,
90
137
userInfo: nil ,
91
138
repeats: false )
92
139
}
@@ -95,7 +142,7 @@ public class DefaultKeyboard: UIView, KeyboardLayoutDelegate {
95
142
backspaceDeleteTimer = NSTimer . scheduledTimerWithTimeInterval (
96
143
backspaceDeleteInterval,
97
144
target: self ,
98
- selector: #selector( DefaultKeyboard . autoDelete) ,
145
+ selector: #selector( CustomKeyboard . autoDelete) ,
99
146
userInfo: nil ,
100
147
repeats: true )
101
148
}
@@ -116,7 +163,7 @@ public class DefaultKeyboard: UIView, KeyboardLayoutDelegate {
116
163
}
117
164
118
165
internal func autoDelete( ) {
119
- delegate? . defaultKeyboardDidPressBackspaceButton ? ( self )
166
+ delegate? . customKeyboardBackspaceButtonPressed ? ( self )
120
167
}
121
168
122
169
// MARK: Shift Toggle
@@ -125,7 +172,7 @@ public class DefaultKeyboard: UIView, KeyboardLayoutDelegate {
125
172
shiftToggleTimer = NSTimer . scheduledTimerWithTimeInterval (
126
173
shiftToggleInterval,
127
174
target: self ,
128
- selector: #selector( DefaultKeyboard . invalidateShiftToggleTimer) ,
175
+ selector: #selector( CustomKeyboard . invalidateShiftToggleTimer) ,
129
176
userInfo: nil ,
130
177
repeats: false )
131
178
}
@@ -138,31 +185,31 @@ public class DefaultKeyboard: UIView, KeyboardLayoutDelegate {
138
185
139
186
// MARK: KeyboardLayoutDelegate
140
187
public func keyboardLayoutDidPressButton( keyboardLayout: KeyboardLayout , keyboardButton: KeyboardButton ) {
141
- delegate? . defaultKeyboardDidPressKeyboardButton ? ( self , keyboardButton: keyboardButton)
188
+ delegate? . customKeyboardButtonPressed ? ( self , keyboardButton: keyboardButton)
142
189
invalidateBackspaceAutoDeleteModeTimer ( )
143
190
invalidateBackspaceDeleteTimer ( )
144
191
if keyboardLayout == currentLayout {
145
192
switch keyboardButton. type {
146
193
case . Key( let key) :
147
- delegate? . defaultKeyboardDidPressKeyButton ? ( self , key: key)
194
+ delegate? . customKeyboardKeyButtonPressed ? ( self , key: key)
148
195
if uppercaseOnce {
149
196
uppercaseOnce = false
150
197
currentLayout = lowercaseLayout
151
198
}
152
199
default :
153
200
if let id = keyboardButton. identifier,
154
- let identifier = DefaultKeyboardIdentifier ( rawValue: id) {
201
+ let identifier = CustomKeyboardIdentifier ( rawValue: id) {
155
202
switch identifier {
156
203
case . Space:
157
- delegate? . defaultKeyboardDidPressSpaceButton ? ( self )
204
+ delegate? . customKeyboardSpaceButtonPressed ? ( self )
158
205
uppercaseOnce = false
159
206
case . Backspace:
160
- delegate? . defaultKeyboardDidPressBackspaceButton ? ( self )
207
+ delegate? . customKeyboardBackspaceButtonPressed ? ( self )
161
208
uppercaseOnce = false
162
209
case . Globe:
163
- delegate? . defaultKeyboardDidPressGlobeButton ? ( self )
210
+ delegate? . customKeyboardGlobeButtonPressed ? ( self )
164
211
case . Return:
165
- delegate? . defaultKeyboardDidPressReturnButton ? ( self )
212
+ delegate? . customKeyboardReturnButtonPressed ? ( self )
166
213
case . Letters:
167
214
currentLayout = uppercaseLayout
168
215
uppercaseOnce = true
@@ -204,7 +251,7 @@ public class DefaultKeyboard: UIView, KeyboardLayoutDelegate {
204
251
invalidateBackspaceAutoDeleteModeTimer ( )
205
252
invalidateBackspaceDeleteTimer ( )
206
253
if keyboardLayout == currentLayout {
207
- if keyboardButton. identifier == DefaultKeyboardIdentifier . Backspace. rawValue {
254
+ if keyboardButton. identifier == CustomKeyboardIdentifier . Backspace. rawValue {
208
255
startBackspaceAutoDeleteModeTimer ( )
209
256
}
210
257
}
0 commit comments