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

スウィフト 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 以降で 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

于 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

もう 1 つのオプションは、コントロールに変換を適用することです。ただし、コントロールの境界線を含むすべてを縮小します。

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

Here i have updated for iOS8

[[UISegmentedControl appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"STHeitiSC-Medium" size:13.0], NSFontAttributeName, nil] forState:UIControlStateNormal];
于 2015-07-29T21:02:25.657 に答える
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

スイフト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 のテキストの色を設定しているだけですが、font プロパティを取得または設定することもできると思います。

于 2011-06-12T15:43:14.177 に答える