一尘不染

如何以编程方式快速更改uisegmentedcontrol的字体大小和字体名称?

swift

如何以编程方式更改uisegmentedcontrol的字体大小和字体名称?我用过迅速。

这是我的代码:

self.mysegmentedControl = UISegmentedControl(items: [
        NSLocalizedString("Aaaaaa", comment: ""),
        NSLocalizedString("Bbbbbb", comment: ""),
        NSLocalizedString("Cccccc", comment: ""),
        NSLocalizedString("Dddddd", comment: ""),
        NSLocalizedString("Eeeeee", comment: ""),
        ])
self.mysegmentedControl.addTarget(self, action: "mysegmentedControlDidChange:", forControlEvents: .ValueChanged)
self.mysegmentedControl.selectedSegmentIndex = 0

问候。


阅读 331

收藏
2020-07-07

共1个答案

一尘不染

UI可以使用控件外观,最好将其添加到应用程序的委托,didFinishLaunchingWithOptions方法中,如果只想一次为项目中的每个UISegmentedControls设置相同的属性,则可以使用此控件:

let 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", size: 16.0)!, forKey: NSFontAttributeName)
seg.setTitleTextAttributes(attr as [NSObject : AnyObject] , forState: .Normal)
2020-07-07