How do I make text labels scale the font size with different Apple product screens?

后端 未结 2 1067
生来不讨喜
生来不讨喜 2021-01-05 07:28

This is for ios dev with swift. I have a very simplistic layout:

link1 (not enough rep to actually post the pic)

All I\'m trying to do is make sure that \"re

2条回答
  •  攒了一身酷
    2021-01-05 08:19

    Hien,

    Here is a super post about detecting the device/screen size.

    How to detect iPhone 5 (widescreen devices)?

    This is my favourite suggestion on it, please click on it and thank the real author!

    Add a 'New Swift File'-> AppDelegateEx.swift

    add an extension to AppDelegate

    import UIKit
    extension AppDelegate {
        class func isIPhone5 () -> Bool{
             return max(UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height) == 568.0
        }
        class func isIPhone6 () -> Bool {
            return max(UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height) == 667.0
        }
        class func isIPhone6Plus () -> Bool {
            return max(UIScreen.mainScreen().bounds.width,     UIScreen.mainScreen().bounds.height) == 736.0
        }  
    }
    

    usage:

    if AppDelegate.isIPhone5() {
        collectionViewTopConstraint.constant = 2
    }else if AppDelegate.isIPhone6() {
        collectionViewTopConstraint.constant = 20
    }
    

    Combine it with setting the font in your application, which you surely must be doing right now?

    variable.font = UIFont(name: "Palatino", size: 24)
    

提交回复
热议问题