改变UISegmentedControl的字体大小

235

请问有谁能告诉我如何更改 UISegmentedControl 的字体类型和大小?


1
有没有在IB中的解决方案,而不是代码? - oky_sabeni
你不能在IB中完成它。当前2021的语法是:UISegmentedControl.appearance().setTitleTextAttributes([NSAttributedString.Key.font: _font], for: .normal) - Fattie
17个回答

528

我遇到了相同的问题。 这段代码设置整个分段控件的字体大小。 为设置字体类型可能需要类似的方法。 请注意,这仅适用于iOS5+。

Obj C:

->

Objective-C:

UIFont *font = [UIFont boldSystemFontOfSize:12.0f];
NSDictionary *attributes = [NSDictionary dictionaryWithObject:font
                                                       forKey:NSFontAttributeName];
[segmentedControl setTitleTextAttributes:attributes 
                                forState:UIControlStateNormal];

编辑:UITextAttributeFont 已被弃用 - 请改用 NSFontAttributeName。

编辑 #2:对于 Swift 4,NSFontAttributeName 已更改为 NSAttributedStringKey.font。

Swift 5:

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

Swift 4:

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

Swift 3:

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

Swift 2.2:

let font = UIFont.systemFontOfSize(16)
segmentedControl.setTitleTextAttributes([NSFontAttributeName: font], 
    forState: UIControlState.Normal)

多亏了 @audrey-gordeev 提供的Swift实现


4
虽然这很好用,但如果我已经运行了 [mySegmentedControl setTintColor:onColor forTag:kTagOnState];[mySegmentedControl setTintColor:offColor forTag:kTagOffState]; 并且再应用 [mySegmentedControl setTitleTextAttributes:attributes forState:UIControlStateNormal]; ,那么我刚设置的颜色会消失。 - scooter133
3
可在 iOS 5.0 或更高版本中使用。 - rakeshNS
8
在iOS7中:NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:12.0f]}; 的意思是创建一个字典对象,其中包含了一个键值对,键为NSFontAttributeName,值为字体大小为12.0且加粗的字体对象。 - Jason Moore
2
@JasonMoore boldSystemFontOfSize:( System 的 S 大写) - Guillaume
1
在iOS 8中运行得很好。再次提醒我,为什么没有“字体”属性呢...?(苹果有很多解释要做...!) - Mike Gledhill
显示剩余4条评论

52

在iOS 5.0及以上版本中,请使用外观(Appearance)API:

[[UISegmentedControl appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"STHeitiSC-Medium" size:13.0], UITextAttributeFont, nil] forState:UIControlStateNormal];

链接: http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIAppearance_Protocol/Reference/Reference.html#//apple_ref/doc/uid/TP40010906

http://www.raywenderlich.com/4344/user-interface-customization-in-ios-5


5
UITextAttributeFont已被弃用 - 请改用NSFontAttributeName。 - Automate This
7
值得注意的是,这将更改所有UISegmentedControl的字体。 - Nat

38

这是接受答案的 Swift 版本:

Swift 3:

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

Swift 2.2:

let font = UIFont.systemFontOfSize(16)
segmentedControl.setTitleTextAttributes([NSFontAttributeName: font], 
    forState: UIControlState.Normal)

13

另一种选择是对控件应用变换。但是,它会缩小包括控件边框在内的所有内容。

segmentedControl.transform = CGAffineTransformMakeScale(.6f, .6f);

由于这段简单的工作代码,将其在iOS6中缩放为.75f可以获得最佳结果。如果在表格单元格中使用,则将单元格的高度增加10。 - kjoelbro
1
这不是在iOS上更改任何控件字体大小的方法。仿射变换主要用于动画。 - vahotm
1
请不要缩放标准控件。 - Michael Peterson
@MichaelPeterson 很希望有更多可自定义的标准控件,这样就不需要像这样做hack了。 - Zaporozhchenko Oleksandr

9

Swift 风格:

UISegmentedControl.appearance().setTitleTextAttributes(NSDictionary(objects: [UIFont.systemFontOfSize(14.0)], forKeys: [NSFontAttributeName]), forState: UIControlState.Normal)

1
你应该使用Swift风格的字典。[NSFontAttributeName: font] - osrl

8

这里我已经更新了iOS8。

[[UISegmentedControl appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"STHeitiSC-Medium" size:13.0], NSFontAttributeName, nil] forState:UIControlStateNormal];

7

XCode 8.1, Swift 3

import UIKit
class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        UISegmentedControl.appearance().setTitleTextAttributes(NSDictionary(objects: [UIFont.systemFont(ofSize: 24.0)], 
        forKeys: [NSFontAttributeName as NSCopying]) as? [AnyHashable : Any], 
        for: UIControlState.normal)
    }
}

只需更改数字值(ofSize: 24.0)

预览


4
// Set font-size and font-femily the way you want
UIFont *objFont = [UIFont fontWithName:@"DroidSans" size:18.0f];

// Add font object to Dictionary
NSDictionary *dictAttributes = [NSDictionary dictionaryWithObject:objFont forKey:NSFontAttributeName];

// Set dictionary to the titleTextAttributes
[yourSegment setTitleTextAttributes:dictAttributes forState:UIControlStateNormal];

如果您有任何疑问,请联系我。

objFont 返回了 nil 值。 - itzmebibin

4
在Swift 5中,
let font = UIFont.systemFont(ofSize: 16)
UISegmentedControl.appearance().setTitleTextAttributes([NSAttributedString.Key.font: font], for: .normal)

3

2021年的正确答案。语法已更改。

12年前的答案(即使进行了编辑)也已经无法使用。

正确答案仅为:

let _font = UIFont.systemFont(ofSize: 10)
UISegmentedControl.appearance()
 .setTitleTextAttributes([NSAttributedString.Key.font: _font], for: .normal)

在进行操作时,您很可能希望正确地更改高度:

import UIKit

class SmallerSegmentedControl: UISegmentedControl {

    override init(frame: CGRect) {
        super.init(frame: frame)
        common()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        common()
    }

    func common() {
        let _font = UIFont.systemFont(ofSize: 10)
        UISegmentedControl.appearance()
         .setTitleTextAttributes([NSAttributedString.Key.font: _font], for: .normal)
    }

    override var intrinsicContentSize:CGSize {
        var s = super.intrinsicContentSize
        s.height = 24
        return s
    }

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接