Scale text label by screen size

4 Solutions Collect From Internet About “Scale text label by screen size”

To set constraints on the label that you have, see this link: How do you make a background image scale to screen size in swift? . I know that you might not be using Swift (I’m using Objective-C), but the first answer shows how to do it in the storyboard. Do the same thing it says, but for the label. Then, see the image below to change the auto shrink options for the label from “Fixed Font Size” to “Minimum Font Scale” (see image below). Hope this helps!

Changing Font Size

I was having the same issue where I needed the text to be scaled proportionally along with the screen size increase.

Adaptive sizing is quite limited as you can only set the font sizing for size classes. Having the font sizes for two width options, compact and regular was not a solution for me.

I have written a small lib which handles automatic font scaling for UILabel and UITextView for different screen sizes.

You can set the scaling globally or for a specific instance of UILabel and UITextView.

Find it here: AMXFontAutoScale

I had my own fix and it’s not one click, but I found it well worth it.

Step 1:

Test some font sizes for different screen sizes, recording the font size and the most relevant other dimension.

Here’s what I mean…

I’d start with a normal iPad Screen and select a font size that worked.

enter image description here

Since I knew the entire height of the screen would determine what makes a font size comfortable or not, I’d record the font size along with the entire screen height.

enter image description here

So the font size was 50 with a height of 1024.

Then, I switched to the iPhone SE

enter image description here
enter image description here

This was pretty atrocious, so I fixed the font size:

enter image description here

After recording that the font size worked at 25 with a height of 568.

Step 2:

Bust out the calculator app and find a constant that relates the font sizes to the heights (in my case), giving or taking a bit.

Here’s what I mean:

I knew 50 works with 1024 and that 25 works with 568.

Dividing 50/1024 gives you .048828125.

Multiplying that by 568 equals 27.734375, which would be a little fat of a font size for the SE.

Repeating that process but using the SE’s values to do the division produced .0440140845, and checking that with the iPad’s height produces a font size of 45, just a bit small for the mighty iPad screen.

So I split the quotients down the middle, resulting in a number around 0.46.

Step 3:

Connect the labels and change their font sizes programmatically using this number.

First, drag the labels into an outlet collection. I named my connection ‘headerLabels’ since they’re all at the top of the screen.

enter image description here

Then, you can kinda steal my code, but just don’t make too much money with it 😉

// Outlet collection of labels
@IBOutlet var headerLabels: [UILabel]!

// Who needs a short variable name when we can have a 
// painfully lengthy one to describe the number we calculated?
let relativeFontConstant:CGFloat = 0.046

override func viewDidLoad() {
    super.viewDidLoad()

    // Should be in the viewDidLoad so it can load the same time as
    // the view does.

    // apply a font size to all the labels.
    for label in headerLabels {
        // multiply the height we based it on of the entire view 
        // to the number we calculated
        label.font = label.font.withSize(self.view.frame.height * relativeFontConstant)
    }
}

If you’ve got any questions, comments, compliments, or insults let me know 🙂

There is an aspect ratio constraint available. Add this to your label. Constraints to left and top margins for anchoring the label in place should silence the compiler warnings.

As @VatsalManot mentioned, learn adaptive sizing for starters. Here’s a good link:

http://www.raywenderlich.com/83276/beginning-adaptive-layout-tutorial

Hope this helps! 🙂