22

I want to set colors for the text and background of UISegmentedControl. So I've four colors to set, the default and selected color of the text and background.

I can use tintColor and backgroundColor to set the background. But how to set the default text color, not same as the tint color?

Note: here is swift3 not the older language. I'm a beginner of ios, I just start from the swift3, have no experience of the former language.

Any help will be appreciated.

LF00
  • 27,015
  • 29
  • 156
  • 295

6 Answers6

34

Swift 5+ code to update text color for your UISegmentedControl (sc in this example)

// selected option color
sc.setTitleTextAttributes([.foregroundColor: UIColor.green], for: .selected)

// color of other options
sc.setTitleTextAttributes([.foregroundColor: UIColor.white], for: .normal)
budiDino
  • 13,044
  • 8
  • 95
  • 91
19
UISegmentedControl.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.redColor()], forState: .Selected)
Sumit Oberoi
  • 3,455
  • 2
  • 18
  • 18
18

Swift 4.2 +

segmentedControl.tintColor = .black //background
segmentedControl.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.white], for: .selected)
segmentedControl.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.red], for: .normal)
ICL1901
  • 7,632
  • 14
  • 90
  • 138
6

Here is the workable one:

// default background color
customSC.backgroundColor = UIColor(red:65/255.0, green:140/255.0, blue:218/255.0, alpha: 1.0)

// selected background color
customSC.tintColor = UIColor(red:65/255.0, green:140/255.0, blue:218/255.0, alpha: 1.0)

// selected title text color
customSC.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.white], for: UIControlState.selected)

// default title text color
customSC.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.darkGray], for: UIControlState.normal)
budiDino
  • 13,044
  • 8
  • 95
  • 91
LF00
  • 27,015
  • 29
  • 156
  • 295
0
customSC.backgroundColor= UIColor(red: 0.5, greep: 0.5, blue: 0.5, alpha: 1.0)
customSC.tintColor= UIColor(red: 0.5, greep: 0.5, blue: 0.5, alpha: 1.0)
  • I want to set the default text color. – LF00 Mar 14 '17 at 15:10
  • While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – DimaSan Mar 14 '17 at 15:29
  • 1
    I find it, check my answer. – LF00 Mar 15 '17 at 06:51
0

Here is a quick way to handle it on storyboard:

extension UISegmentedControl {
@IBInspectable var textSelectedColor: UIColor {
    get {
        return self.textSelectedColor
    }
    set {
        self.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: newValue], for: .selected)
    }
}

@IBInspectable var textNormalColor: UIColor {
    get {
        return self.textNormalColor
    }
    set {
        self.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: newValue], for: .normal)
    }
}}
Ömer Karaca
  • 880
  • 1
  • 8
  • 13