229

谁能告诉我如何更改字体类型和大小UISegmentedControl

4

17 回答 17

518

我遇到了同样的问题。此代码设置整个分段控件的字体大小。类似的东西可能适用于设置字体类型。请注意,这仅适用于 iOS5+

对象 C

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

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

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

斯威夫特 5

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

斯威夫特 4

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

斯威夫特 3

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

斯威夫特 2.2

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

感谢@audrey-gordeev 的 Swift 实现

于 2011-09-15T23:54:59.207 回答
52

在 iOS 5.0+ 中使用外观 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

于 2012-12-15T08:07:23.247 回答
38

这是已接受答案的 Swift 版本:

斯威夫特 3

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

斯威夫特 2.2

let font = UIFont.systemFontOfSize(16)
segmentedControl.setTitleTextAttributes([NSFontAttributeName: font], 
    forState: UIControlState.Normal)
于 2015-06-04T06:47:55.833 回答
13

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

segmentedControl.transform = CGAffineTransformMakeScale(.6f, .6f);
于 2011-04-22T14:38:48.567 回答
9

迅捷风格:

UISegmentedControl.appearance().setTitleTextAttributes(NSDictionary(objects: [UIFont.systemFontOfSize(14.0)], forKeys: [NSFontAttributeName]), forState: UIControlState.Normal)
于 2015-03-12T20:53:07.823 回答
8

这里我更新了 iOS8

[[UISegmentedControl appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"STHeitiSC-Medium" size:13.0], NSFontAttributeName, nil] forState:UIControlStateNormal];
于 2015-07-29T21:02:25.657 回答
7

XCode 8.1,斯威夫特 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)

预习

于 2016-11-01T15:37:02.127 回答
5

swift 5,

let font = UIFont.systemFont(ofSize: 16)
UISegmentedControl.appearance().setTitleTextAttributes([NSAttributedString.Key.font: font], for: .normal)
于 2019-07-14T19:51:19.593 回答
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];

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

于 2015-11-07T08:55:53.517 回答
3

C#/Xamarin:

segment.SetTitleTextAttributes(new UITextAttributes { 
    Font = UIFont.SystemFontOfSize(font_size) }, UIControlState.Normal);
于 2017-02-26T06:09:00.567 回答
2

丹尼尔指出我正确的方式。我是这样用的——

float scaleFactor = 0.8f;

UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]
initWithFrame:CGRectMake(10, 70, 300/scaleFactor,35)];

[segmentedControl insertSegmentWithTitle:@"..." atIndex:0 animated:NO];
[segmentedControl insertSegmentWithTitle:@"..." atIndex:1 animated:NO];
[segmentedControl insertSegmentWithTitle:@"..." atIndex:2 animated:NO];

segmentedControl.transform = CGAffineTransformMakeScale(scaleFactor, 1);
CGPoint segmentedControlCenter = segmentedControl.center;
segmentedControlCenter.x = self.center.x;
segmentedControl.center = segmentedControlCenter;
于 2011-06-07T14:14:47.013 回答
2
 UISegmentedControl.appearance().setTitleTextAttributes(NSDictionary(objects: [UIFont.systemFont(ofSize: 16.0)],
                                                                        forKeys: [kCTFontAttributeName as! NSCopying]) as? [AnyHashable : Any],
                                                           for: UIControlState.normal)
于 2018-08-23T06:00:54.870 回答
2

斯威夫特 4

let font = UIFont.systemFont(ofSize: 16)
UISegmentedControl.setTitleTextAttributes([NSFontAttributeName: font], for: .normal)
于 2019-01-12T13:42:16.607 回答
1

UISegmentedControl用于设置字体大小的扩展。

extension UISegmentedControl {
    @available(iOS 8.2, *)
    func setFontSize(fontSize: CGFloat) {
            let normalTextAttributes: [NSObject : AnyObject]!
            if #available(iOS 9.0, *) {
                normalTextAttributes = [
                    NSFontAttributeName: UIFont.monospacedDigitSystemFontOfSize(fontSize, weight: UIFontWeightRegular)
                ]
            } else {
                normalTextAttributes = [
                    NSFontAttributeName: UIFont.systemFontOfSize(fontSize, weight: UIFontWeightRegular)
                ]
            }

        self.setTitleTextAttributes(normalTextAttributes, forState: .Normal)
    }
 }
于 2016-07-12T20:07:08.767 回答
0

您可以通过递归检查以 UISegmentedControl 开头的每个视图来获取 UILabel 的实际字体。我不知道这是否是最好的方法,但它确实有效。

@interface tmpSegmentedControlTextViewController : UIViewController {
}

@property (nonatomic, assign) IBOutlet UISegmentedControl * theControl;

@end

@implementation tmpSegmentedControlTextViewController

@synthesize theControl; // UISegmentedControl

- (void)viewDidLoad {
  [self printControl:[self theControl]];
  [super viewDidLoad];
}

- (void) printControl:(UIView *) view {
  NSArray * views = [view subviews];
  NSInteger idx,idxMax;
  for (idx = 0, idxMax = views.count; idx < idxMax; idx++) {
    UIView * thisView = [views objectAtIndex:idx];
    UILabel * tmpLabel = (UILabel *) thisView;
    if ([tmpLabel respondsToSelector:@selector(text)]) {
      NSLog(@"TEXT for view %d: %@",idx,tmpLabel.text);
      [tmpLabel setTextColor:[UIColor blackColor]];
    }

    if (thisView.subviews.count) {
      NSLog(@"View has subviews");
      [self printControl:thisView];
    }
  }
}

@end

在上面的代码中,我只是设置了 UILabel 的文本颜色,但我想你也可以抓取或设置字体属性。

于 2011-06-12T15:43:14.177 回答
0

这是为了目标 c 添加您的分段控件名称代替mysegmentedcontrol

UIFont *font = [UIFont systemFontOfSize:11.0f];

NSDictionary *attributes = [NSDictionary dictionaryWithObject:font
                                                            forKey:UITextAttributeFont];

[mySegmentedcontrol setTitleTextAttributes:attributes                                    forState:UIControlStateNormal];

希望能帮助到你

于 2015-07-06T05:52:18.157 回答
0

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
    }

}
于 2021-10-11T13:10:13.567 回答