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

时间:2015-01-14 09:52:50

标签: swift uisegmentedcontrol

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

问候。

15 个答案:

答案 0 :(得分:75)

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)

但是如果您要将属性设置为仅一个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 :(得分:18)

对于Swift 4

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

答案 2 :(得分:16)

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

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(),或者如果您是子类,则添加为单独的函数。

答案 3 :(得分: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)

答案 4 :(得分: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)
}

答案 5 :(得分: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)
}

答案 6 :(得分:3)

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

答案 7 :(得分:2)

对于Swift 4

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

答案 8 :(得分:1)

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

创建UISegmentedControl的扩展类,例如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)

答案 9 :(得分:1)

Xamarin / C#解决方案

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

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

答案 10 :(得分:1)

在回答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()中向上实现的部分相同:

yourSegmentedControl.setFontSize(20)

答案 11 :(得分:1)

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

答案 12 :(得分:0)

快捷键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)

        }
    }

答案 13 :(得分:0)

Objective-C 版本:

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

答案 14 :(得分:-2)

对于swift 3

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