Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change title text color of a specific segment in UISegmentedControl?

How can I change the text color of just one specific segment of a UISegmentedControl? I want to keep them all normal, except for a specific segment which should be a different color, whether it is selected or not.

like image 319
CompC Avatar asked Sep 03 '25 09:09

CompC


1 Answers

@IBDesignable
    class DesignableSegmentControl: UISegmentedControl{
    }
    extension UISegmentedControl{
        @IBInspectable
        var textColor: UIColor{
            get {
                return self.textColor
            }
            set {
                let unselectedAttributes = [NSAttributedString.Key.foregroundColor: newValue,
                                            NSAttributedString.Key.font:  UIFont.systemFont(ofSize: 13, weight: UIFont.Weight.regular)]
                self.setTitleTextAttributes(unselectedAttributes, for: .normal)
                self.setTitleTextAttributes(unselectedAttributes, for: .selected)
            }
        }
    }

Just change you segment control's class name as shown below:

[change class name]:
enter image description here

[change text color]:
enter image description here

like image 94
Mahendra_lariya Avatar answered Sep 04 '25 22:09

Mahendra_lariya