Skip to content

Commit 3dba825

Browse files
committed
[KeyboardButton] Reload button on type, style, width changes
1 parent 5fa522d commit 3dba825

File tree

1 file changed

+35
-26
lines changed

1 file changed

+35
-26
lines changed

Keyboard/KeyboardLayoutEngine/KeyboardButton.swift

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ public var KeyboardButtonPopupViewTag: Int = 101
9898
public var KeyboardButtonMenuViewTag: Int = 102
9999

100100
public class KeyboardButton: UIView {
101-
public var type: KeyboardButtonType = .Key("")
102-
public var widthInRow: KeyboardButtonWidth = .Dynamic
103-
public var style: KeyboardButtonStyle!
101+
public var type: KeyboardButtonType = .Key("") { didSet { reload() } }
102+
public var widthInRow: KeyboardButtonWidth = .Dynamic { didSet { reload() } }
103+
public var style: KeyboardButtonStyle! { didSet { reload() } }
104104
public var keyMenu: KeyMenu?
105105

106106
public var textLabel: UILabel?
@@ -122,8 +122,39 @@ public class KeyboardButton: UIView {
122122
self.style = style
123123
self.widthInRow = width
124124
self.identifier = identifier
125+
reload()
126+
}
127+
128+
public required init?(coder aDecoder: NSCoder) {
129+
super.init(coder: aDecoder)
130+
reload()
131+
}
132+
133+
private func reload() {
125134
userInteractionEnabled = true
126-
setupAppearance()
135+
backgroundColor = style.backgroundColor
136+
layer.cornerRadius = style.cornerRadius
137+
138+
// border
139+
layer.borderColor = style.borderColor.CGColor
140+
layer.borderWidth = style.borderWidth
141+
142+
// shadow
143+
if style.shadowEnabled {
144+
layer.shadowColor = style.shadowColor.CGColor
145+
layer.shadowOpacity = style.shadowOpacity
146+
layer.shadowOffset = style.shadowOffset
147+
layer.shadowRadius = style.shadowRadius
148+
if let path = style.shadowPath {
149+
layer.shadowPath = path.CGPath
150+
}
151+
}
152+
153+
// content
154+
textLabel?.removeFromSuperview()
155+
textLabel = nil
156+
imageView?.removeFromSuperview()
157+
imageView = nil
127158

128159
switch type {
129160
case .Key(let text):
@@ -153,28 +184,6 @@ public class KeyboardButton: UIView {
153184
}
154185
}
155186

156-
public required init?(coder aDecoder: NSCoder) {
157-
super.init(coder: aDecoder)
158-
}
159-
160-
private func setupAppearance() {
161-
backgroundColor = style.backgroundColor
162-
layer.cornerRadius = style.cornerRadius
163-
// border
164-
layer.borderColor = style.borderColor.CGColor
165-
layer.borderWidth = style.borderWidth
166-
// shadow
167-
if style.shadowEnabled {
168-
layer.shadowColor = style.shadowColor.CGColor
169-
layer.shadowOpacity = style.shadowOpacity
170-
layer.shadowOffset = style.shadowOffset
171-
layer.shadowRadius = style.shadowRadius
172-
if let path = style.shadowPath {
173-
layer.shadowPath = path.CGPath
174-
}
175-
}
176-
}
177-
178187
// MARK: Layout
179188
public override func layoutSubviews() {
180189
super.layoutSubviews()

0 commit comments

Comments
 (0)