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

4

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.

Middlebrooks answered 14/6, 2013 at 14:55 Comment(1)
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.Try
U
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...

Unfrock answered 14/6, 2013 at 14:59 Comment(7)
I selected a UILable, why **Size to Fit Content ** is unavailable?Undershorts
@Geaka just hit CMD =Unfrock
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.Littoral
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.Komarek
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.Flossieflossy
@Unfrock If the option in the menu is unavailable, CMD = won't either work.Fantastically
FWIW, I found the Size to Fit is disabled when the label is inside a Stack ViewBasidium
I
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

Illconditioned answered 18/11, 2014 at 16:14 Comment(1)
Only working solution on iOS 9. If you also have height constraint don't forget to set the height <= than the valueDextrality
E
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.

Equisetum answered 24/5, 2020 at 9:1 Comment(0)
E
-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

Epigraphy answered 14/6, 2013 at 14:59 Comment(1)
Link is broken.Interfuse

© 2022 - 2024 — McMap. All rights reserved.