简体   繁体   English

UISegmentedControl中的自定义字体禁用adjustsFontSizeToFitWidth

[英]Custom font in UISegmentedControl disable adjustsFontSizeToFitWidth

I have set a custom font for my UISegmentedControl, but it seems to disable the default autoAdjustFontSizeToFitWidth parameter. 我为我的UISegmentedControl设置了自定义字体,但它似乎禁用了默认的autoAdjustFontSizeToFitWidth参数。

Before: 之前: 字体大小适合宽度 After: 后: 字体大小不再适合宽度

Here is the code I apply to set my custom font: 这是我申请设置自定义字体的代码:

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

Is there a way to preserve it? 有没有办法保存它? Many thanks. 非常感谢。

EDIT: I would like to avoid 编辑:我想避免

 segmentedControl.apportionsSegmentWidthsByContent = YES;  

if possible to always have same width for every segment. 如果可能的话,每个段的宽度始终相同。

I just had to do this. 我只是必须这样做。 You can search through the subviews of the segment control and set the properties. 您可以搜索段控件的子视图并设置属性。

+ (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];
        }
    }
}

Call it with: 称之为:

[Utils setAdjustsFontForWidth:mySegmentControl];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM