简体   繁体   中英

Change font size for iPad Pro in steps

I have an app, which is intended only for iPad in landscape mode. The screen design is completely done in IB with autolayout.

Now I want to achieve the following behaviour: All the labels should have font size 48 when on iPad Pro 12", and for all smaller iPad sizes the font size should be 32.

I tried various options in IB with autoshrink and minimum font size, but then the app picks font sizes in between 48 - 32 and gives a random look. But I only want to have 48 for 12" or 32 for all smaller devices - nothing in between in order to get a consistent look.

My next idea was to set a fixed font size in IB for all labels to 32, and give all labels a tag and in each viewcontroller on ´viewDidLoad´ run the following code:

Extension for UIDevice

public var isPadPro12: Bool {
    if (UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad
        && UIScreen.main.nativeBounds.size.height == 2732) {
        return true
    }
    return false
}

Code called in ´viewDidLoad´

func adjustFont() {
    let isPro12 = UIDevice.current.isPadPro12
    if isPro12 {
        for subview in view.subviews {
            if subview.tag == 999 {
                if let labelView = subview as? UILabel {
                    labelView.font = labelView.font.withSize(48)
                    print ("font size \(labelView.font)")
                }
            }
        }
    }
}

It works only for some labels, some others are still in 32. What can I do to force the label to 48? Is there a problem with autolayout? The label has only a width constraint and a top margin, and X/Y position.

EDIT When I add the debug print after the font size adjustment, I get the following result:

font size Optional(<UICTFont: 0x7fcbf7c2ea90> font-family: "Arial"; font-weight: normal; font-style: normal; font-size: 48.00pt)

But the font size is definitely not 48. It looks different than setting the font size in IB directly to 48.

  1. Subclass UILabel, set that class for every uilabel in your Interface Builder.

  2. Create a enum for all different types/fonts you want to support, and create a function in that enum that returns eg a font size

  3. Determine the current device and store it in a value from the enum

  4. Override the initializers for your subclassed UILabel in step 1 and get the font size from the variabele defined in step 3, with the vales in step 2

Code:

var currentScreen = Screens.iPadLarge //change this to current device


enum Screens{
    case iPadLarge, iPadSmall
    func getFont() -> CGFloat{
        switch self{
        case .iPadLarge:
            return 40
        case .iPadSmall:
            return 20
        }
    }
}

class MyLabel: UILabel{

    init(frame: CGRect){
        super.init(frame: frame)
        commonLoad()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        commonLoad()
    }

    func commonLoad(){
        let fontSize = currentScreen.getFont()
        //use fontSize
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM