UISegmentedControl中自定义字体时禁用调整字体大小ToFitWidth功能

3
我已经为我的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;  

如果可能的话,每个片段都保持相同的宽度。

没有autoAdjustFontSizeToFitWidth这样的东西。你是否正确设置了属性? - BonzaiThePenguin
我是指类似于UILabel属性的autoAdjustFontSizeToFitWidth。我想找到一个类似于这样的属性,但是适用于一个segment。 - Tulleb
1个回答

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];

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