UISegmentedControl 中的自定义字体禁用 adjustsFontSizeToFitWidth [英] Custom font in UISegmentedControl disable adjustsFontSizeToFitWidth

查看:23
本文介绍了UISegmentedControl 中的自定义字体禁用 adjustsFontSizeToFitWidth的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have set a custom font for my UISegmentedControl, but it seems to disable the default autoAdjustFontSizeToFitWidth parameter.

之前:后:

这是我用来设置自定义字体的代码:

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.

我想避免

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

调用它:

[Utils setAdjustsFontForWidth:mySegmentControl];

这篇关于UISegmentedControl 中的自定义字体禁用 adjustsFontSizeToFitWidth的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆