如何以编程方式更改uisegmentedcontrol的字体大小和字体名称?我用swift
这里是我的代码:
self.mysegmentedControl = UISegmentedControl(items: [ NSLocalizedString("Aaaaaa",comment: ""),NSLocalizedString("Bbbbbb",NSLocalizedString("Cccccc",NSLocalizedString("Dddddd",NSLocalizedString("Eeeeee",]) self.mysegmentedControl.addTarget(self,action: "mysegmentedControlDidChange:",forControlEvents: .ValueChanged) self.mysegmentedControl.selectedSegmentIndex = 0
问候。
UI可以使用控件外观,最好添加它是在应用程序委托中,didFinishLaunchingWithOptions方法,如果要为项目中的每个UISegmentedControl设置相同的属性,请使用此方法只需一次:
原文链接:/swift/320723.htmllet attr = NSDictionary(object: UIFont(name: "HelveticaNeue-Bold",size: 16.0)!,forKey: NSFontAttributeName) UISegmentedControl.appearance().setTitleTextAttributes(attr as [NSObject : AnyObject],forState: .Normal)
但是,如果要将属性设置为只有一个UISegmentedControl,或者如果要根据某些条件更频繁地更改它,则使用此UISegmentedControl方法:
func setTitleTextAttributes(_ attributes: [NSObject : AnyObject]?,forState state: UIControlState)
例:
let attr = NSDictionary(object: UIFont(name: "HelveticaNeue-Bold",forKey: NSFontAttributeName) seg.setTitleTextAttributes(attr as [NSObject : AnyObject],forState: .Normal)