How to change font color of UISegmentedControl

前端 未结 12 1301
广开言路
广开言路 2020-12-04 16:30

I try to change font color from white to black for UISegmentedControl (for iOS 4.*)

UISegmentedControl *button = [[[UISegmentedControl alloc] in         


        
相关标签:
12条回答
  • 2020-12-04 16:53

    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)
    
    0 讨论(0)
  • 2020-12-04 16:54

    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)
    
    0 讨论(0)
  • 2020-12-04 17:01

    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)
    
    0 讨论(0)
  • 2020-12-04 17:01

    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
    
    0 讨论(0)
  • 2020-12-04 17:02

    in Swift 5 you can change color with this syntax

    Swift 5

    let titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.red]
    groupSegment.setTitleTextAttributes(titleTextAttributes, for: .selected)
    
    0 讨论(0)
  • 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];
    
    0 讨论(0)
提交回复
热议问题