【问题标题】:Change font size of UISegmentedControl更改 UISegmentedControl 的字体大小
【发布时间】:2011-01-17 20:00:02
【问题描述】:

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

【问题讨论】:

  • IB中的任何解决方案而不是代码?
  • 你不能在 IB 中做到这一点。当前的 2021 语法是:UISegmentedControl.appearance().setTitleTextAttributes([NSAttributedString.Key.font: _font], for: .normal)

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


【解决方案1】:

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

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

【讨论】:

  • 这很好用,不过如果我已经完成了 [mySegmentedControl setTintColor:onColor forTag:kTagOnState];[mySegmentedControl setTintColor:offColor forTag:kTagOffState]; 然后应用 [mySegmentedControl setTitleTextAttributes:attributes forState:UIControlStateNormal]; 然后我刚刚设置的颜色就会消失。
  • 在 iOS 5.0 或更高版本中可用
  • 在 iOS7 中:NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldsystemFontOfSize:12.0f]};
  • @JasonMoore boldSystemFontOfSize:(系统大写 S)
  • 在 iOS 8 中发挥作用。再次提醒我,为什么没有“字体”属性...? (Apple 有很多解释要做......!)
【解决方案2】:

在 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

【讨论】:

  • UITextAttributeFont 已被弃用 - 请改用 NSFontAttributeName
  • 值得注意的是它会改变ALL UISegmentedControls的字体。
【解决方案3】:

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

【讨论】:

    【解决方案4】:

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

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

    【讨论】:

    • 感谢这个简单的工作代码。在 iOS6 中将其缩放到 .75f 可提供最佳结果。如果在表格单元格中使用,则将单元格的高度加 10。
    • 这不是您在 iOS 中更改任何控件的字体大小的方式。仿射变换主要用于动画。
    • 请不要缩放标准控件。
    • @MichaelPeterson 会很高兴拥有更多可定制的标准控件,因此没有人需要像这样进行破解。
    【解决方案5】:

    Swift 风格:

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

    【讨论】:

    • 你应该使用 swift 风格的字典。 [NSFontAttributeName: font]
    【解决方案6】:

    这里我更新了iOS8

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

    【讨论】:

      【解决方案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)

      【讨论】:

        【解决方案8】:

        swift 5

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

        【讨论】:

          【解决方案9】:
          // 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 值。
          【解决方案10】:

          C#/Xamarin:

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

          【讨论】:

            【解决方案11】:

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

            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;
            

            【讨论】:

              【解决方案12】:
               UISegmentedControl.appearance().setTitleTextAttributes(NSDictionary(objects: [UIFont.systemFont(ofSize: 16.0)],
                                                                                      forKeys: [kCTFontAttributeName as! NSCopying]) as? [AnyHashable : Any],
                                                                         for: UIControlState.normal)
              

              【讨论】:

              • 虽然此代码可以回答问题,但提供有关其解决问题的方式和原因的信息可提高其长期价值
              【解决方案13】:

              斯威夫特 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
              【解决方案14】:

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

              【讨论】:

                【解决方案15】:

                您可以通过递归检查从 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 的文本颜色,但我想你也可以抓取或设置字体属性。

                【讨论】:

                • 这是确保 iOS 更新不会破坏您交付给客户的代码的好方法。这现在可能有效,但绝对不能保证将来会继续有效。
                【解决方案16】:

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

                UIFont *font = [UIFont systemFontOfSize:11.0f];
                
                NSDictionary *attributes = [NSDictionary dictionaryWithObject:font
                                                                            forKey:UITextAttributeFont];
                
                [mySegmentedcontrol setTitleTextAttributes:attributes                                    forState:UIControlStateNormal];
                

                希望对你有帮助

                【讨论】:

                • -- 更新 -- NSDictionary *attributes = @{NSFontAttributeName:font}; [mySegmentedcontrol setTitleTextAttributes:attributes forState:UIControlStateNormal];
                【解决方案17】:

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

                【讨论】:

                  猜你喜欢
                  • 2011-03-30
                  • 2016-05-22
                  • 2016-11-22
                  • 2015-08-02
                  • 2015-03-12
                  • 1970-01-01
                  • 1970-01-01
                  • 2011-02-04
                  • 2013-10-05
                  相关资源
                  最近更新 更多