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?
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.
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
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.