折腾:
期间,用代码:
let actionList:[String] = ["登录", "注册"]
var switchActionSegment:UISegmentedControl
self.switchActionSegment = UISegmentedControl(items: actionList)
self.switchActionSegment.backgroundColor = AppMainColor
self.switchActionSegment.setTitleTextAttributes(["NSFontAttributeName" : UIFont.systemFontOfSize(20), "NSForegroundColorAttributeName" : UIColor.whiteColor()], forState: UIControlState.Normal)
但是文字没有生效:
文字还是很小,颜色也不是白色
uisegmentedcontrol setTitleTextAttributes not work
ios – UISegmentedControl setTitleTextAttributes does not work – Stack Overflow
换成:
UISegmentedControl.appearance().setTitleTextAttributes(["NSFontAttributeName" : UIFont.systemFontOfSize(20), "NSForegroundColorAttributeName" : UIColor.whiteColor()], forState: UIControlState.Normal)
没用。
UITextAttributes font not working for UISegmentedControl selected state – Xamarin Forums
uisegmentedcontrol settitletextattributes not working
uisegmentedcontrol settitletextattributes
How can I change the text color of a segmented control in Swift?
iphone – How to change font color of UISegmentedControl – Stack Overflow
ios – UISegmentedControl only changes text color when revisiting ViewController – Stack Overflow
最终才发现,是自己写错了,把:
NSFontAttributeName
写成了:
“NSFontAttributeName”
所以改为:
self.switchActionSegment.setTitleTextAttributes([NSFontAttributeName : UIFont.systemFontOfSize(20), NSForegroundColorAttributeName : UIColor.whiteColor()], forState: UIControlState.Normal)
self.switchActionSegment.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.redColor()], forState: UIControlState.Selected)
即可生效:
转载请注明:在路上 » [已解决]UISegmentControl中设置setTitleTextAttributes没效果