1

In one of the views in my app, I want to use different font sizes for different device sizes. Since iPhone 5 and iPhone 6+ are in the same size class (when in portrait mode), I cannot solve this problem by size class. (If I override the size class methods to put iPhone 6+ into another class, iPad will not display correctly).

I know I can specify font sizes in code, but is there any way to do it in storyboard? So that I can preview the view in assistant editor.

ukim
  • 2,395
  • 15
  • 22

2 Answers2

0

I do not believe there is a different way to do this in storyboard.

Michael Voznesensky
  • 1,612
  • 12
  • 15
0

To solve your case . you need to identify the user device modal.

enum DeviceModals : String {
    //iphones
    case iPhone5s_GSM = "iPhone6,1"
    case iPhone5s_China_Gobal = "iPhone6,2"
    case iPhone6_Plus = "iPhone7,1"
    case iPhone6 = "iPhone7,2"
    case iPhone6S = "iPhone8,1"
    case iPhone6S_Plus = "iPhone8,2"
    case iPhoneSE = "iPhone8,4"
    case iPhone7_CDMA = "iPhone9,1"
    case iPhone7_GSM = "iPhone9,3"
    case iPhone7_Plus_CDMA = "iPhone9,2"
    case iPhone7_Plus_GSM = "iPhone9,4"
    case iPhone_8_CDMA =   "iPhone10,1"
    case iPhone_8_GSM =  "iPhone10,4"
    case iPhone_8_Plus_CDMA =  "iPhone10,2"
    case iPhone_8_Plus_GSM =  "iPhone10,5"
    case iPhone_X_CDMA = "iPhone10,3"
    case iPhone_X_GSM =  "iPhone10,6"
    case iPhone_XS = "iPhone11,2"
    case iPhone_XS_Max =   "iPhone11,4"
    case iPhone_XS_Max_China = "iPhone11,6"
    case iPhone_XR =   "iPhone11,8"
    case iPhone_11 =   "iPhone12,1"
    case iPhone_11_Pro =  "iPhone12,3"
    case iPhone_11_Pro_Max =  "iPhone12,5"

    //iPad
   case iPad_Air_wifi_5Gen  =  "iPad4,1"
   case iPad_Air_cellular_5Gen =  "iPad4,2"
   case iPad_Mini_wifi_2Gen = "iPad4,4"
   case iPad_Mini_cellular_2Gen = "iPad4,5"
   case iPad_Mini_wifi_3rd_Gen =  "iPad4,7"
   case iPad_Pro_12_9_A1584 =  "iPad6,7"
   case iPad_Pro_12_9_A1652 =  "iPad6,8"
   case iPad_Pro_9_7_A1673 =  "iPad6,3"
   case iPad_Pro_9_7_A1674 =  "iPad6,4"

    case otherDevice = "other"

}
extension UIDevice {
    var modelName: String {
        var systemInfo = utsname()
        uname(&systemInfo)
        let machineMirror = Mirror(reflecting: systemInfo.machine)
        let identifier = machineMirror.children.reduce("") { identifier, element in
            guard let value = element.value as? Int8, value != 0 else { return identifier }
            return identifier + String(UnicodeScalar(UInt8(value)))
        }
        return identifier
    }
    func deviceModal() -> DeviceModals{
        let deviceName  = UIDevice.current.modelName
        let deviceModal = DeviceModals(rawValue: deviceName) ?? DeviceModals.otherDevice
        return deviceModal
    }
}

If you support the Accessibility feature in your app. Users will have a controller for text Size in your app.

Settings -> Display&brightness -> Text Size .

This default size will change for different devices based on device size.

    extension UIFont
    {
    // A scale value based on the current device text size setting.    With the device using the default Large setting, `scaler` will be `1.0`. Only used when `UIFontMetrics` is not available.
        var scaler: CGFloat {
            return UIFont.preferredFont(forTextStyle:.body).pointSize/17.0
         }
       func scaledFont() -> UIFont {
            if #available(iOS 11.0, *) {
                return UIFontMetrics.default.scaledFont(for: self)
            } else {
                return self.withSize(scaler * self.pointSize)
            }
        }
    }

You can button font now.

UILabel().font = UIFont.systemFont(ofSize: 12).scaledFont() 

Now you knew which device users have, So you can create one custom Label (or) Button (or) AnyComponent with text data.

class CustomLabel : UILabel{

    override func awakeFromNib() {
        super.awakeFromNib()
        let deviceModal = UIDevice.current.deviceModal()
        var font_size : CGFloat = 18
        switch deviceModal {
        case .iPhone5s_GSM , .iPhone5s_China_Gobal  :
            font_size = 12
        case .iPhone_8_Plus_CDMA , .iPhone_8_Plus_GSM:
           font_size = 18
        case .iPhone_XR:
            font_size = 19
        default:
            font_size = 18
        }
        self.font = UIFont.systemFont(ofSize: font_size).scaledFont()
    }
}

In your storyboard select your label, go to the right corner and select custom class and put "CustomLabel".Now you don't have to write any other code it will work perfectly.

enter image description here

I hope your issue is solved now.

Manikandan
  • 1,195
  • 8
  • 26