更改UISegmentedControl的字体大小


Answers:


502

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

对象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

迅捷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)

Swift 2.2

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

感谢@ audrey-gordeev的Swift实现


4
这很好用,尽管如果我已经做了a [mySegmentedControl setTintColor:onColor forTag:kTagOnState];和a [mySegmentedControl setTintColor:offColor forTag:kTagOffState];然后应用, [mySegmentedControl setTitleTextAttributes:attributes forState:UIControlStateNormal];那么我刚刚设置的颜色就会消失。
scooter133 '02 -2-28

3
在iOS 5.0或更高版本中可用
rakeshNS 2012年

8
在iOS7中:NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldsystemFontOfSize:12.0f]};
Jason Moore

2
@JasonMoore boldSystemFontOfSize:(系统的资本S)
Guillaume14年

1
在iOS 8中有效。请再次提醒我,为什么没有“字体”属性...?(苹果公司有很多要做的解释……!)
Mike Gledhill,2015年

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


5
UITextAttributeFont已折旧-请NSFontAttributeName改用。
波特兰赛跑者

6
值得注意的是,它将更改ALL UISegmentedControl的字体。
Vive

36

这是已接受答案的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 2014年

1
这不是在iOS中的任何控件上更改字体大小的方法。仿射变换主要用于动画。
vahotm '17

1
请不要缩放标准控件。
迈克尔·彼得森

@MichaelPeterson很高兴拥有更多可自定义的标准控件,因此没有人需要像这样的黑客。
Zaporozhchenko Oleksandr

9

迅捷风格:

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

1
您应该使用快速样式字典。[NSFontAttributeName: font]
osrl 2016年

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

3

C#/ Xamarin:

segment.SetTitleTextAttributes(new UITextAttributes { 
    Font = UIFont.SystemFontOfSize(font_size) }, UIControlState.Normal);

3

斯威夫特4

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

错误:Instance member 'setTitleTextAttributes' cannot be used on type 'UISegmentedControl'; did you mean to use a value of this type instead? iOS13 / Swift5
天空

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;

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

1
 UISegmentedControl.appearance().setTitleTextAttributes(NSDictionary(objects: [UIFont.systemFont(ofSize: 16.0)],
                                                                        forKeys: [kCTFontAttributeName as! NSCopying]) as? [AnyHashable : Any],
                                                           for: UIControlState.normal)

尽管此代码可以回答问题,但提供有关如何以及为什么解决该问题的信息可以提高其长期价值
L_J18年

1

swift 5

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

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的文本颜色,但是我想也可以抓取或设置font属性。


这是确保iOS更新不会破坏您已交付给客户的代码的好方法。这现在可能会起作用,但是绝对不能保证将来会继续起作用。
Apoorv Khatreja

0

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

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

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

[mySegmentedcontrol setTitleTextAttributes:attributes                                    forState:UIControlStateNormal];

希望能帮助到你


1
-更新-NSDictionary *属性= @ {NSFontAttributeName:font}; [mySegmentedcontrol setTitleTextAttributes:属性的状态:UIControlStateNormal];
Benny Davidovitz
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.