Adaptive trick for label font size

Elsayed Hussein
2 min readSep 28, 2017

--

For me the adaptive UI in iOS Development is difficult to some extent and adaptive of Labels is most difficult of other control (for me)

Probleme 1:

if we add label and set font size with 60 px the label will look fine on iPhone 6 but in iPhone 5 will not look good, as shown :

iPhone5s in left and iPhone6s in right

Solution1:

I find good trick to make size of controls adaptive , thanks for Saurav Chandra for this trick where it will add “dp” unit like Android for CGFloat type :

But note :

You can replace 320 with the value you desire based on what resolution the screen is designed in. Here are the values you can replace 320 with, based on the phone:

iPhone 6, 7, 8 Plus — 414

iPhone 6, 7, 8, X — 375

and just you need to append .dp after the size and it will be adaptive for devices .

Problem 2

But if we used Storyboard for design (most of all iOS Devs do that) our app so we will face another problem , where we will need to make outlet for each label in our app to modify them , and this is really difficult ,so what is the solution ?!

Ok we can change font of all labels in app with using its appearance , check this answer on StackOverflow :

Solution 2

Now we will compine all solutions :

So you will add new property in UILabel extension that will modify its font property by multiply its font size with “dp” property of CGFloat , don’t forget to add line of UILabel appearance in AppDelegate to get the effect .

Hope this trick help you in your coming apps. thanks for reading 🙂 .

--

--