IOS常用宏定义

  1. //  
  2. //  PrefixHeader.pch  
  3. //  EJW-IOS  
  4. //  
  5. //  Created by iroycn  
  6. //  
  7.   
  8. #ifndef PrefixHeader_pch  
  9. #define PrefixHeader_pch  
  10.   
  11.   
  12. //常用字符  
  13.   
  14. // Include any system framework and library headers here that should be included in all compilation units.  
  15. // You will also need to set the Prefix Header build setting of one or more of your targets to reference this file.  
  16.   
  17. //<><><>获取屏幕高宽<><><><><>(注释的是支持横竖屏,但我们项目里没有用到,所以注释,只支持ios8以上)  
  18. //#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000 // 当前Xcode支持iOS8及以上  
  19.   
  20. //#define kSCREEN_WIDTH ([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)]?[UIScreen mainScreen].nativeBounds.size.width/[UIScreen mainScreen].nativeScale:[UIScreen mainScreen].bounds.size.width)  
  21. //#define kSCREENH_HEIGHT ([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)]?[UIScreen mainScreen].nativeBounds.size.height/[UIScreen mainScreen].nativeScale:[UIScreen mainScreen].bounds.size.height)  
  22. //#define kSCREEN_SIZE ([[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)  
  23. //#else  
  24. #define kSCREEN_WIDTH   [UIScreen mainScreen].bounds.size.width  
  25. #define kSCREENH_HEIGHT [UIScreen mainScreen].bounds.size.height  
  26. #define kSCREEN_SIZE [UIScreen mainScreen].bounds.size  
  27. //#endif  
  28. //<><><><><><><><><><><><><>  
  29.   
  30. //<><><>缓存<><><><><>  
  31. #import "TMCache.h"  
  32. #define kSetCache(key,value) [[TMCache sharedCache] setObject:value forKey:key];  
  33. #define kGetCache(key) [[TMCache sharedCache] objectForKey:key];  
  34. #define kRemoveCache(key) [[TMCache sharedCache] removeObjectForKey:key];  
  35. #define kRemoveAllCache [[TMCache sharedCache] removeAllObjects];  
  36.   
  37.   
  38. //<><><>获取通知中心<><><><><>  
  39. #define kNotificationCenter [NSNotificationCenter defaultCenter]  
  40.   
  41.   
  42. //<><><>设置随机颜色<><><><><>  
  43. #define kRandomColor [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0]  
  44.   
  45.   
  46. //<><><>设置RGB颜色/设置RGBA颜色<><><><><>  
  47. //RGB色系  
  48. #define kColorA(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(r)/255.0 blue:(r)/255.0 alpha:a]  
  49. #define kColor(r, g, b) kColorA(r, g, b, 1.0)  
  50. //16进制 ->#ffffff  
  51. #define kColorHexA(hexValue,a) [UIColor colorWithRed:((float)((hexValue & 0xFF0000) >> 16))/255.0 green:((float)((hexValue & 0xFF00) >> 8))/255.0 blue:((float)(hexValue & 0xFF))/255.0 alpha:a]  
  52. #define kColorHex(hexValue)            kColorHexA(hexValue,1.0)  
  53. //清除  
  54. #define kClearColor [UIColor clearColor]  
  55.   
  56.   
  57. //<><><>自定义高效率的 NSLog<><><><><>  
  58.   
  59. #ifdef DEBUG  
  60. #define kLog(...) NSLog(@"%s 第%d行 \n %@\n\n",__func__,__LINE__,[NSString stringWithFormat:__VA_ARGS__])  
  61. #else  
  62. #define kLog(...)  
  63.   
  64. #endif  
  65.   
  66.   
  67. //<><><>弱引用/强引用<><><><><>  
  68. #define kWeakSelf(type)  __weak typeof(type) weak##type = type;  
  69. #define kStrongSelf(type)  __strong typeof(type) type = weak##type;  
  70.   
  71.   
  72. //<><><>设置 view 圆角和边框<><><><><>  
  73.   
  74. #define kBorderRadius(View, Radius, Width, Color)\  
  75. \  
  76. [View.layer setCornerRadius:(Radius)];\  
  77. [View.layer setMasksToBounds:YES];\  
  78. [View.layer setBorderWidth:(Width)];\  
  79. [View.layer setBorderColor:[Color CGColor]]  
  80.   
  81.   
  82. #import "MBProgressHUD.h"  
  83.   
  84. //<><><>设置加载提示框(第三方框架:MBProgressHUD)<><><><><>  
  85.   
  86. // 加载  
  87. #define kShowNetworkActivityIndicator() [UIApplication sharedApplication].networkActivityIndicatorVisible = YES  
  88. // 收起加载  
  89. #define HideNetworkActivityIndicator()      [UIApplication sharedApplication].networkActivityIndicatorVisible = NO  
  90. // 设置加载  
  91. #define NetworkActivityIndicatorVisible(x)  [UIApplication sharedApplication].networkActivityIndicatorVisible = x  
  92. //window对象  
  93. #define kWindow [UIApplication sharedApplication].keyWindow  
  94.   
  95. #define kBackView         for (UIView *item in kWindow.subviews) { \  
  96. if(item.tag == 10000) \  
  97. { \  
  98. [item removeFromSuperview]; \  
  99. UIView * aView = [[UIView alloc] init]; \  
  100. aView.frame = [UIScreen mainScreen].bounds; \  
  101. aView.tag = 10000; \  
  102. aView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:1]; \  
  103. [kWindow addSubview:aView]; \  
  104. } \  
  105. } \  
  106. //MB,在window层显示  
  107. #define kShowHUDAndActivity kBackView;[MBProgressHUD showHUDAddedTo:kWindow animated:YES];kShowNetworkActivityIndicator()  
  108. #define kHiddenHUD [MBProgressHUD hideAllHUDsForView:kWindow animated:YES]  
  109.   
  110. #define kRemoveBackView         for (UIView *item in kWindow.subviews) { \  
  111. if(item.tag == 10000) \  
  112. { \  
  113. [UIView animateWithDuration:0.4 animations:^{ \  
  114. item.alpha = 0.0; \  
  115. } completion:^(BOOL finished) { \  
  116. [item removeFromSuperview]; \  
  117. }]; \  
  118. } \  
  119. } \  
  120.   
  121. #define kHiddenHUDAndAvtivity kRemoveBackView;kHiddenHUD;HideNetworkActivityIndicator()  
  122.   
  123. //>>>>>>>>>>>>>>>>>>>>>>>>>  
  124. //<><><><><><><><><><>显示加载菊花  
  125. //我自己写的,显示的位置  
  126. #define kShowHUD(view) kBackView;[MBProgressHUD showHUDAddedTo:view animated:YES];kShowNetworkActivityIndicator()  
  127. //隐藏  
  128. #define kHideHUD(view) [MBProgressHUD hideAllHUDsForView:view animated:YES]  
  129. //<><><><><><><><><><>显示问题提醒  
  130. #define kShowLabel(view,text) MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];\  
  131. hud.labelText = text;\  
  132. hud.mode = MBProgressHUDModeText;\  
  133. hud.removeFromSuperViewOnHide = YES;\  
  134. [hud hide:YES afterDelay:0.7];\  
  135.   
  136.   
  137.   
  138. //<><><>获取图片资源<><><><><>  
  139. //读取本地图片  
  140. #define kLoadImage(file,ext) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:file ofType:ext]]  
  141. //定义UIImage对象  
  142. #define kIMAGE(A) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:A ofType:nil]]  
  143. //获取图片;前面2种性能比这个高,但这个常用,assets.xcassets里的用这个吧  
  144. #define kImageName(imageName) [UIImage imageNamed:[NSString stringWithFormat:@"%@",imageName]]  
  145.   
  146.   
  147. //<><><>获取系统版本<><><><><>  
  148. //获取当前版本  
  149. #define IOS_SYSTEM_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]  
  150. //判断 iOS 8 或更高的系统版本  
  151. //#define IOS_VERSION_8_OR_LATER (([[[UIDevice currentDevice] systemVersion] floatValue] >=8.0)? (YES):(NO))  
  152. //等于  
  153. #define IOS_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)  
  154. //大于  
  155. #define IOS_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)  
  156. //大于等于  
  157. #define IOS_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)  
  158. //小于  
  159. #define IOS_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)  
  160. //小于等于  
  161. #define IOS_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)  
  162.   
  163.   
  164. //<><><>判断是真机还是模拟器<><><><><>  
  165. #if TARGET_OS_IPHONE  
  166. //iPhone Device  
  167. #endif  
  168.   
  169. #if TARGET_IPHONE_SIMULATOR  
  170. //iPhone Simulator  
  171. #endif  
  172.   
  173.   
  174. //<><><>沙盒目录文件<><><><><>  
  175.   
  176. //获取temp  
  177. #define kPathTemp NSTemporaryDirectory()  
  178.   
  179. //获取沙盒 Document  
  180. #define kPathDocument [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]  
  181.   
  182. //获取沙盒 Cache  
  183. #define kPathCache [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]  
  184.   
  185.   
  186.   
  187. //<><><>Frame操作相关<><><><><>/  
  188. //获取垂直居中的x(父的高度/2-子的高度/2)  
  189. #define CENTER_VERTICALLY(parent,child) floor((parent.frame.size.height - child.frame.size.height) / 2)  
  190. //获取水平居中的y(父的宽度/2-子的宽度/2)  
  191. #define CENTER_HORIZONTALLY(parent,child) floor((parent.frame.size.width - child.frame.size.width) / 2)  
  192.   
  193. // example: [[UIView alloc] initWithFrame:(CGRect){CENTER_IN_PARENT(parentView,500,500),CGSizeMake(500,500)}];  
  194. #define CENTER_IN_PARENT(parent,childWidth,childHeight) CGPointMake(floor((parent.frame.size.width - childWidth) / 2),floor((parent.frame.size.height - childHeight) / 2))  
  195. #define CENTER_IN_PARENT_X(parent,childWidth) floor((parent.frame.size.width - childWidth) / 2)  
  196. #define CENTER_IN_PARENT_Y(parent,childHeight) floor((parent.frame.size.height - childHeight) / 2)  
  197. //view的宽度  
  198. #define WIDTH(view) view.frame.size.width  
  199. //view的高度  
  200. #define HEIGHT(view) view.frame.size.height  
  201. //view的x  
  202. #define X(view) view.frame.origin.x  
  203. //view的y  
  204. #define Y(view) view.frame.origin.y  
  205. //view的x  
  206. #define LEFT(view) view.frame.origin.x  
  207. //view的y  
  208. #define TOP(view) view.frame.origin.y  
  209. //view的bottom的y  
  210. #define BOTTOM(view) (view.frame.origin.y + view.frame.size.height)  
  211. //view的right的x  
  212. #define RIGHT(view) (view.frame.origin.x + view.frame.size.width)  
  213.   
  214.   
  215.   
  216. //<><><>定义UIFont,默认system<><><><><>  
  217. #define FONT(F) [UIFont systemFontOfSize:F]  
  218.   
  219.   
  220. //<><><NSUserDefaults 实例化<><><><><>  
  221. #define USER_DEFAULT [NSUserDefaults standardUserDefaults]  
  222.   
  223.   
  224. //<><>初始化一个普通的alert view<><><><><>  
  225. #define kALERT(info)\  
  226. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:info delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];\  
  227. [alert show];  
  228.   
  229.   
  230. //<><><NSUserDefaults 实例化<><><><><>  
  231.   
  232.   
  233. //<><><NSUserDefaults 实例化<><><><><>  
  234.   
  235.   
  236. //<><><NSUserDefaults 实例化<><><><><>  
  237.   
  238.   
  239. //<><><NSUserDefaults 实例化<><><><><>  
  240.   
  241.   
  242. //<><><NSUserDefaults 实例化<><><><><>  
  243.   
  244.   
  245. //<><><NSUserDefaults 实例化<><><><><>  
  246.   
  247.   
  248. //<><><NSUserDefaults 实例化<><><><><>  
  249.   
  250.   
  251. //是否为empty  
  252. static inline BOOL IsEmpty(id thing) {  
  253.     return thing == nil || [thing isEqual:[NSNull null]]  
  254.     || ([thing respondsToSelector:@selector(length)]  
  255.         && [(NSData *)thing length] == 0)  
  256.     || ([thing respondsToSelector:@selector(count)]  
  257.         && [(NSArray *)thing count] == 0);  
  258. }  
  259.   
  260. //未知字符串(nil)转为字符串(非nil);在不确定字符串是否为nil的情况下使用  
  261. static inline NSString *StringFromObject(id object) {  
  262.     if (object == nil || [object isEqual:[NSNull null]]) {  
  263.         return @"";  
  264.     } else if ([object isKindOfClass:[NSString class]]) {  
  265.         return object;  
  266.     } else if ([object respondsToSelector:@selector(stringValue)]){  
  267.         return [object stringValue];  
  268.     } else {  
  269.         return [object description];  
  270.     }  
  271. }  
  272.   
  273.   
  274. #endif /* PrefixHeader_pch */  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值