更改UISegmentedControl的字体大小

时间:2010-02-17 12:05:58

标签: ios iphone cocoa-touch font-size uisegmentedcontrol

任何人都可以告诉我如何更改UISegmentedControl的字体类型和大小?

16 个答案:

答案 0 :(得分:464)

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

Obj 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实现

答案 1 :(得分:50)

在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

答案 2 :(得分:35)

以下是接受答案的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)

答案 3 :(得分:13)

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

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

答案 4 :(得分:9)

斯威夫特风格:

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

答案 5 :(得分:8)

这里我更新了iOS8

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

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

Preview

答案 7 :(得分: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];

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

答案 8 :(得分:3)

C#/ Xamarin:

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

答案 9 :(得分: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;

答案 10 :(得分:2)

快捷键4

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

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

答案 12 :(得分:1)

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

答案 13 :(得分: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的文本颜色,但你可以抓住或设置字体属性。

答案 14 :(得分:0)

这是为了客观c添加您的分段控件名称来代替 mysegmentedcontrol

var onloadCallback = function() {
    grecaptcha.render('g-recaptcha', {
      'sitekey' : 'your_site_key',
      'callback' : 'correctCaptcha'
    });
  };

var correctCaptcha = function(response) {
    alert(response);
};

希望有所帮助

答案 15 :(得分:0)

迅速5

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