BAKit_ConfigurationDefine.h 13 KB

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