Change font size for iPad Pro in steps

狂风中的少年 提交于 2019-12-31 06:24:44

问题


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:


  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
    }
}


来源:https://stackoverflow.com/questions/48811797/change-font-size-for-ipad-pro-in-steps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!