OC常用的宏定义

#ifndef LPHDefines_h
#define LPHDefines_h

/* Log */
#pragma mark - Log
#ifdef DEBUG
# define LPHLog(fmt, ...) NSLog((@"[文件名:%s]\n" "[函数名:%s]\n" "[行号:%d] \n" fmt), __FILE__, __FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
# define LPHLog(...);
#endif

/* Device Infos */
#pragma mark - Device Infos

/* App 版本 */
#define AppVersion [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]

/* 当前系统版本 */
#define SystemVersion [[[UIDevice currentDevice] systemVersion] floatValue]

/* 是否iPad */
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
/* 是否iPhone */
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
/* 是否iPhone X */
#define IS_IPHONE_X ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) : NO)

/* 屏幕尺寸 */
#define SCREEN_SIZE [[UIScreen mainScreen] bounds].size
#define SAFEAREAINSETS ({UIEdgeInsets i; if (@available(iOS 11.0, *)) {if (IS_IPHONE_X) { i = UIEdgeInsetsMake(22, 22, 21, 22); } else { i = UIEdgeInsetsZero; }} else { i = UIEdgeInsetsZero; } i;})

#pragma mark - Sandbox
#define SandboxDirectory(type) NSSearchPathForDirectoriesInDomains(type, NSUserDomainMask, YES).firstObject
#define LPHDocumentDirectory SandboxDirectory(NSDocumentDirectory) /* 沙盒地址,Document */
#define LPHCacheDirectory SandboxDirectory(NSCachesDirectory)      /* 沙盒地址,Cache */
#define LPHLibraryDirectory SandboxDirectory(NSLibraryDirectory)   /* 沙盒地址,Library */

#pragma mark - UI To iOS Value

/**
 UI效果图的数值,转成当前APP中UI的数值
 */

#define LPHUIScale (IS_IPHONE ? SCREEN_SIZE.width / 360.0f : SCREEN_SIZE.width / 720.0f)
#define LPHUIToValueNoScale(phone,pad) (IS_IPHONE ? (phone / 3.0f) : (pad / 2.0f))
#define LPHUIToValue(phone,pad) (LPHUIToValueNoScale(phone,pad) * LPHUIScale)
#define LPH_SystemFont(phone,pad) [UIFont systemFontOfSize:LPHUIToValue(phone,pad)];

#pragma mark - NSNotificationCenter && NSUserDefaults Quick Getter
#define LPHNotificationCenter [NSNotificationCenter defaultCenter]
#define LPHUserDefaults [NSUserDefaults standardUserDefaults]

#pragma mark - Color

#define LPH_RGBAColor(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:a]
#define LPH_RGBColor(r, g, b) LPH_RGBAColor(r, g, b, 1.0f)

#define LPH_HexRGBAColor(c, a) [UIColor colorWithRed: \
((float)((c & 0xFF0000) >> 16)) / 255.0      \
green: ((float)((c & 0xFF00) >> 8)) / 255.0  \
blue: ((float)(c & 0xFF)) / 255.0            \
alpha: a]

#define LPH_HexRGBColor(c) LPH_HexRGBAColor(c,1.0f)

/**
 定义单例
 */
#pragma mark Singleton
#define LPHHSingleton + (instancetype)sharedInstance; //.h文件中的定义
#define LPHMSingleton /* .m文件中的定义 */ \
static id shareInstance = nil;\
+ (instancetype)sharedInstance {\
    static dispatch_once_t onceToken;\
    dispatch_once(&onceToken, ^{\
        shareInstance = [[self alloc] init];\
    });\
    \
    return shareInstance;\
}\
\
+ (instancetype)allocWithZone:(struct _NSZone *)zone {\
    \
    static dispatch_once_t onceToken;\
    dispatch_once(&onceToken, ^{\
        shareInstance = [super allocWithZone:zone];\
    });\
    \
    return shareInstance;\
}\
\
- (id)copyWithZone:(NSZone *)zone{\
    return shareInstance;\
}

#endif /* LPHDefines_h */

你可能感兴趣的:(OC常用的宏定义)