ios UILabel 的 minimumScaleFactor 如何工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14650990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:06:42  来源:igfitidea点击:

How does a UILabel's minimumScaleFactor work?

iosobjective-cuilabel

提问by Padin215

I have used minimumFontSizebefore but that function is now deprecated and i don't quite understand how minimumScaleFactorworks.

我以前使用minimumFontSize过,但现在不推荐使用该功能,我不太明白它是如何minimumScaleFactor工作的。

I want the maximum font size to be 10 and the minimum to be 7.

我希望最大字体大小为 10,最小字体大小为 7。

How can I achieve the re-size down to font size 7 with the scale factor?

如何使用比例因子实现重新调整到字体大小 7?

UILabelcreation:

UILabel创建:

UILabel *label = [[UILabel alloc] init];
[label setTranslatesAutoresizingMaskIntoConstraints:NO];
label.text =  [labelName uppercaseString];
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.font = [UIFont fontWithName:HELVETICA_FONT_STYLE_BOLD size:9.5];
label.backgroundColor = [UIColor clearColor];
label.minimumScaleFactor = .1f;

[label addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[label(WIDTH)]"
                                                              options:0
                                                              metrics:@{@"WIDTH" : [NSNumber numberWithFloat:buttonSize.width]}
                                                                views:NSDictionaryOfVariableBindings(label)]];

[contentView addSubview:label];

回答by Scott

You need to set the label.adjustsFontSizeToFitWidth = YES;

你需要设置 label.adjustsFontSizeToFitWidth = YES;

回答by Jsdodgers

In addition to what the other answers say, if you put minSize/defaultSize (division) as the minimumScaleFactor, it will be the same as using the old minimumFontSize.

除了其他答案所说的内容之外,如果您将 minSize/defaultSize (division) 作为 the minimumScaleFactor,它将与使用旧的minimumFontSize.

Ex, if you want the minimum font size to be 10 using default label size, you can do:

例如,如果您希望使用默认标签大小的最小字体大小为 10,您可以执行以下操作:

[label setMinimumScaleFactor:10.0/[UIFont labelFontSize]];

[label setMinimumScaleFactor:10.0/[UIFont labelFontSize]];

(Replace [UIFont labelFontSize]with your label's font size if it is not the default).

[UIFont labelFontSize]如果它不是默认值,请替换为您标签的字体大小)。

which would be the same as: [label setMinimumFontSize:10.0];

这将与: [label setMinimumFontSize:10.0];

回答by kovpas

According to the documentation:

根据文档

Use this property to specify the smallest multiplier for the current font size that yields an acceptable font size to use when displaying the label's text. If you specify a value of 0 for this property, the current font size is used as the smallest font size.

使用此属性来指定当前字体大小的最小乘数,以产生可接受的字体大小,以便在显示标签文本时使用。如果为此属性指定值 0,则当前字体大小将用作最小字体大小。

So if default font size for your label is 10, you put 0.7fas a minimumScaleFactorand it should do the same thing as minimumFontSizedid.

因此,如果您的标签的默认字体大小为,则将其10放置0.7f为 aminimumScaleFactor并且它应该执行与之前相同的操作minimumFontSize

回答by AVog

In addition to other answers, I'm going to add a beginner-friendly explanation that helped myself:

除了其他答案之外,我还将添加一个对初学者有帮助的解释:

How to calculate a minimumScaleFactor? Divide your label's minimum font size by your label's default font size. For example, your default font size is 25. Your minimum font size is 10.

如何计算minimumScaleFactor? 将标签的最小字体大小除以标签的默认字体大小。例如,您的默认字体大小为 25。您的最小字体大小为 10。

10/25 = 0.4

10/25 = 0.4

0.4 is your minimumScaleFactorvalue. Also see @Jsdodgers's answer above.

0.4 是你的minimumScaleFactor价值。另请参阅上面@Jsdodgers 的回答。