0

I am new in swift I am trying to used Roboto font. Roboto font is working in iPhone 6s but it is not working in iPhone XR. My Code is like this

    #ifndef DICE_PrefixHeader_pch
    #define DICE_PrefixHeader_pch  

    #define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    #define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    #define IS_IPHONE_4 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 480.0)
    #define IS_IPHONE_5 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 568.0)
    #define IS_IPHONE_6 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 667.0)
    #define IS_IPHONE_6PLUS (IS_IPHONE && [[UIScreen mainScreen] nativeScale] == 3.0f)
    #define IS_IPHONE_6_PLUS (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 736.0)
    #define IS_IPHONE_X (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 812.0)
    #define IS_IPHONE_XR (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 1792.0)

    GenralMethod.h

    +(UIFont*)setFont:(NSString*)fontName ForiPhone6Plus:(CGFloat)iPhone6Plus iPhone6:(CGFloat)iPhone6 iPhoneXR:(CGFloat)iPhoneXR andiPhone:(CGFloat)iPhone;

    GenralMethod.m

    +(UIFont*)setFont:(NSString*)fontName ForiPhone6Plus:(CGFloat)iPhone6Plus iPhone6:(CGFloat)iPhone6 iPhoneXR:(CGFloat)iPhoneXR andiPhone:(CGFloat)iPhone
    {
        return [UIFont fontWithName:fontName size:IS_IPHONE_6_PLUS?iPhone6Plus:IS_IPHONE_XR?iPhoneXR:IS_IPHONE_5?iPhone:iPhone6];
    }


    [btnMessage.titleLabel setFont:[GeneralMethod setFont:@"Roboto-Light" ForiPhone6Plus:20.0 iPhone6:20.0 iPhoneXR:20.0 andiPhone:15.0]];

enter image description here

I added it in storyboard also but its is not working in iPhone XR.

Mujtaba
  • 97
  • 1
  • 7

2 Answers2

0

Define device screen sizes by point rather than pixels.

This line will never get used.

#define IS_IPHONE_XR (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 1792.0)

The iPhone XR has a screen size of 414×896 points (828×1792 pixels).

For a comprehensive list of all iPhone Screen sizes, look at this link: https://developer.apple.com/library/archive/documentation/DeviceInformation/Reference/iOSDeviceCompatibility/Displays/Displays.html

Eloy B.
  • 565
  • 4
  • 13
  • `+(UIFont*)setFont:(NSString*)fontName ForiPhone6Plus:(CGFloat)iPhone6Plus iPhone6:(CGFloat)iPhone6 iPhoneXR:(CGFloat)iPhoneXR andiPhone:(CGFloat)iPhone { return [UIFont fontWithName:fontName size:IS_IPHONE_6_PLUS?iPhone6Plus:IS_IPHONE_XR?iPhoneXR:IS_IPHONE_5?iPhone:iPhone6]; } ` Maybe have a fallback or default value there, in case your iPhoneXR Dimensions are wrong. 1792.0 screen height seems a bit out of the ordinary. I assume thats why your XR does not load the font correctly. – Eloy B. Jan 13 '20 at 06:03
  • its working fine in iPhone 6s but not in iPhone XR.I have done all the process. Just not understanding why this is happing – Mujtaba Jan 13 '20 at 06:05
  • The iPhone XR has a screen size of 414×896 points (828×1792 pixels). You need to go by the points not by the pixels – Eloy B. Jan 13 '20 at 06:07
  • @Mujtaba Edited my answer, please have a look and adjust your code accordingly. Should work then ;) – Eloy B. Jan 13 '20 at 06:11
  • Change code but nothing change still the problem is same – Mujtaba Jan 16 '20 at 10:22
0

To answer why it doesn't work, it's because you've specified the wrong screen height. Replace your #define IS_IPHONE_XR definition with:

#define IS_IPHONE_XR (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 896.0) //896 seems to be the height after testing

and it should work.

But, why define macros and perform checks with hardcoded display height values when you could do better approaches like: https://stackoverflow.com/a/26962452/9293498 to know the device model? I strongly recommend this approach to be more generic...

Lokesh SN
  • 1,583
  • 7
  • 23