改变UISegmentedControl的字体大小

235

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


1
有没有在IB中的解决方案,而不是代码? - oky_sabeni
你不能在IB中完成它。当前2021的语法是:UISegmentedControl.appearance().setTitleTextAttributes([NSAttributedString.Key.font: _font], for: .normal) - Fattie
17个回答

3

C# / Xamarin:

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

2

Daniel指出了正确的方法。我使用它就像这样-

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;

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

虽然这段代码可能回答了问题,但提供解决问题的方式和原因可以提高其长期价值。 - L_J

2

Swift 4

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

错误:实例成员 'setTitleTextAttributes' 无法在类型 'UISegmentedControl' 上使用;您是否意味着要改用此类型的值? iOS13/Swift5 - Sky

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

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的文本颜色,但你也可以获取或设置字体属性。

这是一种确保iOS更新不会破坏您已经交付给客户的代码的好方法。现在可能有效,但绝对不能保证将来仍然有效。 - Apoorv Khatreja

0

这是针对Objective-C的,将您的分段控件名称替换为mysegmentedcontrol

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

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

[mySegmentedcontrol setTitleTextAttributes:attributes                                    forState:UIControlStateNormal];

希望能对您有所帮助


1
更新:NSDictionary * attributes = @{NSFontAttributeName:font}; [mySegmentedcontrol setTitleTextAttributes:attributes forState:UIControlStateNormal]; - Benny Davidovitz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接