我想为UISegmentedControl的文本和背景设置颜色。因此,我有四种颜色要设置,默认和选定的颜色的文本和背景。
我可以使用tintColor和backgroundColor来设置背景。但是,如何设置默认的文本颜色,而不是相同的颜色呢?
Note:这里是swift3,而不是旧语言。我是ios的初学者,我刚从swift3开始,对以前的语言没有经验。
任何帮助都将不胜感激。
发布于 2017-03-08 10:26:41
UISegmentedControl.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.redColor()], forState: .Selected)发布于 2018-09-26 14:26:12
Swift 5+代码以更新UISegmentedControl的文本颜色(本例中为sc)
// selected option color
sc.setTitleTextAttributes([.foregroundColor: UIColor.green], for: .selected)
// color of other options
sc.setTitleTextAttributes([.foregroundColor: UIColor.white], for: .normal)发布于 2019-03-02 14:38:37
Swift 4.2 +
segmentedControl.tintColor = .black //background
segmentedControl.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.white], for: .selected)
segmentedControl.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.red], for: .normal)https://stackoverflow.com/questions/42667744
复制相似问题