I try to change font color from white to black for UISegmentedControl
(for iOS 4.*)
UISegmentedControl *button = [[[UISegmentedControl alloc] in
Updated for iOS 13:
extension UISegmentedControl
{
func defaultConfiguration(font: UIFont = UIFont.systemFont(ofSize: 12), color: UIColor = UIColor.white)
{
let defaultAttributes = [
NSAttributedString.Key.font: font,
NSAttributedString.Key.foregroundColor: color
]
setTitleTextAttributes(defaultAttributes, for: .normal)
}
func selectedConfiguration(font: UIFont = UIFont.boldSystemFont(ofSize: 12), color: UIColor = UIColor.red)
{
let selectedAttributes = [
NSAttributedString.Key.font: font,
NSAttributedString.Key.foregroundColor: color
]
setTitleTextAttributes(selectedAttributes, for: .selected)
}
}
Updated for Swift 4 - Use this Extension (because extension is always awesome..!!)
extension UISegmentedControl {
func defaultConfiguration(font: UIFont = UIFont.systemFont(ofSize: 12), color: UIColor = UIColor.gray) {
let defaultAttributes = [
NSAttributedStringKey.font.rawValue: font,
NSAttributedStringKey.foregroundColor.rawValue: color
]
setTitleTextAttributes(defaultAttributes, for: .normal)
}
func selectedConfiguration(font: UIFont = UIFont.boldSystemFont(ofSize: 12), color: UIColor = UIColor.red) {
let selectedAttributes = [
NSAttributedStringKey.font.rawValue: font,
NSAttributedStringKey.foregroundColor.rawValue: color
]
setTitleTextAttributes(selectedAttributes, for: .selected)
}
}
and from the respective class, you can use these function,
@IBOutlet weak var segment: UISegmentedControl!
segment.defaultConfiguration()
// or provide paramater as per your requirement
segment.selectedConfiguration(color: .blue)
If you need to change the text color of the highlighted segment in iOS 7, here is a solution (took me awhile to find, but thanks to this post):
Objective-C
[[UISegmentedControl appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor blackColor]} forState:UIControlStateSelected];
Swift
let titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
UISegmentedControl.appearance().setTitleTextAttributes(titleTextAttributes, forState: .Selected)
code to set both states font color to black
Swift 5
let titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black]
segmentedControl.setTitleTextAttributes(titleTextAttributes, for: .normal)
segmentedControl.setTitleTextAttributes(titleTextAttributes, for: .selected)
Swift 4
let titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.black]
segmentedControl.setTitleTextAttributes(titleTextAttributes, for: .normal)
segmentedControl.setTitleTextAttributes(titleTextAttributes, for: .selected)
swift 3.2:
let attributes = [
NSFontAttributeName : bigTextFont,
NSForegroundColorAttributeName : UIColor.blue,
]
segmentedControl?.setTitleTextAttributes(attributes, for: .normal)
note: if You use a custom background color, you will see a glitch in corners (color will fill outside segments..), so use these line to clip it:
segmentedControl!.layer.cornerRadius = 4.0
segmentedControl!.clipsToBounds = true
in Swift 5 you can change color with this syntax
Swift 5
let titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.red]
groupSegment.setTitleTextAttributes(titleTextAttributes, for: .selected)
In iOS 6.0 and above it's very simple:
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont boldSystemFontOfSize:17], NSFontAttributeName,
[UIColor blackColor], NSForegroundColorAttributeName,
nil];
[_segmentedControl setTitleTextAttributes:attributes forState:UIControlStateNormal];
NSDictionary *highlightedAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
[_segmentedControl setTitleTextAttributes:highlightedAttributes forState:UIControlStateSelected];