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

how to change font size and font name of uisegmentedcontrol programmatically on 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

问候。

UI 可以使用控件外观,最好的添加位置是在 app 委托中,didFinishLaunchingWithOptions 方法,如果你想为每个 UI 设置相同的属性,请使用此方法您项目中的 SegmentedControls 一次:

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)

此答案已过时,但对于那些正在 swift 中寻找解决方案的人,您可能想尝试这种方法:

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)
}

请确保在您的 viewDidLoad 中添加 stylizeFonts() 或作为一个单独的函数(如果您正在子类化)。

通过扩展 mvien 的优秀 Swift 方法。我创建了一个 SegmentedControl 扩展:

extension UISegmentedControl {

    func setFontSize(fontSize: CGFloat) {

        let normalTextAttributes: [NSObject : AnyObject] = [
            NSForegroundColorAttributeName: UIColor.blackColor(),
            NSFontAttributeName: UIFont.systemFontOfSize(fontSize, weight: UIFontWeightRegular)
        ]

        let boldTextAttributes: [NSObject : AnyObject] = [
            NSForegroundColorAttributeName : UIColor.whiteColor(),
            NSFontAttributeName : UIFont.systemFontOfSize(fontSize, weight: UIFontWeightMedium),
        ]

        self.setTitleTextAttributes(normalTextAttributes, forState: .Normal)
        self.setTitleTextAttributes(normalTextAttributes, forState: .Highlighted)
        self.setTitleTextAttributes(boldTextAttributes, forState: .Selected)
    }
}

只需将上面的扩展代码添加到您的项目中,然后像这样使用:

yourSegmentedControl.setFontSize(20)

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)
    }

更改所有段控件,使用:

UISegmentedControl.appearance().setTitleTextAttributes(attributedSegmentFont as [NSObject : AnyObject], forState: .Normal)

使用 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)
    }
}

正在实施扩展:

override func viewDidLayoutSubviews() {
    dashBoardSegment.changeTitleFont("Roboto-Regular", newFontSize: 14.0)
}

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)

对于swift 3

UISegmentedControl.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.white], for: .selected)

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)
}

对于Swift 4

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

使用@Alvin George 的回答是因为我认为添加用于操作的扩展是个好主意(并针对 Swift 4 改进它):

创建 UISegmentedControl 的扩展 class,例如 UISegmentedControl+Additions.swift

import Foundation
import UIKit

extension UISegmentedControl {
    func font(name:String?, size:CGFloat?) {
        let attributedSegmentFont = NSDictionary(object: UIFont(name: name!, size: size!)!, forKey: NSAttributedStringKey.font as NSCopying)
        setTitleTextAttributes(attributedSegmentFont as [NSObject : AnyObject], for: .normal)
    }
}

在您的代码中使用它:

segmentedControl?.font(name: "My Font Name", size: 12)

Swift 4

@IBOutlet var sgmentTypeSelect: UISegmentedControl!{
        didSet {

            let normalTextAttributes: [NSAttributedStringKey : AnyObject] = [
                NSAttributedStringKey.foregroundColor : UIColor.themeGold,
                NSAttributedStringKey.font : UIFont.defaultMontserratFont(style: "SemiBold", size: 13)
            ]

            let selectedTextAttributes: [NSAttributedStringKey : AnyObject] = [
                NSAttributedStringKey.foregroundColor : UIColor.black,
                NSAttributedStringKey.font : UIFont.defaultMontserratFont(style: "Bold", size: 13)
            ]

            sgmentTypeSelect.setTitleTextAttributes(normalTextAttributes, for: .normal)
            sgmentTypeSelect.setTitleTextAttributes(normalTextAttributes, for: .highlighted)
            sgmentTypeSelect.setTitleTextAttributes(selectedTextAttributes, for: .selected)

        }
    }

对于Swift 4

let font = UIFont.systemFont(ofSize: 16)
segmentedControl.setTitleTextAttributes([NSAttributedString.Key.font: font],
                                            for: .normal)

Xamarin/C#解决方案

以下是根据批准的答案更改 UIAppearance 的细目:

var font = UIFont.FromName("HelveticaNeue-Bold", 16f);
var textAttributes = new UITextAttributes { Font = font };
UISegmentedControl.Appearance.SetTitleTextAttributes(textAttributes, 
    UIControlState.Normal);

除了 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)
  }
}

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

yourSegmentedControl.setFontSize(20)

@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)
    }
}

用法 "segControl" 是您的 IBOutlet 名称:

segControl.font(name: "Your Font Name", size: 10)

Objective-C版本:

NSDictionary *attributes = @{ NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Light" size:17] };
[segmentedControl setTitleTextAttributes:attributes forState:UIControlStateNormal];