iOS 常用宏定义
//========================宽高============================
// Frame
#define SCREEN_FRAME ([UIScreen mainScreen].applicationFrame)
// 宽度
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
// 高度
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
//等比例坐标
#define KWidthFit(w) (((w) / 375.0) * SCREEN_WIDTH)
#define KHeightFit(w) (((w) / 667.0) * SCREEN_HEIGHT)
/*状态栏高度*/
#define kStatusBarHeight (CGFloat)(kIs_iPhoneX?(44.0):(20.0))
/*导航栏高度*/
#define kNavBarHeight (44)
/*状态栏和导航栏总高度*/
#define kNavBarAndStatusBarHeight (CGFloat)(kIs_iPhoneX?(88.0):(64.0))
/*TabBar高度*/
#define kTabBarHeight (CGFloat)(kIs_iPhoneX?(49.0 + 34.0):(49.0))
/*顶部安全区域远离高度*/
#define kTopBarSafeHeight (CGFloat)(kIs_iPhoneX?(44.0):(0))
/*底部安全区域远离高度*/
#define kBottomSafeHeight (CGFloat)(kIs_iPhoneX?(34.0):(0))
/*iPhoneX的状态栏高度差值*/
#define kTopBarDifHeight (CGFloat)(kIs_iPhoneX?(24.0):(0))
/*导航条和Tabbar总高度*/
#define kNavAndTabHeight (kNavBarAndStatusBarHeight + kTabBarHeight)
//========================设备型号============================
//是否是iphoneX 以上的设备
#define ISIphoneX (([UIScreen mainScreen].bounds.size.height/[UIScreen mainScreen].bounds.size.width) >= 2.16)
#define IS_IPHONE_X ((IS_IPHONE && HEIGHT_SCREEN == 812.0)? YES : NO) || ((IS_IPHONE && HEIGHT_SCREEN == 896.0)? YES : NO)
#define kIs_iphone (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define kIs_iPhoneX SCREEN_WIDTH >=375.0f && SCREEN_HEIGHT >=812.0f&& kIs_iphone
// 判断是否为iPhone
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
// 判断是否为iPad
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
// 判断是否为ipod
#define IS_IPOD ([[[UIDevice currentDevice] model] isEqualToString:@"iPod touch"])
// 获取系统版本
#define IOS_SYSTEM_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]
//判断系统版本
#define iOS9Later ([UIDevice currentDevice].systemVersion.floatValue >= 9.0f)
#define iOS10Later ([UIDevice currentDevice].systemVersion.floatValue >= 10.0f)
#define iOS11Later ([UIDevice currentDevice].systemVersion.floatValue >= 11.0f)
// 判断 iOS 9 或更高的系统版本
#define IOS_VERSION_9_OR_LATER (([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0) ? (YES) : (NO))
// 判断是真机还是模拟器
#if TARGET_OS_IPHONE
// iPhone Device
#endif
#if TARGET_IPHONE_SIMULATOR
// iPhone Simulator
#endif
//========================颜色相关============================
#define AppMainColor [UIColor cz_ToUIColorByStr:@"#09122f"]
#define AppSubLabelColor [UIColor cz_ToUIColorByStr:@"#FF0500"]
#define AppTitleColor [UIColor cz_ToUIColorByStr:@"#FFFFFF"]
#define AppButtonTitleColor [UIColor cz_ToUIColorByStr:@"#87c38f"]
//渐变色
#define KGradientColor(f,c1,c2) [UIColor colorWithGradientStyle:UIGradientStyleTopToBottom withFrame:(f) andColors:@[[UIColor colorWithHexString:(c1)],[UIColor colorWithHexString:(c2)]]]
//样式
#define KGradientColorWithStyle(style,frame,c1,c2) [UIColor colorWithGradientStyle:(style) withFrame:(frame) andColors:@[[UIColor colorWithHexString:(c1)],[UIColor colorWithHexString:(c2)]]]
#define KColorHex(hexString) [UIColor colorWithHexString:hexString]
// RGBA
#define KRGBA(R /*红*/, G /*绿*/, B /*蓝*/, A /*透明*/) [UIColor colorWithRed: (float)R / 255.f green: (float)G / 255.f blue: (float)B / 255.f alpha: A]
// RGB
#define KRGB(r, g, b) [UIColor colorWithRed: (r) / 255.0 green: (g) / 255.0 blue: (b) / 255.0 alpha: 1.0]
// 设置随机颜色
#define KRandomColor [UIColor colorWithRed:arc4random_uniform(256) / 255.0 green:arc4random_uniform(256) / 255.0 blue:arc4random_uniform(256) / 255.0 alpha:1.0]
// clear背景颜色
#define KClearColor [UIColor clearColor]
//========================UIKit相关============================
#define KBoldFont(x) [UIFont boldSystemFontOfSize:x]
#define KFont(x) [UIFont systemFontOfSize:x]
#define KNameFont(x) [UIFont fontWithName:@"PingFang SC"size:x]
#define KUrlString(x) [NSURL URLWithString:x]
#define KImageNamed(x) [UIImage imageNamed:x]
#define KString(x) [NSString stringWithFormat:@"%@",x]
// 设置 view 圆角和边框
#define KViewBorderRadius(View, Radius, Width, Color) \
\
[View.layer setCornerRadius: (Radius)]; \
[View.layer setMasksToBounds:YES]; \
[View.layer setBorderWidth:(Width)]; \
[View.layer setBorderColor:[Color CGColor]]
//========================打印============================
#define IS_DEBUG 1
// 自定义高效率的 NSLog
#ifdef DEBUG
#define KLog(...) NSLog(@"%s 第%d行 \n %@\n\n", __func__, __LINE__, [NSString stringWithFormat:__VA_ARGS__])
#else
#define KLog(...)
#endif
//========================弱&强引用============================
#define KWeakSelf(type) __weak typeof(type) weakSelf = type;
#define KStrongSelf(type) __strong typeof(type) type = weakSelf;
//========================系统对象============================
// 获取keyWindow
#define KWindow [UIApplication sharedApplication].keyWindow
//获取window
#define KGetWindow [[UIApplication sharedApplication].delegate window]
#define KAppDelegate ((AppDelegate *)[[UIApplication sharedApplication] delegate])
#define KObjectAlloc(object,name) object * (name) = [[object alloc]init]
// 沙盒目录
#define KUserDefaults [NSUserDefaults standardUserDefaults]
// 获取通知中心
#define KNotificationCenter [NSNotificationCenter defaultCenter]
// 设置状态条加载
#define KShowNetworkActivityIndicator() [UIApplication sharedApplication].networkActivityIndicatorVisible = YES
// 收起状态条加载
#define KHideNetworkActivityIndicator() [UIApplication sharedApplication].networkActivityIndicatorVisible = NO
// MBProgressHUD加载
#define KShowHUDAndActivity kBackView; [MBProgressHUD showHUDAddedTo:KWindow animated:YES]; kShowNetworkActivityIndicator()
// MBProgressHUD隐藏
#define KHiddenHUD [MBProgressHUD hideAllHUDsForView:KWindow animated:YES]; kHideNetworkActivityIndicator()
#define MBPSHOW(x) [MBProgressHUD showMessage:x toview:KWindow afterDelay:1];
#define KMBPSHOW(x,y) [MBProgressHUD showMessage:x toview:KWindow afterDelay:y];
// 获取当前语言
#define KCurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0])
// 使用 ARC 和 MRC
#if __has_feature(objc_arc)
// ARC
#else
// MRC
#endif
// 沙盒目录文件
// 获取temp
#define KPathTemp NSTemporaryDirectory()
// 获取沙盒 Document
#define KPathDocument [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
// 获取沙盒 Cache
#define KPathCache [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]
//========================GCD============================
// GCD 的宏定义
// GCD - 一次性执行
#define KDISPATCH_ONCE_BLOCK(onceBlock) static dispatch_once_t onceToken; dispatch_once(&onceToken, onceBlock);
// GCD - 在Main线程上运行
#define KDISPATCH_MAIN_THREAD(mainQueueBlock) dispatch_async(dispatch_get_main_queue(), mainQueueBlock);
// GCD - 开启异步线程
#define KDISPATCH_GLOBAL_QUEUE_DEFAULT(globalQueueBlock) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), globalQueueBlocl);