UISegmentedControl中的自定义字体禁用adjustsFontSizeToFitWidth

我为我的UISegmentedControl设置了自定义字体,但似乎禁用了默认的autoAdjustFontSizeToFitWidth参数。

之前: 字体大小适合宽度 后: 字体大小不适合宽度了

以下是我应用于设置自定义字体的代码:

UISegmentedControl *segmentedControl = (UISegmentedControl *)subview; UIFont *font = [UIFont fontWithName:DEFAULT_FONT size:DEFAULT_FONT_SIZE]; NSDictionary *attributes = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName]; [segmentedControl setTitleTextAttributes:attributes forState:UIControlStateNormal]; 

有没有办法保存它? 非常感谢。

编辑:我想避免

  segmentedControl.apportionsSegmentWidthsByContent = YES; 

如果可能的话,每个细分总是有相同的宽度。

我只需要这样做。 您可以search段控件的子视图并设置属性。

 + (void) setAdjustsFontForWidth:(UIView *) view { NSArray * subvies = view.subviews; for(UIView * view in subvies) { if([view isKindOfClass:[UILabel class]]) { UILabel * label = (UILabel *)view; NSLog(@"label found: %@", label.text); label.adjustsFontSizeToFitWidth = TRUE; label.minimumScaleFactor = .1; } else { [self setAdjustsFontForWidth:view]; } } } 

调用它:

 [Utils setAdjustsFontForWidth:mySegmentControl];