更改UISegmented控制器字体

时间:2015-11-04 17:32:43

标签: swift uisegmentedcontrol

有谁知道如何更改自定义UISegmented控制器的字体。在代码中,IB没有做我想用控制器做的所有其他事情!

我的代码是:

let items = ["Classic", "Cool Blue", "Safari", "Motors", "Old School"]
let customSC = UISegmentedControl(items: items)
customSC.selectedSegmentIndex = 3
customSC.frame = CGRectMake(ScreenWidth/2 - (ScreenWidth*0.342), ScreenHeight/2, (ScreenWidth*0.684), (ScreenHeight*0.085))
customSC.layer.cornerRadius = 5.0
customSC.backgroundColor = UIColor.clearColor()
customSC.tintColor = UIColor.whiteColor()
customSC.font // font doesn't exist.... 

// add the SC target action
customSC.addTarget(self, action: "changeScheme:", forControlEvents:.ValueChanged)
self.view.addSubview(customSC)

事先感谢任何可以提供帮助的人!

1 个答案:

答案 0 :(得分:1)

我认为您正在寻找的方法是:

func setTitleTextAttributes(_ attributes: [NSObject : AnyObject]?, forState state: UIControlState)

类似的东西:

let attrs = [NSFontAttributeName:UIFont(name: "AmericanTypewriter", size: 10.0)!]
customSC.setTitleTextAttributes(attrs, forState: UIControlState.Normal)