How to set different font sizes for different devices using Xcode Storyboard?
Asked Answered
T

3

14

I want to set different font sizes for iPhone 5, iPhone 6 Plus, iPhone 7 Plus and iPhone X using Xcode Storyboard.

Can anyone offer any advice?

Example is shown in the image where i was stuck.

Trondheim answered 15/3, 2018 at 9:56 Comment(4)
you can set global font in app delegate...Krick
see this it helps you : medium.com/@craiggrummitt/…Conyers
there is also one more tick i use for same is Use "AutoShrink" and select last one "Minimum Font Size" use value 0.5 and now assign your max size of font to this label and you done ;)Femur
Check below link:- https://mcmap.net/q/828816/-how-to-ios-item-and-font-sizes-scaling-as-screen-size Hope it helps :)Kepi
B
21

Use Size-Class and add size variation for fonts from Attribute Inspector of Label property.

Here are different possible variations, you can set with Size class:

enter image description here

enter image description here

Try this:

enter image description here

enter image description here

Here is (result) preview of font-size variation, in iPhone and iPad

enter image description here

Update

The result you are expecting, may not be possible using IB (Storyboard) but you can try it with following programmatic solution:

extension UIDevice {
    
    
    enum DeviceType: String {
        case iPhone4_4S = "iPhone 4 or iPhone 4S"
        case iPhones_5_5s_5c_SE = "iPhone 5, iPhone 5s, iPhone 5c or iPhone SE"
        case iPhones_6_6s_7_8 = "iPhone 6, iPhone 6S, iPhone 7 or iPhone 8"
        case iPhones_6Plus_6sPlus_7Plus_8Plus = "iPhone 6 Plus, iPhone 6S Plus, iPhone 7 Plus or iPhone 8 Plus"
        case iPhoneX = "iPhone X"
        case unknown = "iPadOrUnknown"
    }
    
    var deviceType: DeviceType {
        switch UIScreen.main.nativeBounds.height {
        case 960:
            return .iPhone4_4S
        case 1136:
            return .iPhones_5_5s_5c_SE
        case 1334:
            return .iPhones_6_6s_7_8
        case 1920, 2208:
            return .iPhones_6Plus_6sPlus_7Plus_8Plus
        case 2436:
            return .iPhoneX
        default:
            return .unknown
        }
    }
}


// Get device type (with help of above extension) and assign font size accordingly.
let label = UILabel()

let deviceType = UIDevice.current.deviceType

switch deviceType {
    
case .iPhone4_4S:
    label.font = UIFont.systemFont(ofSize: 10)
    
case .iPhones_5_5s_5c_SE:
    label.font = UIFont.systemFont(ofSize: 12)
    
case .iPhones_6_6s_7_8:
    label.font = UIFont.systemFont(ofSize: 14)
    
case .iPhones_6Plus_6sPlus_7Plus_8Plus:
    label.font = UIFont.systemFont(ofSize: 16)
    
case .iPhoneX:
    label.font = UIFont.systemFont(ofSize: 18)
    
default:
    print("iPad or Unkown device")
    label.font = UIFont.systemFont(ofSize: 20)

}
Bourbonism answered 15/3, 2018 at 10:5 Comment(4)
you can't have adifferent variation in font not layout for iPhone 6 ,7 and X ,,, R,R for ipadBisk
@Sh_Khan - You are welcomed to share your inputs and improve quality of this answer. :)Bourbonism
Thanks... @BourbonismTrondheim
Note, that you can add font variations ONLY if you have enabled "Use Trait Variation" for your storyboard. Spent hours finding that...Clayborn
A
3

Note: This is not a straightforward answer. However, this can be achieved based on the constraints.

If you are using autolayout, you can have the font scale automatically through storyboards. In the Attributes inspector for UILabel, there is an Autoshrink section where you can set the Minimum Font Size or Minimum Font Scale. So, as your label resizes itself through constraints, it will also scale down your fonts to fit.enter image description here

Anew answered 15/3, 2018 at 11:16 Comment(1)
how do you set the constraint? because it doesnt work for meAltdorf
B
2

You can't accomplish this in IB , you can detect the current device at runtime with it's size and set the font accordingly , if all elements have same font you can use .appearnce to set a global font

There is no font variation for IPhone 6,7,X , it has to be in code not IB

Bisk answered 15/3, 2018 at 10:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.