Swift how to set default text color for UISegmentedControl?

LF00 picture LF00 · Mar 8, 2017 · Viewed 9.5k times · Source

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.

Answer

budiDino picture budiDino · Sep 26, 2018

Swift 4.2 + code to update text color for your UISegmentedControl (sc in example bellow):

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

// color of other options
sc.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.white], for: .normal)