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

问题描述 投票:39回答:13

如何以编程方式更改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

问候

swift uisegmentedcontrol
13个回答
77
投票

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)

1
投票

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)

        }
    }

1
投票

Xamarin / C#解决方案

以下是根据批准的答案使用UIAppearance进行更改的细分:

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

0
投票

更进一步的是,我一直在玩这些代码,并找到了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()中的实现工具相同的部分:

yourSegmentedControl.setFontSize(20)

-2
投票

用于快速3

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

24
投票

对于Swift 4

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

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

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


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)

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

更改所有段控件,使用:

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

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

4
投票

通过扩展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)

2
投票

对于Swift 4

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

1
投票

使用@Alvin George的答案,因为我认为添加扩展名来操纵它(并针对Swift 4进行改进是一个好主意:

[创建像UISegmentedControl+Additions.swift这样的UISegmentedControl的扩展类

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)
© www.soinside.com 2019 - 2024. All rights reserved.