如何在自动布局 iOS 中动态更改字体大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35153363/
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
How to dynamically change the font size in auto layout iOS?
提问by Muntaqeem Labbaeik
I want to fit my text in a UILabel
, but for different iPhone the size of the UILabel
is changing, as I'm using auto layout, but I cannot fix the font size, so my text is cutting out.
我想将我的文本放入UILabel
,但对于不同的 iPhone,它的大小UILabel
正在变化,因为我使用的是自动布局,但我无法修复字体大小,所以我的文本被剪掉了。
Is there any way I can set any constraint so that the text fits in the UILabel
dynamically?
有什么办法可以设置任何约束,使文本适合UILabel
动态?
See here the text got cut, because of different screen resolution
回答by Crazyrems
You should use autoshrink.
您应该使用autoshrink。
Since all iPhones have the same Compact width
size class when in portrait mode, you can't rely on this to handle your label size.
由于所有 iPhoneCompact width
在纵向模式下都具有相同的尺寸等级,因此您不能依靠它来处理您的标签尺寸。
Previews are for iPhone5, iPhone6 and iPhone 6+
预览适用于 iPhone5、iPhone6 和 iPhone 6+
In the inspector, you must select minimum font scale or minimum font size in front of Autoshrink. This enables the content to change the size of the font to fit in the label.
在检查器中,您必须在 Autoshrink 前面选择最小字体比例或最小字体大小。这使内容能够更改字体大小以适应标签。
Here, I set minimum font scale to 0,5 so the minimum size is half of the current size (31.0). The text will try to fit until it reaches the minimum scale/size.
在这里,我将最小字体比例设置为 0,5,因此最小尺寸是当前尺寸(31.0) 的一半。文本将尝试适应,直到达到最小比例/大小。
(Generally do not use "Tighten letter spacing" for this purpose. Tighten letter spacing uses the same font size and reduces spacing between letters. It can make the label up to 5% tighter before truncating, but it's not effective when minimum font scale/size is enabled.)
(一般不要为此使用“拉紧字母间距”。拉紧字母间距使用相同的字体大小并减少字母之间的间距。它可以使标签在截断前最多紧缩5%,但在最小字体比例时无效/大小已启用。)
You may want to test with a wide screen device such as the iPad Pro, and also on a smaller screen such as the iPhone 4S. As a good practice you should test with different user system font sizes, you can set them in Settings
>General
>Accessibility
>Larger Text
.
您可能想使用iPad Pro等宽屏设备以及iPhone 4S等小屏幕设备进行测试。作为一个好的做法,您应该使用不同的用户系统字体大小进行测试,您可以在Settings
> General
> Accessibility
> 中设置它们Larger Text
。
Autoshrink will not adjust the font size bigger than the one set on the label, that means if you make the label the same width as the screen but leave the font size to 14, it will try to increase the font size until it reaches that size.
Autoshrink 不会调整大于标签上设置的字体大小,这意味着如果您将标签设置为与屏幕相同的宽度但将字体大小保留为14,它将尝试增加字体大小直到达到该大小.
To make it actually work, select a bigfont size.
要使其实际工作,请选择大字体。
You can still combine autoshrink with size classes to change the maximum font size depending on the device/the orientation.
您仍然可以将 autoshrink 与 size classes 结合使用,以根据设备/方向更改最大字体大小。
In case you want to use autoshrinkwith UIButton
s, you can still set this behavior with two lines of code.
如果您想将autoshrink与UIButton
s一起使用,您仍然可以使用两行代码设置此行为。
myButton.titleLabel.minimumScaleFactor = 0.5;
myButton.titleLabel.adjustsFontSizeToFitWidth = YES;
回答by Mobile Apps Expert
回答by Madan gupta
You can do this by using size classes.
each display dimension using a size class, this will result in four abstract devices: Regular width-Regular Height, Regular width-Compact Height, Compact width-Regular Height and Compact width-Compact Height.
The table below shows the iOS devices and their corresponding size classes.
?
您可以通过使用大小类来做到这一点。每个显示尺寸使用一个尺寸类,这将产生四个抽象设备:常规宽度 - 常规高度,常规宽度 - 紧凑高度,紧凑宽度 - 常规高度和紧凑宽度 - 紧凑高度。下表显示了 iOS 设备及其相应的尺寸等级。?
To set the font size for this particular size class, first select the UILabel
. Under the Attributes inspector, you should see a plus (+) button next to the font field. Click the + button and select Compact Width > Regular Height (Or as per your requirement select width & Height). ? You will then see a new entry for the Font option, which is dedicated to that particular size class. Keep the size intact for the original Font option but change the size of wC hR (OR as per your selection) font field to required points (for example 14).
要为此特定大小类别设置字体大小,请首先选择UILabel
. 在属性检查器下,您应该会在字体字段旁边看到一个加号 (+) 按钮。单击 + 按钮并选择紧凑宽度 > 常规高度(或根据您的要求选择宽度和高度)。? 然后,您将看到字体选项的新条目,该条目专用于该特定尺寸类别。保持原始字体选项的大小不变,但将 wC hR(或根据您的选择)字体字段的大小更改为所需的点(例如 14)。