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.

1
  • 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. Mar 21, 2015 at 4:27

4 Answers 4

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

7
  • 7
    I selected a UILable, why **Size to Fit Content ** is unavailable?
    – Mil0R3
    Sep 7, 2014 at 15:25
  • 2
    @Geaka just hit CMD =
    – Fogmeister
    Sep 7, 2014 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, 2015 at 9:20
  • 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, 2017 at 16:41
  • 1
    FWIW, I found the Size to Fit is disabled when the label is inside a Stack View Apr 18, 2019 at 17:26
39

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

2
  • 1
    Only working solution on iOS 9. If you also have height constraint don't forget to set the height <= than the value Dec 28, 2015 at 11:20
  • You save my day man. Thanks
    – Sajid Zeb
    Sep 6 at 6:16
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.

-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

1
  • 1
    Link is broken.
    – Amr Lotfy
    Mar 1, 2017 at 5:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.