How to change font color of UISegmentedControl

前端 未结 12 1313
广开言路
广开言路 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 17:12

    Just in case to help someone else that get there and is working using swift.

    I will put the two possible ways of doing it. You can change the text attributes in the UIAppearance or directly in the segmented your are working.

    The first example setting the attributes in the appearance, this way you will customize all the segmented controls in your app:

        let attributes = [ NSForegroundColorAttributeName : UIColor.grayColor(),
            NSFontAttributeName : UIFont.systemFontOfSize(20)];
        let attributesSelected = [ NSForegroundColorAttributeName : UIColor.blueColor(),
            NSFontAttributeName : UIFont.systemFontOfSize(20)];
        UISegmentedControl.appearance().setTitleTextAttributes(attributes, forState: UIControlState.Normal)
        UISegmentedControl.appearance().setTitleTextAttributes(attributesSelected, forState: UIControlState.Selected)
    

    The second example, directly in the segmented, will customize only this segmented:

        let attributes = [ NSForegroundColorAttributeName : UIColor.grayColor(),
            NSFontAttributeName : UIFont.systemFontOfSize(20)];
        let attributesSelected = [ NSForegroundColorAttributeName : UIColor.blueColor(),
            NSFontAttributeName : UIFont.systemFontOfSize(20)];
        segmented.setTitleTextAttributes(attributes, forState: UIControlState.Normal)
        segmented.setTitleTextAttributes(attributesSelected, forState: UIControlState.Selected)
    

提交回复
热议问题