Set different font size for iPhone 5 and 6

2019-04-13 06:43发布

I have searched for but not found an answer to this yet.

I would like to set different font sizes of labels for iPhone 5 and 6. I know that I can set specific layout for Compact Width, but both 5 and 6 belong to that group. Is there a way to do this?

3条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-04-13 07:01

My helper extension:

extension UIFont {
    static var sizeMultiplier: CGFloat {
        return UIDevice.current.isPhone5 ? 0.85 : 1
    }

    static func regular(_ size: CGFloat) -> UIFont {
        return UIFont(name: "SFUIText-Regular", size: size * sizeMultiplier)!
    }

    static func bold(_ size: CGFloat) -> UIFont {
        return UIFont(name: "SFUIText-Bold", size: size * sizeMultiplier)!
    }
}

Usage:

titleLabel.font = .bold(16)

I'm also looking for a solution to attach the iPhone5-specific size in the runtime attributes.

查看更多
够拽才男人
3楼-- · 2019-04-13 07:07

You can get the model of the iPhone and compare it to iPhone5, 5s, 5c, 6, and set the font accordingly. You should not use hard-coded sizes to get the device model, but you can get it with Apple's API. Please refer this and this.

查看更多
Deceive 欺骗
4楼-- · 2019-04-13 07:19
if UIScreen.mainScreen().bounds.size.height == 480 {
// iPhone 4
label.font = label.font.fontWithSize(20)     

} else if UIScreen.mainScreen().bounds.size.height == 568 {
// IPhone 5
label.font = label.font.fontWithSize(20)

} else if UIScreen.mainScreen().bounds.size.width == 375 {
// iPhone 6
label.font = label.font.fontWithSize(20)

} else if UIScreen.mainScreen().bounds.size.width == 414 {
// iPhone 6+
label.font = label.font.fontWithSize(20)

} else if UIScreen.mainScreen().bounds.size.width == 768 {
// iPad
label.font = label.font.fontWithSize(20)

}

Or you can get the device using the Apple's API and perform the rest of the logic to set the font size. Please refer this

查看更多
登录 后发表回答