ios 调整文本大小以适应 Swift 中的标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29914628/
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-31 05:49:06  来源:igfitidea点击:

Resize text to fit a label in Swift

iosswifttextxcode6autolayout

提问by John Allijn

I want to display some text in a label.

我想在标签中显示一些文本。

The label has a fixed size but the text can vary from one single word to a complete sentence.

标签有固定的大小,但文本可以从一个单词到一个完整的句子不等。

Is there a way to automatically resize the text in such a way that it is alway displayed in the largest possible font-size without truncating it?

有没有办法自动调整文本大小,使其始终以尽可能大的字体大小显示而不会截断它?

I'm working through tutorials for days now, but can only find ways to adjust the label size, not the text size.

我现在已经学习了几天的教程,但只能找到调整标签大小的方法,而不是文本大小。

回答by Satachito

Set minimumScaleFactorproperty of UILabel.

设置 的minimumScaleFactor属性UILabel

How to set minimumScaleFactor

如何设置 minimumScaleFactor

回答by Erik

You can change the font size to fit the width of the UILabel (non-multiline):

您可以更改字体大小以适应 UILabel 的宽度(非多行):

myLabel.adjustsFontSizeToFitWidth = true