如何在swift上以编程方式更改uisegmentedcontrol的字体大小和字体名称?

use*_*725 38 uisegmentedcontrol swift

如何以编程方式更改uisegmentedcontrol的字体大小和字体名称?我使用了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
Run Code Online (Sandbox Code Playgroud)

问候.

Gre*_*reg 77

UI可以使用控件外观,添加它的最佳位置是在app delegate,didFinishLaunchingWithOptions方法中,如果要为项目中的每个UISegmentedControls设置相同的属性,只需使用一次:

let attr = NSDictionary(object: UIFont(name: "HelveticaNeue-Bold", size: 16.0)!, forKey: NSFontAttributeName)
UISegmentedControl.appearance().setTitleTextAttributes(attr as [NSObject : AnyObject] , forState: .Normal)
Run Code Online (Sandbox Code Playgroud)

但是,如果您要将属性设置为仅一个UISegmentedControl,或者如果您想根据某些条件更频繁地更改它,请使用此UISegmentedControl方法:

func setTitleTextAttributes(_ attributes: [NSObject : AnyObject]?,
                   forState state: UIControlState)
Run Code Online (Sandbox Code Playgroud)

例:

let attr = NSDictionary(object: UIFont(name: "HelveticaNeue-Bold", size: 16.0)!, forKey: NSFontAttributeName)
seg.setTitleTextAttributes(attr as [NSObject : AnyObject] , forState: .Normal)
Run Code Online (Sandbox Code Playgroud)


小智 22

对于Swift 4

    let font: [AnyHashable : Any] = [NSAttributedStringKey.font : UIFont.systemFont(ofSize: 17)]
    segmentedControl.setTitleTextAttributes(font, for: .normal)
Run Code Online (Sandbox Code Playgroud)


小智 19

这个答案是过时的,但对于那些正在寻求快速解决方案的人来说,你可能想尝试这种方法:

func stylizeFonts(){
    let normalFont = UIFont(name: "Helvetica", size: 16.0)
    let boldFont = UIFont(name: "Helvetica-Bold", size: 16.0)

    let normalTextAttributes: [NSObject : AnyObject] = [
        NSForegroundColorAttributeName: UIColor.blackColor(),
        NSFontAttributeName: normalFont!
    ]

    let boldTextAttributes: [NSObject : AnyObject] = [
        NSForegroundColorAttributeName : UIColor.whiteColor(),
        NSFontAttributeName : boldFont!,
    ]

    self.setTitleTextAttributes(normalTextAttributes, forState: .Normal)
    self.setTitleTextAttributes(normalTextAttributes, forState: .Highlighted)
    self.setTitleTextAttributes(boldTextAttributes, forState: .Selected)
}
Run Code Online (Sandbox Code Playgroud)

确保在viewDidLoad中添加stylizeFonts(),或者如果您是子类,则添加为单独的函数.


Chr*_*son 11

Greg的答案更新为Swift3:

let attr = NSDictionary(object: UIFont(name: "OpenSans", size: 12.0)!, forKey: NSFontAttributeName as NSCopying)
UISegmentedControl.appearance().setTitleTextAttributes(attr as [NSObject : AnyObject] , for: .normal)
Run Code Online (Sandbox Code Playgroud)


小智 8

@Jordan Montel 答案针对 Swift 5 进行了更新。用于更改字体和字体大小的工作代码。

extension UISegmentedControl {
    func font(name:String?, size:CGFloat?) {
        let attributedSegmentFont = NSDictionary(object: UIFont(name: name!, size: size!)!, forKey: NSAttributedString.Key.font as NSCopying)
        setTitleTextAttributes(attributedSegmentFont as [NSObject : AnyObject] as [NSObject : AnyObject] as? [NSAttributedString.Key : Any], for: .normal)
    }
}
Run Code Online (Sandbox Code Playgroud)

用法“segControl”是您的 IBOutlet 名称:

segControl.font(name: "Your Font Name", size: 10)
Run Code Online (Sandbox Code Playgroud)


A.G*_*A.G 7

Swift 2.0

    override func viewDidLayoutSubviews() {
        let attributedSegmentFont = NSDictionary(object: UIFont(name: "Roboto-Regular", size: 14.0)!, forKey: NSFontAttributeName)

    dashBoardSegment.setTitleTextAttributes(attributedSegmentFont as [NSObject : AnyObject], forState: .Normal)
    }
Run Code Online (Sandbox Code Playgroud)

更改所有段控件,使用:

UISegmentedControl.appearance().setTitleTextAttributes(attributedSegmentFont as [NSObject : AnyObject], forState: .Normal)
Run Code Online (Sandbox Code Playgroud)

使用Swift扩展:

extension UISegmentedControl{
    func changeTitleFont(newFontName:String?, newFontSize:CGFloat?){
        let attributedSegmentFont = NSDictionary(object: UIFont(name: newFontName!, size: newFontSize!)!, forKey: NSFontAttributeName)
        setTitleTextAttributes(attributedSegmentFont as [NSObject : AnyObject], forState: .Normal)
    }
}
Run Code Online (Sandbox Code Playgroud)

实施扩展:

override func viewDidLayoutSubviews() {
    dashBoardSegment.changeTitleFont("Roboto-Regular", newFontSize: 14.0)
}
Run Code Online (Sandbox Code Playgroud)


Bas*_*lam 6

Swift3

override func viewDidLayoutSubviews() {
    let attr = NSDictionary(object: UIFont(name: "Chalkduster", size: 14.0)!, forKey: NSFontAttributeName as NSCopying)
    segmentedControl.setTitleTextAttributes(attr as [NSObject : AnyObject] , for: .normal)
}
Run Code Online (Sandbox Code Playgroud)


小智 5

除了 jeff-ziligy 答案之外,我还研究了代码并找到了 Swift 5.1 的更新代码:

extension UISegmentedControl {

func setFontSize(fontSize: CGFloat) {

    let normalTextAttributes: [NSObject : AnyObject] = [
        NSAttributedString.Key.foregroundColor as NSObject: UIColor.black,
        NSAttributedString.Key.font as NSObject : UIFont.systemFont(ofSize: fontSize, weight: UIFont.Weight.medium)
    ]

    let boldTextAttributes: [NSObject : AnyObject] = [
        NSAttributedString.Key.foregroundColor as NSObject : UIColor.black,
        NSAttributedString.Key.font as NSObject : UIFont.systemFont(ofSize: fontSize, weight: UIFont.Weight.medium),
    ]

    self.setTitleTextAttributes(normalTextAttributes as? [NSAttributedString.Key : Any], for: .normal)
    self.setTitleTextAttributes(normalTextAttributes as? [NSAttributedString.Key : Any], for: .highlighted)
    self.setTitleTextAttributes(boldTextAttributes as? [NSAttributedString.Key : Any], for: .selected)
  }
}
Run Code Online (Sandbox Code Playgroud)

ViewDidLoad() 中的 up 实现部分是相同的:

yourSegmentedControl.setFontSize(20)
Run Code Online (Sandbox Code Playgroud)