UISegmentedControl中的自定义字体禁用adjustsFontSizeToFitWidth

时间:2015-08-22 11:22:12

标签: ios objective-c uisegmentedcontrol

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

在: Font size is fit to width 后: Font size is not fit to width anymore

以下是我申请设置自定义字体的代码:

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;  

如果可能的话,每个段的宽度始终相同。

1 个答案:

答案 0 :(得分:2)

我必须这样做。您可以搜索段控件的子视图并设置属性。

+ (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];
相关问题