简体   繁体   English

逐步更改iPad Pro的字体大小

[英]Change font size for iPad Pro in steps

I have an app, which is intended only for iPad in landscape mode. 我有一个应用程序,仅适用于横向模式的iPad。 The screen design is completely done in IB with autolayout. 屏幕设计完全在IB中通过自动布局完成。

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. 现在,我要实现以下行为:在iPad Pro 12“上时,所有标签的字体大小应为48,对于所有较小的iPad字体,字体大小应为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. 我尝试了自动缩水和最小字体大小的IB中的各种选项,但随后该应用选择的字体大小在48-32之间,并具有随机外观。 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. 但是我只想为12“或32的所有较小设备设置48个-两者之间为了保持一致的外观而没有设置。

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: 我的下一个想法是将IB中所有标签的固定字体大小设置为32,并为所有标签提供标签,并在“ viewDidLoad”上的每个viewcontroller中运行以下代码:

Extension for UIDevice 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´ 在“ 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? 它仅适用于某些标签,另一些标签仍在32中。我该怎么做才能将标签强制为48? Is there a problem with autolayout? 自动排版有问题吗? The label has only a width constraint and a top margin, and X/Y position. 标签仅具有宽度约束和上边距以及X / Y位置。

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. 但是字体大小绝对不是48。它看起来与将IB中的字体大小直接设置为48不同。

  1. Subclass UILabel, set that class for every uilabel in your Interface Builder. 子类UILabel,为Interface Builder中的每个uilabel设置该类。

  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 在步骤1中重写子类UILabel的初始化程序,并从步骤3中定义的variabele中获取字体大小,并在步骤2中使用vales

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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