How to set the hue color of text for only a selected segment using Swift?

I used the following code to set the background of the 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 to set the text color for the selected segment?

+6
source share
3 answers

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

 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

+7
source

for Swift 4.0

 let fontAttribute = [NSAttributedStringKey.font: UIFont(name: "Montserrat-Regular", size: 12)!, NSAttributedStringKey.foregroundColor: UIColor.red] segmentedControl.setTitleTextAttributes(fontAttribute, for: .normal) 
+3
source

@Portland Runner Answer for the old version of Swift.

Swift 3.0, it works.

The logic is the same as Runner @Portland, but the syntax has been changed and updated in accordance with Swift 3.0

Here, this code that I implemented is an extension for segment control and can be used for all segment controls in the application, where the code set must be defined in the 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! } } 

You can use the code below for segments.

  self.mySegment.setSegmentStyle() 

enter image description here

0
source

Source: https://habr.com/ru/post/982428/


All Articles