Change font size for iPad Pro in steps

后端 未结 1 1517
甜味超标
甜味超标 2021-01-26 03:35

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 behavio

相关标签:
1条回答
  • 2021-01-26 04:23
    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 e.g. 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
        }
    }
    
    0 讨论(0)
提交回复
热议问题