Change font size of UISegmentedControl

前端 未结 16 1747
死守一世寂寞
死守一世寂寞 2020-11-28 20:54

Can anyone please tell me how can I change the font type and size of UISegmentedControl?

相关标签:
16条回答
  • 2020-11-28 21:16

    C# / Xamarin:

    segment.SetTitleTextAttributes(new UITextAttributes { 
        Font = UIFont.SystemFontOfSize(font_size) }, UIControlState.Normal);
    
    0 讨论(0)
  • 2020-11-28 21:18

    Swift Style:

    UISegmentedControl.appearance().setTitleTextAttributes(NSDictionary(objects: [UIFont.systemFontOfSize(14.0)], forKeys: [NSFontAttributeName]), forState: UIControlState.Normal)
    
    0 讨论(0)
  • 2020-11-28 21:20

    Extension for UISegmentedControl for setting Font Size.

    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)
        }
     }
    
    0 讨论(0)
  • 2020-11-28 21:21

    You can get at the actual font for the UILabel by recursively examining each of the views starting with the UISegmentedControl. I don't know if this is the best way to do it, but it works.

    @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
    

    In the code above I am just setting the text color of the UILabel, but you could grab or set the font property as well I suppose.

    0 讨论(0)
提交回复
热议问题