61

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Doug Null
  • 7,989
  • 15
  • 69
  • 148
  • 2
    You asked: "How do I scale text to my view in IB." Three people explained the opposite - how to size views to fit the text, and including the negative, they received 32 votes and a correct answer acknowledgement. Apple has a source sample for using a CALayer to vectorize text (can't find it just now). I used that as the basis for an IBDesignable view, so I can drop it in, set the text in IB, use constraints to externally define the size, and view the results in IB. I can try to dig it up if you still need it. – Chris Conover Mar 21 '15 at 04:27

4 Answers4

115

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

mr5
  • 3,438
  • 3
  • 40
  • 57
Fogmeister
  • 76,236
  • 42
  • 207
  • 306
  • 7
    I selected a UILable, why **Size to Fit Content ** is unavailable? – Mil0R3 Sep 07 '14 at 15:25
  • 2
    @Geaka just hit CMD = – Fogmeister Sep 07 '14 at 21:53
  • 3
    I was wondering why Size to Fit content was greyed out for me... turned out that my UIButton was already sized perfectly to the content. If I resized the frame manually, I could later use Size to Fit. – Jonny Jun 25 '15 at 09:20
  • Guys, in my XCode (v7.2.1), whenever I drag a Label to the Superview, the option "Size to fit content" under Editor Menu doesn't show enabled. So, to solve this (magically) I just go to the "Size inspector" menu icon and hit on "Explicit" checkbox in "Preferred Width".. and then hit again to enable automatic option. This seens like a XCode bug. I didn't have any constraint assigned to the view. – Vinicius Lima Mar 19 '16 at 14:49
  • 5
    Note that if you are selecting the item from the overview list on the left, you have to double-click it to focus the designer pane before you can hit `Cmd`-`=`. Terrible. But thank goodness for this tip. – devios1 Feb 25 '17 at 16:41
  • @Fogmeister If the option in the menu is unavailable, CMD = won't either work. – Donald Duck Oct 02 '17 at 12:42
  • 1
    FWIW, I found the Size to Fit is disabled when the label is inside a Stack View – Robert Walters Apr 18 '19 at 17:26
38

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.

Content Compression Resistance Priority Before: Before After: After

Shiprack
  • 1,095
  • 11
  • 18
1

For the text label:

Set Lines to 0

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

Now the height will be adjusted based on the lines number.

Dan Alboteanu
  • 9,404
  • 1
  • 52
  • 40
-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

Gianluca Tranchedone
  • 3,598
  • 1
  • 18
  • 33