Skip to content

Commit 8aa7d69

Browse files
committed
Merge pull request #22 from cemolcay/fix/cemolcay-key-pop
Fix key pop and update default layout
2 parents f60fa4d + 4bcff3a commit 8aa7d69

File tree

6 files changed

+208
-84
lines changed

6 files changed

+208
-84
lines changed

Keyboard/DefaultKeyboard/CustomKeyboard.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ import UIKit
1919
}
2020

2121
// MARK: - CustomKeyboard
22+
public var CustomKeyboardKeyButtonPopupTag: Int = 101
23+
2224
public class CustomKeyboard: UIView, KeyboardLayoutDelegate {
25+
public var keyButtonPopupContainer: UIView?
26+
2327
public var shiftToggleInterval: NSTimeInterval = 0.5
2428
private var shiftToggleTimer: NSTimer?
2529
private var shiftCanBeToggled: Bool = false
@@ -185,6 +189,31 @@ public class CustomKeyboard: UIView, KeyboardLayoutDelegate {
185189
shiftCanBeToggled = false
186190
}
187191

192+
// MARK: Key popup in custom container
193+
public func addKeyPopup(forKey key: KeyboardButton) {
194+
switch key.type {
195+
case .Key(_):
196+
if let container = keyButtonPopupContainer {
197+
if container.viewWithTag(CustomKeyboardKeyButtonPopupTag) == nil {
198+
let popup = key.createPopup()
199+
popup.tag = CustomKeyboardKeyButtonPopupTag
200+
popup.frame.origin = key.convertPoint(popup.frame.origin, toView: container)
201+
container.addSubview(popup)
202+
}
203+
}
204+
default:
205+
return
206+
}
207+
}
208+
209+
public func removeKeyPopup() {
210+
if let container = keyButtonPopupContainer {
211+
if let popup = container.viewWithTag(CustomKeyboardKeyButtonPopupTag) {
212+
popup.removeFromSuperview()
213+
}
214+
}
215+
}
216+
188217
// MARK: KeyboardLayoutDelegate
189218
public func keyboardLayoutDidPressButton(keyboardLayout: KeyboardLayout, keyboardButton: KeyboardButton) {
190219
delegate?.customKeyboardButtonPressed?(self, keyboardButton: keyboardButton)
@@ -252,10 +281,20 @@ public class CustomKeyboard: UIView, KeyboardLayoutDelegate {
252281
public func keyboardLayoutDidStartPressingButton(keyboardLayout: KeyboardLayout, keyboardButton: KeyboardButton) {
253282
invalidateBackspaceAutoDeleteModeTimer()
254283
invalidateBackspaceDeleteTimer()
284+
addKeyPopup(forKey: keyboardButton)
255285
if keyboardLayout == currentLayout {
256286
if keyboardButton.identifier == CustomKeyboardIdentifier.Backspace.rawValue {
257287
startBackspaceAutoDeleteModeTimer()
258288
}
259289
}
260290
}
291+
292+
public func keyboardLayoutDidDraggedInButton(keyboardLayout: KeyboardLayout, keyboardButton: KeyboardButton) {
293+
removeKeyPopup()
294+
addKeyPopup(forKey: keyboardButton)
295+
}
296+
297+
public func keyboardLayoutDidEndTouches(keyboardLayout: KeyboardLayout) {
298+
removeKeyPopup()
299+
}
261300
}

Keyboard/DefaultKeyboard/CustomKeyboardLayout.swift

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,29 @@ import UIKit
1010

1111
// MARK: - Layout Style
1212
public var CustomKeyboardLayoutStyle = KeyboardLayoutStyle(
13-
topPadding: 10,
14-
bottomPadding: 4,
15-
rowPadding: 12,
1613
backgroundColor: UIColor(red: 208.0/255.0, green: 213.0/255.0, blue: 219.0/255.0, alpha: 1))
1714

1815
// MARK: - Row Style
19-
public var CustomKeyboardRowStyle = KeyboardRowStyle(
20-
leadingPadding: 3,
21-
trailingPadding: 3,
22-
buttonsPadding: 6)
16+
public var CustomKeyboardRowStyle = KeyboardRowStyle()
2317

2418
public var CustomKeyboardSecondRowStyle = KeyboardRowStyle(
2519
leadingPadding: 22,
20+
leadingPaddingLandscape: 30,
2621
trailingPadding: 22,
27-
buttonsPadding: 6)
22+
trailingPaddingLandscape: 30)
23+
24+
public var CustomKeyboardThirdRowStyle = KeyboardRowStyle(
25+
bottomPadding: 10,
26+
bottomPaddingLandscape: 6,
27+
buttonsPadding: 15)
2828

2929
public var CustomKeyboardChildRowStyle = KeyboardRowStyle(
30-
leadingPadding: 12,
31-
trailingPadding: 12,
32-
buttonsPadding: 6)
30+
leadingPadding: 0,
31+
trailingPadding: 0)
3332

3433
// MARK: - Button Style
3534
public var CustomKeyboardKeyButtonStyle = KeyboardButtonStyle(showsPopup: true)
35+
public var CustomKeyboardLowercaseKeyButtonStyle = KeyboardButtonStyle(showsPopup: true, textOffsetY: -2)
3636

3737
public var CustomKeyboardSpaceButtonStyle = KeyboardButtonStyle(
3838
font: UIFont.systemFontOfSize(15),
@@ -134,7 +134,7 @@ public enum CustomKeyboardLayout {
134134
]
135135
),
136136
KeyboardRow(
137-
style: CustomKeyboardRowStyle,
137+
style: CustomKeyboardThirdRowStyle,
138138
characters: [
139139
KeyboardButton(
140140
type: .Image(UIImage(named: toggled ? "shiftToggled" : "shiftToggledOnce", inBundle: NSBundle(forClass: CustomKeyboard.self), compatibleWithTraitCollection: nil)),
@@ -195,34 +195,34 @@ public enum CustomKeyboardLayout {
195195
KeyboardRow(
196196
style: CustomKeyboardRowStyle,
197197
characters: [
198-
KeyboardButton(type: .Key("q"), style: CustomKeyboardKeyButtonStyle),
199-
KeyboardButton(type: .Key("w"), style: CustomKeyboardKeyButtonStyle),
200-
KeyboardButton(type: .Key("e"), style: CustomKeyboardKeyButtonStyle),
201-
KeyboardButton(type: .Key("r"), style: CustomKeyboardKeyButtonStyle),
202-
KeyboardButton(type: .Key("t"), style: CustomKeyboardKeyButtonStyle),
203-
KeyboardButton(type: .Key("y"), style: CustomKeyboardKeyButtonStyle),
204-
KeyboardButton(type: .Key("u"), style: CustomKeyboardKeyButtonStyle),
205-
KeyboardButton(type: .Key("i"), style: CustomKeyboardKeyButtonStyle),
206-
KeyboardButton(type: .Key("o"), style: CustomKeyboardKeyButtonStyle),
207-
KeyboardButton(type: .Key("p"), style: CustomKeyboardKeyButtonStyle),
198+
KeyboardButton(type: .Key("q"), style: CustomKeyboardLowercaseKeyButtonStyle),
199+
KeyboardButton(type: .Key("w"), style: CustomKeyboardLowercaseKeyButtonStyle),
200+
KeyboardButton(type: .Key("e"), style: CustomKeyboardLowercaseKeyButtonStyle),
201+
KeyboardButton(type: .Key("r"), style: CustomKeyboardLowercaseKeyButtonStyle),
202+
KeyboardButton(type: .Key("t"), style: CustomKeyboardLowercaseKeyButtonStyle),
203+
KeyboardButton(type: .Key("y"), style: CustomKeyboardLowercaseKeyButtonStyle),
204+
KeyboardButton(type: .Key("u"), style: CustomKeyboardLowercaseKeyButtonStyle),
205+
KeyboardButton(type: .Key("i"), style: CustomKeyboardLowercaseKeyButtonStyle),
206+
KeyboardButton(type: .Key("o"), style: CustomKeyboardLowercaseKeyButtonStyle),
207+
KeyboardButton(type: .Key("p"), style: CustomKeyboardLowercaseKeyButtonStyle),
208208
]
209209
),
210210
KeyboardRow(
211211
style: CustomKeyboardSecondRowStyle,
212212
characters: [
213-
KeyboardButton(type: .Key("a"), style: CustomKeyboardKeyButtonStyle),
214-
KeyboardButton(type: .Key("s"), style: CustomKeyboardKeyButtonStyle),
215-
KeyboardButton(type: .Key("d"), style: CustomKeyboardKeyButtonStyle),
216-
KeyboardButton(type: .Key("f"), style: CustomKeyboardKeyButtonStyle),
217-
KeyboardButton(type: .Key("g"), style: CustomKeyboardKeyButtonStyle),
218-
KeyboardButton(type: .Key("h"), style: CustomKeyboardKeyButtonStyle),
219-
KeyboardButton(type: .Key("j"), style: CustomKeyboardKeyButtonStyle),
220-
KeyboardButton(type: .Key("k"), style: CustomKeyboardKeyButtonStyle),
221-
KeyboardButton(type: .Key("l"), style: CustomKeyboardKeyButtonStyle),
213+
KeyboardButton(type: .Key("a"), style: CustomKeyboardLowercaseKeyButtonStyle),
214+
KeyboardButton(type: .Key("s"), style: CustomKeyboardLowercaseKeyButtonStyle),
215+
KeyboardButton(type: .Key("d"), style: CustomKeyboardLowercaseKeyButtonStyle),
216+
KeyboardButton(type: .Key("f"), style: CustomKeyboardLowercaseKeyButtonStyle),
217+
KeyboardButton(type: .Key("g"), style: CustomKeyboardLowercaseKeyButtonStyle),
218+
KeyboardButton(type: .Key("h"), style: CustomKeyboardLowercaseKeyButtonStyle),
219+
KeyboardButton(type: .Key("j"), style: CustomKeyboardLowercaseKeyButtonStyle),
220+
KeyboardButton(type: .Key("k"), style: CustomKeyboardLowercaseKeyButtonStyle),
221+
KeyboardButton(type: .Key("l"), style: CustomKeyboardLowercaseKeyButtonStyle),
222222
]
223223
),
224224
KeyboardRow(
225-
style: CustomKeyboardRowStyle,
225+
style: CustomKeyboardThirdRowStyle,
226226
characters: [
227227
KeyboardButton(
228228
type: .Image(UIImage(named: "shift", inBundle: NSBundle(forClass: CustomKeyboard.self), compatibleWithTraitCollection: nil)),
@@ -232,13 +232,13 @@ public enum CustomKeyboardLayout {
232232
KeyboardRow(
233233
style: CustomKeyboardChildRowStyle,
234234
characters: [
235-
KeyboardButton(type: .Key("z"), style: CustomKeyboardKeyButtonStyle),
236-
KeyboardButton(type: .Key("x"), style: CustomKeyboardKeyButtonStyle),
237-
KeyboardButton(type: .Key("c"), style: CustomKeyboardKeyButtonStyle),
238-
KeyboardButton(type: .Key("v"), style: CustomKeyboardKeyButtonStyle),
239-
KeyboardButton(type: .Key("b"), style: CustomKeyboardKeyButtonStyle),
240-
KeyboardButton(type: .Key("n"), style: CustomKeyboardKeyButtonStyle),
241-
KeyboardButton(type: .Key("m"), style: CustomKeyboardKeyButtonStyle),
235+
KeyboardButton(type: .Key("z"), style: CustomKeyboardLowercaseKeyButtonStyle),
236+
KeyboardButton(type: .Key("x"), style: CustomKeyboardLowercaseKeyButtonStyle),
237+
KeyboardButton(type: .Key("c"), style: CustomKeyboardLowercaseKeyButtonStyle),
238+
KeyboardButton(type: .Key("v"), style: CustomKeyboardLowercaseKeyButtonStyle),
239+
KeyboardButton(type: .Key("b"), style: CustomKeyboardLowercaseKeyButtonStyle),
240+
KeyboardButton(type: .Key("n"), style: CustomKeyboardLowercaseKeyButtonStyle),
241+
KeyboardButton(type: .Key("m"), style: CustomKeyboardLowercaseKeyButtonStyle),
242242
]
243243
),
244244
KeyboardButton(
@@ -311,7 +311,7 @@ public enum CustomKeyboardLayout {
311311
]
312312
),
313313
KeyboardRow(
314-
style: CustomKeyboardRowStyle,
314+
style: CustomKeyboardThirdRowStyle,
315315
characters: [
316316
KeyboardButton(
317317
type: .Text("#+="),
@@ -398,7 +398,7 @@ public enum CustomKeyboardLayout {
398398
]
399399
),
400400
KeyboardRow(
401-
style: CustomKeyboardRowStyle,
401+
style: CustomKeyboardThirdRowStyle,
402402
characters: [
403403
KeyboardButton(
404404
type: .Text("123"),

Keyboard/KeyboardLayoutEngine/KeyboardButton.swift

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public struct KeyboardButtonStyle {
4242
// Text
4343
public var textColor: UIColor
4444
public var font: UIFont
45+
public var textOffsetY: CGFloat
4546

4647
// Image
4748
public var imageSize: CGFloat?
@@ -57,13 +58,14 @@ public struct KeyboardButtonStyle {
5758
borderColor: UIColor = UIColor.clearColor(),
5859
borderWidth: CGFloat = 0,
5960
shadowEnabled: Bool = true,
60-
shadowColor: UIColor = UIColor.blackColor(),
61-
shadowOpacity: Float = 0.4,
61+
shadowColor: UIColor = UIColor(red: 138.0/255.0, green: 139.0/255.0, blue: 143.0/255.0, alpha: 1),
62+
shadowOpacity: Float = 1,
6263
shadowOffset: CGSize = CGSize(width: 0, height: 1),
6364
shadowRadius: CGFloat = 1 / UIScreen.mainScreen().scale,
6465
shadowPath: UIBezierPath? = nil,
6566
textColor: UIColor = UIColor.blackColor(),
6667
font: UIFont = UIFont.systemFontOfSize(20),
68+
textOffsetY: CGFloat = 0,
6769
imageSize: CGFloat? = nil,
6870
showsPopup: Bool = true,
6971
popupWidthMultiplier: CGFloat = 1.7,
@@ -80,6 +82,7 @@ public struct KeyboardButtonStyle {
8082
self.shadowPath = shadowPath
8183
self.textColor = textColor
8284
self.font = font
85+
self.textOffsetY = textOffsetY
8386
self.imageSize = imageSize
8487
self.showsPopup = showsPopup
8588
self.popupWidthMultiplier = popupWidthMultiplier
@@ -88,6 +91,8 @@ public struct KeyboardButtonStyle {
8891
}
8992

9093
// MARK: - KeyboardButton
94+
private let KeyboardButtonPopupViewTag: Int = 101
95+
9196
public class KeyboardButton: UIView {
9297
public var type: KeyboardButtonType = .Key("")
9398
public var width: KeyboardButtonWidth = .Dynamic
@@ -111,7 +116,7 @@ public class KeyboardButton: UIView {
111116
style: KeyboardButtonStyle,
112117
width: KeyboardButtonWidth = .Dynamic,
113118
identifier: String? = nil) {
114-
119+
115120
super.init(frame: CGRect.zero)
116121
self.type = type
117122
self.style = style
@@ -128,6 +133,8 @@ public class KeyboardButton: UIView {
128133
textLabel?.font = style.font
129134
textLabel?.textAlignment = .Center
130135
textLabel?.translatesAutoresizingMaskIntoConstraints = false
136+
textLabel?.adjustsFontSizeToFitWidth = true
137+
textLabel?.minimumScaleFactor = 0.5
131138
addSubview(textLabel!)
132139
case .Text(let text):
133140
textLabel = UILabel()
@@ -136,6 +143,8 @@ public class KeyboardButton: UIView {
136143
textLabel?.font = style.font
137144
textLabel?.textAlignment = .Center
138145
textLabel?.translatesAutoresizingMaskIntoConstraints = false
146+
textLabel?.adjustsFontSizeToFitWidth = true
147+
textLabel?.minimumScaleFactor = 0.5
139148
addSubview(textLabel!)
140149
case .Image(let image):
141150
imageView = UIImageView(image: image)
@@ -172,10 +181,17 @@ public class KeyboardButton: UIView {
172181
var padding = CGFloat(5)
173182
textLabel?.frame = CGRect(
174183
x: padding,
175-
y: padding,
184+
y: padding + style.textOffsetY,
176185
width: frame.size.width - (padding * 2),
177186
height: frame.size.height - (padding * 2))
178187

188+
switch type {
189+
case .Key(_):
190+
textLabel?.font = textLabel?.font.fontWithSize(min(textLabel!.frame.size.height, textLabel!.frame.size.width) + 1)
191+
default:
192+
break
193+
}
194+
179195
if let imageSize = style.imageSize {
180196
padding = (min(frame.size.height, frame.size.width) - imageSize) / 2
181197
}
@@ -187,21 +203,20 @@ public class KeyboardButton: UIView {
187203
height: frame.size.height - (padding * 2))
188204
}
189205

190-
// MARK: Popup
191206
private func showPopup(show show: Bool) {
192207
if show {
193-
if viewWithTag(101) != nil { return }
208+
if viewWithTag(KeyboardButtonPopupViewTag) != nil { return }
194209
let popup = createPopup()
195-
popup.tag = 101
210+
popup.tag = KeyboardButtonPopupViewTag
196211
addSubview(popup)
197212
} else {
198-
if let popup = viewWithTag(101) {
213+
if let popup = viewWithTag(KeyboardButtonPopupViewTag) {
199214
popup.removeFromSuperview()
200215
}
201216
}
202217
}
203218

204-
private func createPopup() -> UIView {
219+
public func createPopup() -> UIView {
205220
let topCornerRadius = style.cornerRadius * style.popupWidthMultiplier
206221
let topWidth = frame.size.width * style.popupWidthMultiplier
207222
let topHeight = frame.size.height * style.popupHeightMultiplier
@@ -223,6 +238,7 @@ public class KeyboardButton: UIView {
223238
top.addSubview(copyContentIntoView(top))
224239
// popup
225240
let popup = UIView()
241+
popup.userInteractionEnabled = false
226242
popup.addSubview(middle)
227243
popup.addSubview(top)
228244
return popup
@@ -241,23 +257,35 @@ public class KeyboardButton: UIView {
241257
let label = UILabel(frame: CGRect(x: 0, y: 0, width: contentView.frame.size.width, height: contentView.frame.size.height))
242258
label.text = text
243259
label.textColor = style.textColor
244-
label.font = style.font.fontWithSize(style.font.pointSize * style.popupWidthMultiplier)
245260
label.textAlignment = .Center
261+
label.adjustsFontSizeToFitWidth = true
262+
label.minimumScaleFactor = 0.5
263+
if let textLabel = self.textLabel {
264+
label.font = textLabel.font.fontWithSize(textLabel.font.pointSize * style.popupWidthMultiplier)
265+
} else {
266+
label.font = style.font.fontWithSize(style.font.pointSize * style.popupWidthMultiplier)
267+
}
246268
contentView.addSubview(label)
247269
case .Text(let text):
248270
let label = UILabel(frame: CGRect(x: 0, y: 0, width: contentView.frame.size.width, height: contentView.frame.size.height))
249271
label.text = text
250272
label.textColor = style.textColor
251-
label.font = style.font.fontWithSize(style.font.pointSize * style.popupWidthMultiplier)
252273
label.textAlignment = .Center
274+
label.adjustsFontSizeToFitWidth = true
275+
label.minimumScaleFactor = 0.5
276+
if let textLabel = self.textLabel {
277+
label.font = textLabel.font.fontWithSize(textLabel.font.pointSize * style.popupWidthMultiplier)
278+
} else {
279+
label.font = style.font.fontWithSize(style.font.pointSize * style.popupWidthMultiplier)
280+
}
253281
contentView.addSubview(label)
254282
case .Image(let image):
255283
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: contentView.frame.size.width, height: contentView.frame.size.height))
256284
imageView.contentMode = .ScaleAspectFit
257285
imageView.image = image
258286
contentView.addSubview(imageView)
259287
}
260-
288+
261289
return contentView
262290
}
263291
}

0 commit comments

Comments
 (0)