Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Dynamic UILabel and other objects (programatically)


I'm working on an app, without the use of storyboards.

However, one thing that is completely untouched in most tutorials is designing in code.

For an easy example, I need a UILabel with the following "properties"

The layout should be as follows:

16 points from the left (standard margin) 16 points from the right (standard margin) 8 points from the bottom of the navigation bar.

But more important it needs to automatically resize the font to fit the layout. (So it will have the same aspect ratio on an Iphone 4 and an Iphone 6plus)

after all that. I need to be able to continue this workflow, so I can place my next object 8 points below my UILabel.

Damien Watson
Damien Watson
27,419 Points

Hey Mark, I'm going to assume you're talking about Swift? I haven't moved over to this yet but have done it in ObjC. I extended the UILabel class and created my own initialise method, this set up all of my defaults for me and I just passed in a frame, text, and maybe font size. Maybe something like that will work for you.

There used to be a method where text size was scaled to fill the width but they deprecated it, not sure of how to do it now apart from calculating font size with percentages.

Hi Damien, thanks for the reply.

Sorry it wasn't specified. But yes, it's being written in swift.

Since I posted it, I managed to find a tiny bit of documentation on how programmatic constraints work in swift, which I can use to position my objects with.

However, like you mentioned, the "size to fit width" thing they had is deprecated. And I still need to figure out, how to scale my font-size to fit the width of the layout.

Now you mention calculating the font-size with percentages. Could you elaborate on this?

Mark, I'm sorry I'm not able to help you on that subject.

But, out of curiosity, why are you designing in code? What made you decide to dump the storyboard altogether?