做项目常用的宏定义梳理(pch)
一、屏幕宽高的宏
// 屏幕rect
#define SCREEN_BOUNDS ([UIScreen mainScreen].bounds)
// 屏幕宽度
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
// 屏幕高度
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
// 屏幕分辨率
#define SCREEN_RESOLUTION (SCREEN_WIDTH * SCREEN_HEIGHT * ([UIScreen mainScreen].scale))
二、屏幕顶部安全高度的宏及机型判断
// iPhoneX系列判断
#define IS_iPhoneX [UIScreen mainScreen].bounds.size.height >= 812.f
#define mIsiP6 ([UIScreen mainScreen].bounds.size.height == 667.0)
#define mIsiP6P ([UIScreen mainScreen].bounds.size.height == 736.0)
// 顶部安全区域高度
#define StatusBarSafeTopMargin (IS_iPhoneX ? 24.f : 0.f)
// 底部安全区域高度
#define TabbarSafeBottomMargin (IS_iPhoneX ? 34.f : 0.f)
// 顶部状态栏高度
#define StatusBarHeight (IS_iPhoneX ? 44.f : 20.f)
// 导航栏高度
#define NavBarHeight (44.f+StatusBarHeight)
// 底部标签栏高度
#define TabBarHeight (IS_iPhoneX ? (49.f+34.f) : 49.f)
三、字体宏
//字体设置
#define ZA_FontSize(value) [UIFont systemFontOfSize:value]
#define ZAFont_PF_Light(fontSize) [UIFont fontWithName:@"PingFangSC-Light" size:fontSize]
#define ZAFont_PF_Ultralight(fontSize) [UIFont fontWithName:@"PingFangSC-Ultralight" size:fontSize]
// 苹方常规
#define ZAFont_PF_Regular(fontSize) [UIFont fontWithName:@"PingFangSC-Regular" size:fontSize]
// 苹方粗体
#define ZAFont_PF_Semibold(fontSize) [UIFont fontWithName:@"PingFangSC-Semibold" size:fontSize]
#define ZAFont_PF_Thin(fontSize) [UIFont fontWithName:@"PingFangSC-Thin" size:fontSize]
#define ZAFont_PF_Medium(fontSize) [UIFont fontWithName:@"PingFangSC-Medium" size:fontSize]
使用例子:
-(UILabel *)watchNumLabel
{
if (!_watchNumLabel) {
_watchNumLabel = [[UILabel alloc] init];
_watchNumLabel.textColor = RGB_HEX(0x999999, 1);
_watchNumLabel.textAlignment = NSTextAlignmentRight;
_watchNumLabel.font = ZAFont_PF_Regular(12);
}
return _watchNumLabel;
}
四、判断当前iOS版本
//判断当前iOS版本
#define IOS_VERSION [UIDevice currentDevice].systemVersion
五、颜色RGB
#define RGB_GGCOLOR(r,g,b) [UIColor colorWithRed:(r) / 255.0f green:(g) / 255.0f blue:(b) / 255.0f alpha:(1.0)]
#define RGBA_GGCOLOR(r,g,b,p) [UIColor colorWithRed:(r) / 255.0f green:(g) / 255.0f blue:(b) / 255.0f alpha:(p)]
// 形如 RGB_HEX(0xFDFDFD, 1.0f) 的颜色定义
#define RGB_HEX(rgbValue, a) \
[UIColor colorWithRed:((CGFloat)((rgbValue & 0xFF0000) >> 16)) / 255.0 \
green:((CGFloat)((rgbValue & 0xFF00) >> 8)) / 255.0 \
blue:((CGFloat)(rgbValue & 0xFF)) / 255.0 \
alpha:(a)]
使用例子:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(SCREEN_WIDTH - 70, SCREEN_HEIGHT - 134, 60, 60)];
[button setBackgroundColor:RGB_GGCOLOR(86, 202, 252)];
button.layer.masksToBounds = YES;
button.layer.cornerRadius = 30;
[button setTitle:@"导航" forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button addTarget:self action:@selector(openNaviApp:) forControlEvents:UIControlEventTouchUpInside];
[self .view addSubview:button];
六、非空判断
#define IsNilOrNull(_ref) (((_ref) == nil) || ([(_ref) isEqual:[NSNull null]]))
//字符串是否为空
#define kStringIsEmpty(str) ([str isKindOfClass:[NSNull class]] || str == nil || [str length] < 1 ? YES : NO )
//数组是否为空
#define kArrayIsEmpty(array) (array == nil || [array isKindOfClass:[NSNull class]] || array.count == 0)
//字典是否为空
#define kDictIsEmpty(dic) (dic == nil || [dic isKindOfClass:[NSNull class]] || dic.allKeys == 0)
//是否是空对象
#define kObjectIsEmpty(_object) (_object == nil \
|| [_object isKindOfClass:[NSNull class]] \
|| ([_object respondsToSelector:@selector(length)] && [(NSData *)_object length] == 0) \
|| ([_object respondsToSelector:@selector(count)] && [(NSArray *)_object count] == 0))
使用例子:
if (!kStringIsEmpty(_model.insid)) {
HomeServiceTrainDetailVC *detailVC = [HomeServiceTrainDetailVC new];
detailVC.insId = _model.insid;
[self.navigationController pushViewController:detailVC animated:YES];
}