xcode Swift 如何为 UISegmentedControl 设置默认文本颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42667744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift how to set default text color for UISegmentedControl?
提问by Kris Roofe
I want to set colors for the text and background of UISegmentedControl. So I've four colors to set, the default and selected color of the text and background.
我想为 UISegmentedControl 的文本和背景设置颜色。所以我有四种颜色要设置,文本和背景的默认颜色和选定颜色。
I can use tintColor
and backgroundColor
to set the background. But how to set the default text color, not same as the tint color?
我可以使用tintColor
和backgroundColor
来设置背景。但是如何设置默认文本颜色,与色调颜色不同?
Note: here is swift3 not the older language. I'm a beginner of ios, I just start from the swift3, have no experience of the former language.
注意:这里是 swift3 而不是旧语言。我是ios的初学者,我刚从swift3开始,对以前的语言没有经验。
Any help will be appreciated.
任何帮助将不胜感激。
回答by Sumit Oberoi
UISegmentedControl.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.redColor()], forState: .Selected)
回答by budidino
Swift 4.2 +code to update text color for your UISegmentedControl
(sc
in example bellow):
Swift 4.2 +为您更新文本颜色的代码UISegmentedControl
(sc
在下面的示例中):
// selected option color
sc.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.green], for: .selected)
// color of other options
sc.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.white], for: .normal)
回答by Kris Roofe
Here is the workable one:
这是可行的:
//default background color
customSC.backgroundColor = UIColor(red:65/255.0, green:140/255.0, blue:218/255.0, alpha: 1.0)
//selected background color
customSC.tintColor = UIColor(red:65/255.0, green:140/255.0, blue:218/255.0, alpha: 1.0)
//selected title text color
customSC.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.white], for: UIControlState.selected)
//default title text color
customSC.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.darkGray], for: UIControlState.normal)
回答by David DelMonte
Swift 4.2 +
斯威夫特 4.2 +
segmentedControl.tintColor = .black //background
segmentedControl.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.white], for: .selected)
segmentedControl.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.red], for: .normal)
回答by David DelMonte
customSC.backgroundColor= UIColor(red: 0.5, greep: 0.5, blue: 0.5, alpha: 1.0)
customSC.tintColor= UIColor(red: 0.5, greep: 0.5, blue: 0.5, alpha: 1.0)