我是swift新手,我正在尝试使用Roboto字体。Roboto字体在iPhone 6s中可以使用,但在iPhone XR中不能使用。我的代码是这样的
#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]];

我也在故事板中添加了它,但它在iPhone XR中不起作用。
发布于 2020-01-13 13:49:56
按点而不是按像素定义设备屏幕大小。
这一行永远不会被使用。
#define IS_IPHONE_XR (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 1792.0)iPhone XR的屏幕尺寸为414×896点(828×1792像素)。
有关所有iPhone屏幕大小的综合列表,请查看以下链接:https://developer.apple.com/library/archive/documentation/DeviceInformation/Reference/iOSDeviceCompatibility/Displays/Displays.html
发布于 2020-01-13 16:15:07
要回答为什么它不能工作,那是因为您指定了错误的屏幕高度。将您的#define IS_IPHONE_XR定义替换为:
#define IS_IPHONE_XR (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 896.0) //896 seems to be the height after testing它应该是有效的。
但是,为什么要定义宏并使用硬编码的显示高度值执行检查,而您可以采用更好的方法:使用https://stackoverflow.com/a/26962452/9293498来了解设备型号?我强烈建议这种方法更加通用……
https://stackoverflow.com/questions/59710657
复制相似问题