10

I have used following code to set background of UISegmentedControl.

homeSegment.setDividerImage(UIImage(named: "seperator.png"), forLeftSegmentState: UIControlState.Normal, rightSegmentState: UIControlState.Normal, barMetrics: UIBarMetrics.Default)



homeSegment.setBackgroundImage(UIImage(named: "squareSegment.png"), forState: UIControlState.Selected, barMetrics: UIBarMetrics.Default)
homeSegment.setBackgroundImage(UIImage(named: "squareSegment.png"), forState: UIControlState.Normal, barMetrics: UIBarMetrics.Default)

How can I set text color of selected segment?

Abubakr Dar
  • 4,078
  • 4
  • 22
  • 28
Saqib Omer
  • 5,387
  • 7
  • 50
  • 71

4 Answers4

15

I accomplish this by using setTitleTextAttributes. The example below uses a dictionary to set attributes since you most likely want to set font type and size at the same time. Try this to set your selected segment text color to red:

let segAttributes: NSDictionary = [
       NSForegroundColorAttributeName: UIColor.redColor(), 
                  NSFontAttributeName: UIFont(name: "Avenir-MediumOblique", size: 20)!
]

homeSegment.setTitleTextAttributes(segAttributes as [NSObject : AnyObject], forState: UIControlState.Selected)

Example:

enter image description here

Automate This
  • 30,726
  • 11
  • 60
  • 82
  • how to you set a different color for each segment? Let say i want the first one to be red when highlighted but the second one to be black. How would i achieve that? – tapizquent Jan 29 '19 at 02:13
  • @JoseTapizquent - This is older code but check out [my answer](https://stackoverflow.com/a/24709849/2521004) to this other question. It addresses selected state tint. – Automate This Jan 29 '19 at 14:59
13

for Swift 4.0

let fontAttribute = [NSAttributedStringKey.font: UIFont(name: "Montserrat-Regular", size: 12)!, 
                       NSAttributedStringKey.foregroundColor: UIColor.red]

segmentedControl.setTitleTextAttributes(fontAttribute, for: .normal)
huseyinmetin
  • 131
  • 1
  • 6
2

@Portland Runner Answer is for the older version of Swift.

Swift 3.0, this works.

Logic is same as the @Portland Runner, but the syntax are modified and updated according to Swift 3.0

Here this code I have implemented is the extension for the Segment control and can be used for all the segment controls in the application, where the set of code has to defined in application class.

extension UISegmentedControl {
func setSegmentStyle() {
    setBackgroundImage(imageWithColor(color: backgroundColor!), for: .normal, barMetrics: .default)
    setBackgroundImage(imageWithColor(color: tintColor!), for: .selected, barMetrics: .default)
    setDividerImage(imageWithColor(color: UIColor.clear), forLeftSegmentState: .normal, rightSegmentState: .normal, barMetrics: .default)



     let segAttributes: NSDictionary = [
            NSForegroundColorAttributeName: UIColor.gray,
            NSFontAttributeName: UIFont(name: "System-System", size: 14)!
        ]

        setTitleTextAttributes(segAttributes as [NSObject : AnyObject], for: UIControlState.selected)
    }

    // create a 1x1 image with this color
    private func imageWithColor(color: UIColor) -> UIImage {
        let rect = CGRect(x: 0.0, y: 0.0, width:  1.0, height: 1.0)
        UIGraphicsBeginImageContext(rect.size)
        let context = UIGraphicsGetCurrentContext()
        context!.setFillColor(color.cgColor);
        context!.fill(rect);
        let image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image!
    }
}

Everywhere for the segments the below code can be used

 self.mySegment.setSegmentStyle()

enter image description here

BHUVANESH MOHANKUMAR
  • 2,747
  • 1
  • 33
  • 33
1

For Swift 5.6, Please try below code line.

    segmentedControl.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.white], for: 
    .normal)
Okan TEL
  • 26
  • 3