如何在自动布局iOS中动态更改字体大小? [英] How to dynamically change the font size in auto layout iOS?

查看:25
本文介绍了如何在自动布局iOS中动态更改字体大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我的文本放在 UILabel 中,但对于不同的 iPhone,UILabel 的大小正在改变,因为我使用的是自动布局,但我不能修复字体大小,所以我的文字被剪掉了.

有什么方法可以设置任何约束,使文本动态适合 UILabel?

在检查器中,您必须在 Autoshrink 前面选择最小字体比例或最小字体大小.这使内容能够更改字体大小以适合标签.

在这里,我将最小字体比例设置为 0,5,因此最小大小是当前大小的一半 (31.0).文本将尝试适应,直到达到最小比例/大小.

(一般不要使用Tighten letter spacing"来达到这个目的.Tighten letter spacing使用相同的字体大小,减少字母之间的间距.它可以使标签在截断前紧缩5%,但在最小时无效字体比例/大小已启用.)

<小时>

您可能希望使用 iPad Pro 等宽屏设备以及 iPhone 4S 等小屏幕设备进行测试.作为一个好的做法,您应该使用不同的用户系统字体大小进行测试,您可以在 Settings>General>Accessibility>Larger 中设置它们文本.

Autoshrink 不会调整大于标签上设置的字体大小,这意味着如果您将标签设置为与屏幕相同的宽度但将字体大小保留为 14,它会尝试增加字体大小直到达到该大小.

要使其真正起作用,请选择字体大小.

您仍然可以将 autoshrink 与大小类结合使用,以根据设备/方向更改最大字体大小.

<小时>

如果您想将 autoshrinkUIButton 一起使用,您仍然可以使用两行代码来设置此行为.

myButton.titleLabel.minimumScaleFactor = 0.5;myButton.titleLabel.adjustsFontSizeToFitWidth = YES;

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.

Is there any way I can set any constraint so that the text fits in the UILabel dynamically?

See here the text got cut, because of different screen resolution

解决方案

You should use 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.


Previews are for iPhone5, iPhone6 and 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.

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.

(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.)


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.

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.

To make it actually work, select a big font size.

You can still combine autoshrink with size classes to change the maximum font size depending on the device/the orientation.


In case you want to use autoshrink with UIButtons, you can still set this behavior with two lines of code.

myButton.titleLabel.minimumScaleFactor = 0.5;
myButton.titleLabel.adjustsFontSizeToFitWidth = YES;

这篇关于如何在自动布局iOS中动态更改字体大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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