ios 如何在故事板(或界面构建器)中使 UILabel 自动调整文本大小,而不是以编程方式

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

how to make UILabel autosize text in storyboard (or interface builder), NOT programmatically

iphoneiosxcodeinterface-builderxcode-storyboard

提问by Doug Null

Every time I click-and-drag a UILabel to storyboard, and then add text to Text field, the text is cut off, so I then have to click-and-drag the UILabel to stretch it big enough so that the text appears.

每次我单击并拖动 UILabel 到情节提要,然后将文本添加到文本字段时,文本都会被切断,因此我必须单击并拖动 UILabel 以将其拉伸到足够大以便文本出现。

I can't figure out how to make the UILabel automatically get big enough to fit the text.

我不知道如何让 UILabel 自动变得足够大以适合文本。

This would save time while designing new views.

这将在设计新视图时节省时间。

回答by Fogmeister

On any element in Interface Builder you can select the element and hit...

在 Interface Builder 中的任何元素上,您都可以选择该元素并点击...

Editor > Size to Fit Content (keyboard shortcut: CMD+=)

编辑器 > 适合内容的大小(键盘快捷键:CMD+ =

This will do a "sizeToFit" on the selected element. Labels will fit their text size, image view will resize to the image size, etc...

这将对所选元素执行“sizeToFit”。标签将适合其文本大小,图像视图将调整为图像大小等...

回答by Shiprack

In Xcode 6.1, I had to set Content Compression Resistance Priority to a higher value, likely because I have other constraints that were conflicting, or causing the sizeToFit option to be disabled.

在 Xcode 6.1 中,我必须将 Content Compression Resistance Priority 设置为更高的值,这可能是因为我有其他冲突的约束,或者导致 sizeToFit 选项被禁用。

Content Compression Resistance PriorityBefore:BeforeAfter:After

内容抗压优先之前:前之后:后

回答by Dan Alboteanu

For the text label:

对于文本标签:

Set Linesto 0

设置为 0

Set Heightto Greater than or equal to 30 (or other value)

高度设置为大于或等于 30(或其他值)

Now the heightwill be adjusted based on the lines number.

现在将根据行数调整高度

回答by Gianluca Tranchedone

Starting with iOS 6, there's a new API, available in Interface Builder as well called 'Auto Layout'. You can specify some contraints and UIKit will resize / move your views based on those contraints. If you don't want to use Autolayout you can use the autoresizeMaskproperty of UIViews (which is settable in Interface Builder as well).

从 iOS 6 开始,有一个新的 API,可在 Interface Builder 中使用,也称为“自动布局”。您可以指定一些限制,UIKit 将根据这些限制调整/移动您的视图。如果您不想使用 Autolayout,您可以使用 UIViews 的autoresizeMask属性(也可以在 Interface Builder 中设置)。

Check out the Autolayout Guide!

查看自动布局指南

To resize the views in Interface Builder as you build them, see the reply from @Fogmeister

要在构建时在 Interface Builder 中调整视图的大小,请参阅@Fogmeister 的回复