Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift how to set default text color for UISegmentedControl?

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.

like image 775
LF00 Avatar asked Mar 08 '17 09:03

LF00


People also ask

How do I change the segment control color in Swift?

To change the color/font of the selected segment titles, use setTitleTextAttributes with a state of . selected / UIControlStateSelected . If you create a segmented control with images, if the images are created as template images, then the segmented control's tintColor will be used to color the images.

How do I change the color of my Uilabel Swift?

The easiest workaround is create dummy labels in IB, give them the text the color you like and set to hidden. You can then reference this color in your code to set your label to the desired color. The only way I could change the text color programmatically was by using the standard colors, UIColor.


1 Answers

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)
like image 186
budiDino Avatar answered Sep 22 '22 15:09

budiDino