Nerdy Bunz
Nerdy Bunz

Reputation: 7437

How to make text size relative to view in IOS?

In the XCode storyboard, I have set up my ViewController as a bunch of stackviews and everything is relative -- view dimensions are expressed as fractions of other view dimensions... lots of constraints, etc.

This is to make sure it looks decent on all IOS devices (phones and Ipads, anyway).

It does look acceptable in different aspect ratios, but I've noticed that the font size of my UILabels and TextViews are NOT changing -- not getting LARGER along with their containing views.

So, for example, if I switch from an iPhone to an iPad preview, a UILabel size may increase drastically and yet the text that it contains stays the same... so it's tiny text in a big box.

SO... the question is:

Is there a way to express font/text sizes as relative to the view that contains the text?

Something like this:

text.height = 0.7 * container.height
text.width = maintain aspect ratio with height

Thanks.

Upvotes: 0

Views: 627

Answers (2)

Mahesh Agrawal
Mahesh Agrawal

Reputation: 3358

The same problem i had when i was designing an application for both iPhone and iPad and i tried this solution which is working fine but took a little efforts to manage. You need to create a custom label class which will inherits from UILabel class. There in your awakeFromNib function you can check the device and you can multiply whatever the font size with a ratio you feel ok for iPhone and iPad. You can also add checking for different iPhone sizes. If you wish to use different ratios for different label, make a IBDesignable property named dynamicRatio in your custom class and take that value to increase font. You can play around this. The effort is assigning the class to all your labels and setting properties which i use to do parallel during designing.

Below are the set of code which am using.

import UIKit

class MyLabel: UILabel {

    @IBInspectable var autoFont: Bool = false
    @IBInspectable var fontSize: CGFloat = 0

    override open func awakeFromNib() {
        super.awakeFromNib()

        let baseHeight: CGFloat = (((UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.pad)) ?1024.0:568.0)

        if autoFont == true {
            if (isDevice() == DEVICES.iPhoneX) {
                let size: CGFloat = 667.0 * (fontSize / baseHeight)
                self.font = UIFont(name: self.font!.fontName, size: size)
            } else {
                let size: CGFloat = UIScreen.main.bounds.size.height * (fontSize / baseHeight)
                self.font = UIFont(name: self.font!.fontName, size: size)
            }
        }
    }
}

Hope this idea helps.

Upvotes: 2

Arash Etemad
Arash Etemad

Reputation: 1909

Increase text font size of UILabels or UITextFields as you can, then set MinimumFontScale or MinimumFontSize attribute for them in "Attributes Inspector" tab, now font size increases as the UITextFields or UILabels size increases.

UILabel
for UILabel

UITextField
for UITextField

Upvotes: 0

Related Questions