How To Add Dynamic Font Size about Multi Device in Xcode Story Board

前端 未结 4 2013
借酒劲吻你
借酒劲吻你 2020-12-30 16:04

I added autoLayout in storyBoard (w: Any, h: Any)

But because of fixed font size, The font size is same in all of devices (4, 4.7, 5.5 inch)

It looks nice i

4条回答
  •  被撕碎了的回忆
    2020-12-30 16:48

    Check the screen size and manipulate the font size based on it:

    let screenSize = UIScreen.mainScreen().bounds.size
    
    if screenSize.height < 568 {
        //Set font size for 4
    } else if screenSize.height < 568 {
        //Set font size for 5
    } else if screenSize.height < 568 {
        //Set font size for 6
    } else {
        //Set font size for 6+
    }
    

    UPDATE:

    Extend the UILabel class to setup your label:

    extension UILabel { 
        func setupLabelDynamicSize(size size:CGFloat) {
            let screenSize = UIScreen.mainScreen().bounds.size
    
            if screenSize.height < 568 {
                //Set self.font equal size
            } else if screenSize.height < 568 {
                //Set self.font equal size + 1
            } else if screenSize.height < 568 {
                //Set self.font equal size + 2
            } else {
                //Set self.font equal size + 3
            }
        }
    }
    

    then wherever you have a label that you want with dynamic size just call:

    labelObject.setupLabelForDynamicSize(size: 14)
    

提交回复
热议问题