iOS 常用的宏

2018-06-21  本文已影响0人  可别
//常用的宏定义总结
#ifndef PrefixHeader_pch
#define PrefixHeader_pch

/** self的弱引用 */
#define WeakSelf __weak typeof(self) weakSelf = self;
/** self的强引用/一般是和弱引用配合使用 */
#define StrongSelf __strong __typeof(self) strongSelf = weakSelf;

//宽和高
#define ScreenWIDTH [UIScreen mainScreen].bounds.size.width
#define ScreenHEIGHT [UIScreen mainScreen].bounds.size.height

//在有导航栏的情况下(X应该减去额外的高度88/不占用底部安全区的高度34)
#define NVScreenHEIGHT ([[UIScreen mainScreen] bounds].size.height == 812.0 ? 690 : [[UIScreen mainScreen] bounds].size.height-64)

//在有导航栏的情况下(X应该减去额外的高度88/占用底部安全区的高度34)
#define NVSafeAreaScreenHEIGHT ([[UIScreen mainScreen] bounds].size.height == 812.0 ? 724 : [[UIScreen mainScreen] bounds].size.height-64)

//导航栏和状态栏高度
#define NavBar_Height ([[UIScreen mainScreen] bounds].size.height == 812.0 ? 88 : 64)

//控件宽高屏幕适配
#define AutoSize(x) (x/667.0) * [[UIScreen mainScreen] bounds].size.height

//默认背景颜色
#define BackGroundColor [UIColor colorWithRed:245.0/255.0f green:245.0/255.0f blue:245.0/255.0f alpha:1]
//RGB颜色定义
#define RGBColor(r, g, b)     [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
//RGB颜色定义(透明度)
#define RGBAColor(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)]
//随机颜色
#define RGBColor_arc RGBColor(arc4random_uniform(255), arc4random_uniform(255), arc4random_uniform(255))

/** 是否是4.0屏幕*/
#define iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
/** 是否是4.7屏幕*/
#define iPhone6 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(750, 1334), [[UIScreen mainScreen] currentMode].size) : NO)
/** 是否是5.5屏幕*/
#define iPhone6plus ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? (CGSizeEqualToSize(CGSizeMake(1125, 2001), [[UIScreen mainScreen] currentMode].size) || CGSizeEqualToSize(CGSizeMake(1242, 2208), [[UIScreen mainScreen] currentMode].size)) : NO)
/** 是否是5.8屏幕*/
#define iPhone10 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) : NO)

//调整字体大小、如果是4.0屏幕,-1;如果是4.7屏幕或其他不在判断内的,原倍数;如果是5.5屏幕,+1
#define fontScale(fontSize) iPhone5 ? fontSize - 1.0 : iPhone6 ? fontSize * 1.0 : iPhone6plus ? fontSize + 1.0 : fontSize * 1.0

//scorllview适配ios11
#define AdjustsScrollViewInsetNever(controller,view) if(@available(iOS 11.0, *)) {view.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;} else if([controller isKindOfClass:[UIViewController class]]) {controller.automaticallyAdjustsScrollViewInsets = false;}

//判断数组或者字典对象是否为空
#define JudgeArrayAndDictionaryWhetherNull(object) ((![object isKindOfClass:[NSNull class]] && object.count > 0) ? YES : NO)
//判断字符串是否是null或者为空
#define JudgeStringWhetherNull(object) ((![object isKindOfClass:[NSNull class]] && object.length > 0) ? YES : NO)

//NSLog
#ifdef DEBUG
# define DLog(fmt, ...) NSLog((@"[文件:%s]\n" "[函数:%s]\n" "[行号:%d] log信息:" fmt), __FILE__, __FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
# define DLog(...);
#endif

// iOS不同版本
#define iOS8                  ([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0&&[[UIDevice currentDevice].systemVersion doubleValue] < 9.0)
#define iOS9                  ([[UIDevice currentDevice].systemVersion doubleValue] >= 9.0&&[[UIDevice currentDevice].systemVersion doubleValue] < 10.0)
#define iOS10                 ([[UIDevice currentDevice].systemVersion doubleValue] >= 10.0&&[[UIDevice currentDevice].systemVersion doubleValue] < 11.0)
#define iOS11                 ([[UIDevice currentDevice].systemVersion doubleValue] >= 11.0&&[[UIDevice currentDevice].systemVersion doubleValue] < 12.0)

// Include any system framework and library headers here that should be included in all compilation units.
// You will also need to set the Prefix Header build setting of one or more of your targets to reference this file.

#endif /* PrefixHeader_pch */
上一篇下一篇

猜你喜欢

热点阅读