@@ -35,11 +35,18 @@ open class SWSegmentedControl: UIControl {
35
35
}
36
36
37
37
// Wait for a day UIFont will be inspectable
38
+ /// For unselected state
38
39
@IBInspectable open var font : UIFont = DefaultTitleFont {
39
40
didSet {
40
41
self . configureView ( )
41
42
}
42
43
}
44
+ /// For selected state
45
+ @IBInspectable open var selectedFont : UIFont = DefaultTitleFont {
46
+ didSet {
47
+ configureView ( )
48
+ }
49
+ }
43
50
44
51
@IBInspectable open var badgeFont : UIFont = DefaultBadgeFont {
45
52
didSet {
@@ -221,7 +228,11 @@ open class SWSegmentedControl: UIControl {
221
228
}
222
229
for index in 0 ..< self . numberOfSegments {
223
230
let button = SWSegmentedItem ( )
224
- self . configureButton ( button)
231
+ if index == _selectedSegmentIndex {
232
+ configureButton ( button, with: selectedFont)
233
+ } else {
234
+ configureButton ( button, with: font)
235
+ }
225
236
button. translatesAutoresizingMaskIntoConstraints = false
226
237
button. title = titleForSegmentAtIndex ( index)
227
238
button. addTarget ( self , action: #selector( SWSegmentedControl . didTapButton ( _: ) ) , for: . touchUpInside)
@@ -316,14 +327,18 @@ open class SWSegmentedControl: UIControl {
316
327
}
317
328
318
329
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
+ }
321
336
}
322
337
}
323
338
324
- private func configureButton( _ button: SWSegmentedItem ) {
339
+ private func configureButton( _ button: SWSegmentedItem , with font : UIFont ) {
325
340
button. badgeView. label. font = self . badgeFont
326
- button. textLabel. font = self . font
341
+ button. textLabel. font = font
327
342
button. setTitleColor ( self . colorToUse ( self . titleColor) , for: . selected)
328
343
button. setTitleColor ( self . unselectedTitleColor, for: . normal)
329
344
button. badgeColor = colorToUse ( badgeColor)
@@ -350,6 +365,14 @@ open class SWSegmentedControl: UIControl {
350
365
351
366
delegate? . segmentedControl ? ( self , didDeselectItemAtIndex: selectedSegmentIndex)
352
367
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
+ }
353
376
}
354
377
355
378
// MARK: - Layout Helpers
0 commit comments