我想根据设备大小定义标签字体大小。这是我到目前为止所做的,但是没有用。
Macros.h
#define ScreenH [UIScreen mainScreen].bounds.size.height
#define ScreenW [UIScreen mainScreen].bounds.size.width
//Weakself
#define WEAKSELF __weak typeof(self) weakSelf = self;
//Screen
#define iphone5 (ScreenH == 568) // Width = 320 [iphone5 | iphone5S | iphoneSE]
#define iphone6 (ScreenH == 667) // Width = 375 [iphone6 | iphone6S | iphone7 | iphone8]
#define iphone6p (ScreenH == 736) // Width = 414 [iphone6P | iphone6SP | iphone7P]
#define iphoneX (ScreenH == 812) // Width = 375 [iphoneX]
#define ipadAir2 (ScreenH == 1024) // Width = 768 [ipad(5G) | ipadAir | ipadAir2 | iPadPro(9.7)]
#define ipadPro105 (ScreenH == 1112) // Width = 834 [iPadPro(10.5)]
#define iPadPro129 (ScreenH == 1366) // Width = 1024 [iPadPro(12.9) | iPadPro(12.9 2G)]
//Font
#define PFR [[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0 ? @"FiraSans-Regular" : @"FiraSans-Regular"
#define PFR20Font [UIFont fontWithName:PFR size:20];
#define PFR18Font [UIFont fontWithName:PFR size:18];
#define PFR16Font [UIFont fontWithName:PFR size:16];
#define PFR15Font [UIFont fontWithName:PFR size:15];
#define PFR14Font [UIFont fontWithName:PFR size:14];
#define PFR13Font [UIFont fontWithName:PFR size:13];
#define PFR12Font [UIFont fontWithName:PFR size:12];
#define PFR11Font [UIFont fontWithName:PFR size:11];
#define PFR10Font [UIFont fontWithName:PFR size:10];
#define TittleFont (ScreenH == 568) ? [UIFont fontWithName:PFR size:11] : (ScreenH == 667) ? [UIFont fontWithName:PFR size:12] : (ScreenH == 736) ? [UIFont fontWithName:PFR size:12] : (ScreenH == 812) ? [UIFont fontWithName:PFR size:12] : (ScreenH == 1024) ? [UIFont fontWithName:PFR size:14] : (ScreenH == 1112) ? [UIFont fontWithName:PFR size:16] : (ScreenH == 1366) ? [UIFont fontWithName:PFR size:16];
ViewController.m
_lblMerchantName = [[UILabel alloc] init];
_lblMerchantName.font = TittleFont; //Here will hit error "Expected expression"
[self addSubview:_lblMerchantName];
请帮助。谢谢。
答案 0 :(得分:1)
我看不到您在问题中定义ScreenH
的位置,我会假设您将其定义为
#define ScreenH [UIScreen mainScreen].bounds.size.height
您的代码中存在一些问题。
我认为应该是TitleFont
而不是TittleFont
:)
正如我所见,您定义了#define iphone5 (ScreenH == 568)
,因此在定义TittleFont
时,请使用iphone5
而不是(ScreenH == 568)
,与另一种情况相同
无需在;
定义的末尾使用TitleFont
您忘记为TitleFont
提供默认字体(在else语句中)。
请在下面尝试我的代码,
#define ScreenH [UIScreen mainScreen].bounds.size.height
//Weakself
#define WEAKSELF __weak typeof(self) weakSelf = self;
//Screen
#define iphone5 (ScreenH == 568) // Width = 320 [iphone5 | iphone5S | iphoneSE]
#define iphone6 (ScreenH == 667) // Width = 375 [iphone6 | iphone6S | iphone7 | iphone8]
#define iphone6p (ScreenH == 736) // Width = 414 [iphone6P | iphone6SP | iphone7P]
#define iphoneX (ScreenH == 812) // Width = 375 [iphoneX]
#define ipadAir2 (ScreenH == 1024) // Width = 768 [ipad(5G) | ipadAir | ipadAir2 | iPadPro(9.7)]
#define ipadPro105 (ScreenH == 1112) // Width = 834 [iPadPro(10.5)]
#define iPadPro129 (ScreenH == 1366) // Width = 1024 [iPadPro(12.9) | iPadPro(12.9 2G)]
//Font
#define PFR [[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0 ? @"FiraSans-Regular" : @"FiraSans-Regular"
#define PFR20Font [UIFont fontWithName:PFR size:20];
#define PFR18Font [UIFont fontWithName:PFR size:18];
#define PFR16Font [UIFont fontWithName:PFR size:16];
#define PFR15Font [UIFont fontWithName:PFR size:15];
#define PFR14Font [UIFont fontWithName:PFR size:14];
#define PFR13Font [UIFont fontWithName:PFR size:13];
#define PFR12Font [UIFont fontWithName:PFR size:12];
#define PFR11Font [UIFont fontWithName:PFR size:11];
#define PFR10Font [UIFont fontWithName:PFR size:10];
#define kDefaultTitleFont [UIFont fontWithName:PFR size:16]
#define TittleFont iphone5 ? [UIFont fontWithName:PFR size:11] : iphone6 ? [UIFont fontWithName:PFR size:12] : iphone6p ? [UIFont fontWithName:PFR size:12] : iphoneX ? [UIFont fontWithName:PFR size:12] : ipadAir2 ? [UIFont fontWithName:PFR size:14] : ipadPro105 ? [UIFont fontWithName:PFR size:16] : iPadPro129 ? [UIFont fontWithName:PFR size:16] : kDefaultTitleFont