【问题标题】:Custom font in UISegmentedControl disable adjustsFontSizeToFitWidthUISegmentedControl 中的自定义字体禁用 adjustsFontSizeToFitWidth
【发布时间】:2015-11-16 07:06:51
【问题描述】:

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

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

【问题讨论】:

  • 没有 autoAdjustFontSizeToFitWidth 这样的东西。您是否正确设置了属性?
  • 我的意思是 autoAdjustFontSizeToFitWidth 像 UILabel 属性。我想找一个这样的属性,但要针对一个细分市场。

标签: ios objective-c uisegmentedcontrol


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2010-11-19
    • 1970-01-01
    • 2013-12-06
    • 2011-11-27
    • 1970-01-01
    • 2010-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多