WHMacro.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //
  2. // WHMacro.h
  3. // WHKit
  4. // https://github.com/remember17/WHKit
  5. // Created by 吴浩 on 2017/6/7.
  6. // Copyright © 2017年 remember17. All rights reserved.
  7. // http://www.jianshu.com/p/c935314b078e
  8. //NSLog
  9. #ifdef DEGUG
  10. #define NSLog(...) NSLog(@"%s 第%d行: %@\n\n",__func__,__LINE__,[NSString stringWithFormat:__VA_ARGS__])
  11. #else
  12. #define NSLog(...)
  13. #endif
  14. //是否为iPhone X
  15. #define kIs_iPhoneX ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) : NO)
  16. //APP版本号
  17. #define KAppVersion [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]
  18. //系统版本号
  19. #define kSystemVersion [[UIDevice currentDevice] systemVersion]
  20. //黑色和白色
  21. #define kWhiteColor [UIColor whiteColor]
  22. #define kBlackColor [UIColor blackColor]
  23. // View 圆角和加边框
  24. #define kViewBorderRadius(View, Radius, Width, Color)\
  25. \
  26. [View.layer setCornerRadius:(Radius)];\
  27. [View.layer setMasksToBounds:YES];\
  28. [View.layer setBorderWidth:(Width)];\
  29. [View.layer setBorderColor:[Color CGColor]]
  30. // View 圆角
  31. #define kViewRadius(View, Radius)\
  32. \
  33. [View.layer setCornerRadius:(Radius)];\
  34. [View.layer setMasksToBounds:YES]
  35. //字符串是否为空
  36. #define kIsStringEmpty(str) ([str isKindOfClass:[NSNull class]] || str == nil || [str length] < 1 ? YES : NO )
  37. //数组是否为空
  38. #define kIsArrayEmpty(array) (array == nil || [array isKindOfClass:[NSNull class]] || array.count == 0)
  39. //字典是否为空
  40. #define kIsDictEmpty(dic) (dic == nil || [dic isKindOfClass:[NSNull class]] || dic.allKeys == 0)
  41. //是否是空对象
  42. #define kIsObjectEmpty(_object) (_object == nil \
  43. || [_object isKindOfClass:[NSNull class]] \
  44. || ([_object respondsToSelector:@selector(length)] && [(NSData *)_object length] == 0) \
  45. || ([_object respondsToSelector:@selector(count)] && [(NSArray *)_object count] == 0))
  46. //屏幕宽度
  47. #define kScreenWidth \
  48. ([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)] ? [UIScreen mainScreen].nativeBounds.size.width/[UIScreen mainScreen].nativeScale : [UIScreen mainScreen].bounds.size.width)
  49. //屏幕高度
  50. #define kScreenHeight \
  51. ([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)] ? [UIScreen mainScreen].nativeBounds.size.height/[UIScreen mainScreen].nativeScale : [UIScreen mainScreen].bounds.size.height)
  52. //屏幕Size
  53. #define kScreenSize \
  54. ([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)] ? CGSizeMake([UIScreen mainScreen].nativeBounds.size.width/[UIScreen mainScreen].nativeScale,[UIScreen mainScreen].nativeBounds.size.height/[UIScreen mainScreen].nativeScale) : [UIScreen mainScreen].bounds.size)
  55. //状态栏加导航栏高度
  56. #define kTopHeight (kIs_iPhoneX ? 88 : 64)
  57. //TabBar高度
  58. #define kTabBarHeight (kIs_iPhoneX ? 83 : 49)
  59. //Aplication
  60. #define kApplication [UIApplication sharedApplication]
  61. //keyWindow
  62. #define kKeyWindow [UIApplication sharedApplication].keyWindow
  63. //AppDelegate
  64. #define kAppdelegate [UIApplication sharedApplication].delegate
  65. //UserDefaults
  66. #define kUserDefault [NSUserDefaults standardUserDefaults]
  67. //NSNotificationCenter
  68. #define kNotificationCenter [NSNotificationCenter defaultCenter]
  69. //获取当前语言
  70. #define kCurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0])
  71. //判断是否为iPhone
  72. #define kIs_iPhone (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
  73. //判断是否为iPad
  74. #define kIs_iPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  75. //获取沙盒Document路径
  76. #define kDocumentPath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
  77. //获取沙盒temp路径
  78. #define kTempPath NSTemporaryDirectory()
  79. //获取沙盒Cache路径
  80. #define kCachePath [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]
  81. //判断是真机还是模拟器
  82. #if TARGET_OS_IPHONE
  83. //真机
  84. #endif
  85. #if TARGET_IPHONE_SIMULATOR
  86. //模拟器
  87. #endif
  88. //颜色
  89. #define kRGB(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
  90. #define kRGBA(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(r)/255.0 blue:(r)/255.0 alpha:a]
  91. #define kRandomColor KRGB_COLOR(arc4random_uniform(256),arc4random_uniform(256),arc4random_uniform(256))
  92. /**
  93. 十六进制表示颜色
  94. @param hexValue 十六进制值,不是字符串
  95. @param alphaValue 透明度
  96. @return 颜色
  97. */
  98. #define kHexColor(hexValue, alphaValue) \
  99. [UIColor colorWithRed:((float)((hexValue & 0xFF0000) >> 16)) / 255.0 \
  100. green:((float)((hexValue & 0xFF00) >> 8)) / 255.0 \
  101. blue:((float)(hexValue & 0xFF)) / 255.0 alpha:alphaValue]
  102. //弱引用/强引用
  103. #define kWeakSelf(type) __weak typeof(type) weak##type = type;
  104. #define kStrongSelf(type) __strong typeof(type) type = weak##type;
  105. // 必须成对使用,使用该宏前必须加@,如@weakify(self); @strongify(self);
  106. #define weakify( x ) autoreleasepool{} __weak typeof(x) weak##x = x
  107. #define strongify( x ) autoreleasepool{} __strong typeof(weak##x) x = weak##x
  108. #define onExit(deferBlock) \
  109. autoreleasepool{} __strong nob_defer_block_t nob_macro_concat(__nob_stack_defer_block_, __LINE__) __attribute__((cleanup(nob_deferFunc), unused)) = deferBlock
  110. //由角度转换弧度
  111. #define kDegreesToRadian(x) (M_PI * (x) / 180.0)
  112. //由弧度转换角度
  113. #define kRadianToDegrees(radian) (radian * 180.0) / (M_PI)
  114. //获取一段时间间隔
  115. #define kStart CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
  116. #define KEnd NSLog(@"Time: %f", CFAbsoluteTimeGetCurrent() - start)
  117. // 图片
  118. #define kImageOfFile(NAME,EXT) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:(NAME) ofType:(EXT)]]
  119. #define kImageNamed(NAME) [UIImage imageNamed:NAME]
  120. // 多行文本获取高度
  121. #if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_7_0
  122. #define kMultiLineTextSize(text, font, maxSize) [text length] > 0 ? [text \
  123. boundingRectWithSize:maxSize options:(NSStringDrawingUsesLineFragmentOrigin) \
  124. attributes:@{NSFontAttributeName:font} context:nil].size : CGSizeZero;
  125. #else
  126. #define kMultiLineTextSize(text, font, maxSize) [text length] > 0 ? [text \
  127. sizeWithFont:font constrainedToSize:maxSize] : CGSizeZero;
  128. #endif
  129. // 字体大小(常规/粗体)
  130. #define kBoldFont(FONTSIZE) [UIFont boldSystemFontOfSize:FONTSIZE]
  131. #define kSystemFont(FONTSIZE) [UIFont systemFontOfSize:FONTSIZE]
  132. #define kFont(NAME,FONTSIZE) [UIFont fontWithName:(NAME) size:(FONTSIZE)]