簡體   English   中英

逐步更改iPad Pro的字體大小

[英]Change font size for iPad Pro in steps

我有一個應用程序,僅適用於橫向模式的iPad。 屏幕設計完全在IB中通過自動布局完成。

現在,我要實現以下行為:在iPad Pro 12“上時,所有標簽的字體大小應為48,對於所有較小的iPad字體,字體大小應為32。

我嘗試了自動縮水和最小字體大小的IB中的各種選項,但隨后該應用選擇的字體大小在48-32之間,並具有隨機外觀。 但是我只想為12“或32的所有較小設備設置48個-兩者之間為了保持一致的外觀而沒有設置。

我的下一個想法是將IB中所有標簽的固定字體大小設置為32,並為所有標簽提供標簽,並在“ viewDidLoad”上的每個viewcontroller中運行以下代碼:

UIDevice的擴展

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

在“ 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)")
                }
            }
        }
    }
}

它僅適用於某些標簽,另一些標簽仍在32中。我該怎么做才能將標簽強制為48? 自動排版有問題嗎? 標簽僅具有寬度約束和上邊距以及X / Y位置。

編輯在調整字體大小后添加調試打印時,得到以下結果:

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

但是字體大小絕對不是48。它看起來與將IB中的字體大小直接設置為48不同。

  1. 子類UILabel,為Interface Builder中的每個uilabel設置該類。

  2. 為要支持的所有不同類型/字體創建一個枚舉,並在該枚舉中創建一個函數,該函數返回例如字體大小

  3. 確定當前設備並將其存儲在枚舉中的值中

  4. 在步驟1中重寫子類UILabel的初始化程序,並從步驟3中定義的variabele中獲取字體大小,並在步驟2中使用vales

碼:

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