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

折月煮酒 提交于 2019-12-03 08:06:08

问题


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.

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

This would save time while designing new views.


回答1:


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

Editor > Size to Fit Content (keyboard shortcut: 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...




回答2:


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.

Before:

After:




回答3:


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 autoresizeMask property of UIViews (which is settable in Interface Builder as well).

Check out the Autolayout Guide!

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



来源:https://stackoverflow.com/questions/17111348/how-to-make-uilabel-autosize-text-in-storyboard-or-interface-builder-not-prog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!