Skip to content

Commit 4f3a2c8

Browse files
committed
Add support for separate font for selected state
1 parent 7e49242 commit 4f3a2c8

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

Pod/Classes/SWSegmentedControl.swift

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,18 @@ open class SWSegmentedControl: UIControl {
3535
}
3636

3737
// Wait for a day UIFont will be inspectable
38+
/// For unselected state
3839
@IBInspectable open var font: UIFont = DefaultTitleFont {
3940
didSet {
4041
self.configureView()
4142
}
4243
}
44+
/// For selected state
45+
@IBInspectable open var selectedFont: UIFont = DefaultTitleFont {
46+
didSet {
47+
configureView()
48+
}
49+
}
4350

4451
@IBInspectable open var badgeFont: UIFont = DefaultBadgeFont {
4552
didSet {
@@ -221,7 +228,11 @@ open class SWSegmentedControl: UIControl {
221228
}
222229
for index in 0..<self.numberOfSegments {
223230
let button = SWSegmentedItem()
224-
self.configureButton(button)
231+
if index == _selectedSegmentIndex {
232+
configureButton(button, with: selectedFont)
233+
} else {
234+
configureButton(button, with: font)
235+
}
225236
button.translatesAutoresizingMaskIntoConstraints = false
226237
button.title = titleForSegmentAtIndex(index)
227238
button.addTarget(self, action: #selector(SWSegmentedControl.didTapButton(_:)), for: .touchUpInside)
@@ -316,14 +327,18 @@ open class SWSegmentedControl: UIControl {
316327
}
317328

318329
private func configureButtons() {
319-
for button in buttons {
320-
self.configureButton(button)
330+
for (index, button) in buttons.enumerated() {
331+
if index == _selectedSegmentIndex {
332+
configureButton(button, with: selectedFont)
333+
} else {
334+
configureButton(button, with: font)
335+
}
321336
}
322337
}
323338

324-
private func configureButton(_ button: SWSegmentedItem) {
339+
private func configureButton(_ button: SWSegmentedItem, with font: UIFont) {
325340
button.badgeView.label.font = self.badgeFont
326-
button.textLabel.font = self.font
341+
button.textLabel.font = font
327342
button.setTitleColor(self.colorToUse(self.titleColor), for: .selected)
328343
button.setTitleColor(self.unselectedTitleColor, for: .normal)
329344
button.badgeColor = colorToUse(badgeColor)
@@ -350,6 +365,14 @@ open class SWSegmentedControl: UIControl {
350365

351366
delegate?.segmentedControl?(self, didDeselectItemAtIndex: selectedSegmentIndex)
352367
delegate?.segmentedControl?(self, didSelectItemAtIndex: index)
368+
// update the font after selection
369+
for (index, b) in buttons.enumerated() {
370+
if index == _selectedSegmentIndex {
371+
b.textLabel.font = selectedFont
372+
} else {
373+
b.textLabel.font = font
374+
}
375+
}
353376
}
354377

355378
// MARK: - Layout Helpers

0 commit comments

Comments
 (0)