BAKit_ConfigurationDefine.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. #ifndef BAKit_ConfigurationDefine_h
  2. #define BAKit_ConfigurationDefine_h
  3. #ifndef __OPTIMIZE__
  4. #define NSLog(...) NSLog(__VA_ARGS__)
  5. #else
  6. #define NSLog(...){}
  7. #endif
  8. #pragma mark - weak / strong
  9. #define BAKit_WeakSelf @BAKit_Weakify(self);
  10. #define BAKit_StrongSelf @BAKit_Strongify(self);
  11. /*!
  12. * 强弱引用转换,用于解决代码块(block)与强引用self之间的循环引用问题
  13. * 调用方式: `@BAKit_Weakify`实现弱引用转换,`@BAKit_Strongify`实现强引用转换
  14. *
  15. * 示例:
  16. * @BAKit_Weakify
  17. * [obj block:^{
  18. * @strongify_self
  19. * self.property = something;
  20. * }];
  21. */
  22. #ifndef BAKit_Weakify
  23. #if DEBUG
  24. #if __has_feature(objc_arc)
  25. #define BAKit_Weakify(object) autoreleasepool{} __weak __typeof__(object) weak##_##object = object;
  26. #else
  27. #define BAKit_Weakify(object) autoreleasepool{} __block __typeof__(object) block##_##object = object;
  28. #endif
  29. #else
  30. #if __has_feature(objc_arc)
  31. #define BAKit_Weakify(object) try{} @finally{} {} __weak __typeof__(object) weak##_##object = object;
  32. #else
  33. #define BAKit_Weakify(object) try{} @finally{} {} __block __typeof__(object) block##_##object = object;
  34. #endif
  35. #endif
  36. #endif
  37. /*!
  38. * 强弱引用转换,用于解决代码块(block)与强引用对象之间的循环引用问题
  39. * 调用方式: `@BAKit_Weakify(object)`实现弱引用转换,`@BAKit_Strongify(object)`实现强引用转换
  40. *
  41. * 示例:
  42. * @BAKit_Weakify(object)
  43. * [obj block:^{
  44. * @BAKit_Strongify(object)
  45. * strong_object = something;
  46. * }];
  47. */
  48. #ifndef BAKit_Strongify
  49. #if DEBUG
  50. #if __has_feature(objc_arc)
  51. #define BAKit_Strongify(object) autoreleasepool{} __typeof__(object) object = weak##_##object;
  52. #else
  53. #define BAKit_Strongify(object) autoreleasepool{} __typeof__(object) object = block##_##object;
  54. #endif
  55. #else
  56. #if __has_feature(objc_arc)
  57. #define BAKit_Strongify(object) try{} @finally{} __typeof__(object) object = weak##_##object;
  58. #else
  59. #define BAKit_Strongify(object) try{} @finally{} __typeof__(object) object = block##_##object;
  60. #endif
  61. #endif
  62. #endif
  63. /*! 获取sharedApplication */
  64. #define BAKit_SharedApplication [UIApplication sharedApplication]
  65. // 操作系统版本号
  66. #define BAKit_IOS_VERSION ([[[UIDevice currentDevice] systemVersion] floatValue])
  67. /*! 主线程同步队列 */
  68. #define dispatch_main_sync_safe(block)\
  69. if ([NSThread isMainThread]) {\
  70. block();\
  71. } else {\
  72. dispatch_sync(dispatch_get_main_queue(), block);\
  73. }
  74. /*! 主线程异步队列 */
  75. #define dispatch_main_async_safe(block)\
  76. if ([NSThread isMainThread]) {\
  77. block();\
  78. } else {\
  79. dispatch_async(dispatch_get_main_queue(), block);\
  80. }
  81. #pragma mark - runtime
  82. #import <objc/runtime.h>
  83. /*! runtime set */
  84. #define BAKit_Objc_setObj(key, value) objc_setAssociatedObject(self, key, value, OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  85. /*! runtime setCopy */
  86. #define BAKit_Objc_setObjCOPY(key, value) objc_setAssociatedObject(self, key, value, OBJC_ASSOCIATION_COPY)
  87. /*! runtime get */
  88. #define BAKit_Objc_getObj objc_getAssociatedObject(self, _cmd)
  89. /*! runtime exchangeMethod */
  90. #define BAKit_Objc_exchangeMethodAToB(originalSelector,swizzledSelector) { \
  91. Method originalMethod = class_getInstanceMethod(self, originalSelector); \
  92. Method swizzledMethod = class_getInstanceMethod(self, swizzledSelector); \
  93. if (class_addMethod(self, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))) { \
  94. class_replaceMethod(self, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod)); \
  95. } else { \
  96. method_exchangeImplementations(originalMethod, swizzledMethod); \
  97. } \
  98. }
  99. #pragma mark - 简单警告框
  100. /*! view 用 BAKit_ShowAlertWithMsg */
  101. #define BAKit_ShowAlertWithMsg(msg) [[[UIAlertView alloc] initWithTitle:@"温馨提示" message:(msg) delegate:nil cancelButtonTitle:@"确 定" otherButtonTitles:nil] show];
  102. /*! VC 用 BAKit_ShowAlertWithMsg */
  103. #define BAKit_ShowAlertWithMsg_ios8(msg) UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:msg preferredStyle:UIAlertControllerStyleAlert];\
  104. UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确 定" style:UIAlertActionStyleDefault handler:nil];\
  105. [alert addAction:sureAction];\
  106. [self presentViewController:alert animated:YES completion:nil];
  107. #pragma mark - color
  108. CG_INLINE UIColor *
  109. BAKit_Color_RGBA_pod(u_char r,u_char g, u_char b, u_char a) {
  110. return [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a];
  111. }
  112. CG_INLINE UIColor *
  113. BAKit_Color_RGB_pod(u_char r,u_char g, u_char b) {
  114. return BAKit_Color_RGBA_pod(r, g, b, 1.0);
  115. }
  116. CG_INLINE UIColor *
  117. BAKit_Color_RGBValue_pod(UInt32 rgbValue){
  118. return [UIColor colorWithRed:((rgbValue & 0xff0000) >> 16) / 255.0f
  119. green:((rgbValue & 0xff00) >> 8) / 255.0f
  120. blue:(rgbValue & 0xff) / 255.0f
  121. alpha:1.0f];
  122. }
  123. CG_INLINE UIColor *
  124. BAKit_Color_RGBAValue_pod(UInt32 rgbaValue){
  125. return [UIColor colorWithRed:((rgbaValue & 0xff000000) >> 24) / 255.0f
  126. green:((rgbaValue & 0xff0000) >> 16) / 255.0f
  127. blue:((rgbaValue & 0xff00) >> 8) / 255.0f
  128. alpha:(rgbaValue & 0xff) / 255.0f];
  129. }
  130. CG_INLINE UIColor *
  131. BAKit_Color_RandomRGB_pod(){
  132. return BAKit_Color_RGBValue_pod(arc4random_uniform(0xffffff));
  133. }
  134. CG_INLINE UIColor *
  135. BAKit_Color_RandomRGBA_pod(){
  136. return BAKit_Color_RGBAValue_pod(arc4random_uniform(0xffffffff));
  137. }
  138. #define BAKit_Color_Translucent_pod [UIColor colorWithRed:0.3f green:0.3f blue:0.3f alpha:0.5f]
  139. #define BAKit_Color_White_pod [UIColor whiteColor]
  140. #define BAKit_Color_Clear_pod [UIColor clearColor]
  141. #define BAKit_Color_Black_pod [UIColor blackColor]
  142. #define BAKit_Color_White_pod [UIColor whiteColor]
  143. #define BAKit_Color_Red_pod [UIColor redColor]
  144. #define BAKit_Color_Green_pod [UIColor greenColor]
  145. #define BAKit_Color_Orange_pod [UIColor orangeColor]
  146. #define BAKit_Color_Yellow_pod [UIColor yellowColor]
  147. /*! 灰色 */
  148. #define BAKit_Color_Gray_1_pod BAKit_Color_RGB_pod(53, 60, 70)
  149. #define BAKit_Color_Gray_2_pod BAKit_Color_RGB_pod(73, 80, 90)
  150. #define BAKit_Color_Gray_3_pod BAKit_Color_RGB_pod(93, 100, 110)
  151. #define BAKit_Color_Gray_4_pod BAKit_Color_RGB_pod(113, 120, 130)
  152. #define BAKit_Color_Gray_5_pod BAKit_Color_RGB_pod(133, 140, 150)
  153. #define BAKit_Color_Gray_6_pod BAKit_Color_RGB_pod(153, 160, 170)
  154. #define BAKit_Color_Gray_7_pod BAKit_Color_RGB_pod(173, 180, 190)
  155. #define BAKit_Color_Gray_8_pod BAKit_Color_RGB_pod(196, 200, 208)
  156. #define BAKit_Color_Gray_9_pod BAKit_Color_RGB_pod(216, 220, 228)
  157. #define BAKit_Color_Gray_10_pod BAKit_Color_RGB_pod(240, 240, 240)
  158. #define BAKit_Color_Gray_11_pod BAKit_Color_RGB_pod(248, 248, 248)
  159. #pragma mark - Margin
  160. #define BAKit_Margin_1_pod BAKit_Flat_pod(1)
  161. #define BAKit_Margin_2_pod BAKit_Flat_pod(2)
  162. #define BAKit_Margin_5_pod BAKit_Flat_pod(5)
  163. #define BAKit_Margin_10_pod BAKit_Flat_pod(10)
  164. #define BAKit_Margin_15_pod BAKit_Flat_pod(15)
  165. #define BAKit_Margin_20_pod BAKit_Flat_pod(20)
  166. #define BAKit_Margin_25_pod BAKit_Flat_pod(25)
  167. #define BAKit_Margin_30_pod BAKit_Flat_pod(30)
  168. #define BAKit_Margin_35_pod BAKit_Flat_pod(35)
  169. #define BAKit_Margin_40_pod BAKit_Flat_pod(40)
  170. #define BAKit_Margin_44_pod BAKit_Flat_pod(44)
  171. #define BAKit_Margin_50_pod BAKit_Flat_pod(50)
  172. #define BAKit_Margin_100_pod BAKit_Flat_pod(100)
  173. #define BAKit_Margin_150_pod BAKit_Flat_pod(150)
  174. #define BAKit_ImageName(imageName) [UIImage imageNamed:imageName]
  175. #pragma mark - NotiCenter other
  176. #define BAKit_NotiCenter [NSNotificationCenter defaultCenter]
  177. #define BAKit_NSUserDefaults [NSUserDefaults standardUserDefaults]
  178. /*! 获取sharedApplication */
  179. #define BAKit_SharedApplication [UIApplication sharedApplication]
  180. /*! 用safari打开URL */
  181. #define BAKit_OpenUrl(urlStr) [BAKit_SharedApplication openURL:[NSURL URLWithString:urlStr]]
  182. /*! 复制文字内容 */
  183. #define BAKit_CopyContent(content) [[UIPasteboard generalPasteboard] setString:content]
  184. /*!
  185. * 获取屏幕宽度和高度
  186. */
  187. #define BAKit_SCREEN_WIDTH ((([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait) || ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)) ? [[UIScreen mainScreen] bounds].size.width : [[UIScreen mainScreen] bounds].size.height)
  188. #define BAKit_SCREEN_HEIGHT ((([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait) || ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)) ? [[UIScreen mainScreen] bounds].size.height : [[UIScreen mainScreen] bounds].size.width)
  189. #define BAKit_BaseScreenWidth 320.0f
  190. #define BAKit_BaseScreenHeight 568.0f
  191. /*! 屏幕适配(5S标准屏幕:320 * 568) */
  192. // iPhone 7 屏幕:375 * 667
  193. //376/320 =
  194. //667/568 =
  195. #define BAKit_ScaleXAndWidth BAKit_SCREEN_WIDTH/BAKit_BaseScreenWidth
  196. #define BAKit_ScaleYAndHeight BAKit_SCREEN_HEIGHT/BAKit_BaseScreenHeight
  197. #define BAKit_ScreenScale ([[UIScreen mainScreen] scale])
  198. CG_INLINE BOOL
  199. BAKit_stringIsBlank_pod(NSString *string) {
  200. NSCharacterSet *blank = [NSCharacterSet whitespaceAndNewlineCharacterSet];
  201. for (NSInteger i = 0; i < string.length; ++i) {
  202. unichar c = [string characterAtIndex:i];
  203. if (![blank characterIsMember:c]) {
  204. return NO;
  205. }
  206. }
  207. return YES;
  208. }
  209. /**
  210. * 基于指定的倍数,对传进来的 floatValue 进行像素取整。若指定倍数为0,则表示以当前设备的屏幕倍数为准。
  211. *
  212. * 例如传进来 “2.1”,在 2x 倍数下会返回 2.5(0.5pt 对应 1px),在 3x 倍数下会返回 2.333(0.333pt 对应 1px)。
  213. */
  214. CG_INLINE CGFloat
  215. BAKit_FlatSpecificScale_pod(CGFloat floatValue, CGFloat scale) {
  216. scale = scale == 0 ? BAKit_ScreenScale : scale;
  217. CGFloat flattedValue = ceil(floatValue * scale) / scale;
  218. return flattedValue;
  219. }
  220. /**
  221. * 基于当前设备的屏幕倍数,对传进来的 floatValue 进行像素取整。
  222. *
  223. * 注意如果在 Core Graphic 绘图里使用时,要注意当前画布的倍数是否和设备屏幕倍数一致,若不一致,不可使用 flat() 函数,而应该用 flatSpecificScale
  224. */
  225. CG_INLINE CGFloat
  226. BAKit_Flat_pod(CGFloat floatValue) {
  227. return BAKit_FlatSpecificScale_pod(floatValue, 0);
  228. }
  229. /// 将一个CGSize像素对齐
  230. CG_INLINE CGSize
  231. BAKit_CGSizeFlatted_pod(CGSize size) {
  232. return CGSizeMake(BAKit_Flat_pod(size.width), BAKit_Flat_pod(size.height));
  233. }
  234. /// 创建一个像素对齐的CGRect
  235. CG_INLINE CGRect
  236. BAKit_CGRectFlatMake_pod(CGFloat x, CGFloat y, CGFloat width, CGFloat height) {
  237. return CGRectMake(BAKit_Flat_pod(x), BAKit_Flat_pod(y), BAKit_Flat_pod(width), BAKit_Flat_pod(height));
  238. }
  239. /**
  240. 计算列数【根据 array.count、每行多少个 item,计算列数】
  241. @param array array
  242. @param rowCount 每行多少个 item
  243. @return 列数
  244. */
  245. CG_INLINE NSInteger
  246. BAKit_getColumnCountWithArrayAndRowCount_pod(NSArray *array, NSInteger rowCount){
  247. NSUInteger count = array.count;
  248. NSUInteger i = 0;
  249. if (count % rowCount == 0)
  250. {
  251. i = count / rowCount;
  252. }
  253. else
  254. {
  255. i = count / rowCount + 1;
  256. }
  257. return i;
  258. }
  259. #endif /* BAKit_ConfigurationDefine_h */