问题描述
如何以编程方式更改 uisegmentedcontrol 的字体大小和字体名称?我用的是swift.
How to change font size and font name of uisegmentedcontrol programmatically? I used swift.
这是我的代码:
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
问候.
推荐答案
UI 可以使用控件外观,最好在 app delegate 中添加,didFinishLaunchingWithOptions
方法,如果要设置就用这个将相同的属性添加到项目中的每个 UISegmentedControls 一次:
UI can use control appearance, best place to add it is in app delegate, didFinishLaunchingWithOptions
method, use this if you want to set up the same attribute to every UISegmentedControls in your project just once:
let attr = NSDictionary(object: UIFont(name: "HelveticaNeue-Bold", size: 16.0)!, forKey: NSFontAttributeName)
UISegmentedControl.appearance().setTitleTextAttributes(attr as [NSObject : AnyObject] , forState: .Normal)
但是如果您只想为一个 UISegmentedControl 设置属性,或者如果您想根据某些条件更频繁地更改它,请使用此 UISegmentedControl 方法:
But if you are going to set up attributes to just one UISegmentedControl or if you want to change it more often base on some condition use this, UISegmentedControl method:
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)
这篇关于如何在swift上以编程方式更改uisegmentedcontrol的字体大小和字体名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!