1. iOS开发过程中使用一些常用的宏可以提高开发效率,提高代码的重用性;将这些宏放到一个头文件里然后再放到工程中的-Prefix.pch文件中(或者直接放到-Prefix.pch中)直接可以使用,灰常方便。
  2. 做了一些分类和注释,可以根据自己习惯再添加或者删除或者修改这些宏进行使用。
  3.  
  4. #ifndef MacroDefinition_h
  5. #define MacroDefinition_h
  6. //AppDelegate
  7.  
  8. #define APPDELEGATE [(AppDelegate*)[UIApplication sharedApplication] delegate]
  9. //----------------------系统设备相关----------------------------
  10. //获取设备屏幕尺寸
  11. #define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
  12. #define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)//应用尺寸
  13. #define APP_WIDTH [[UIScreen mainScreen]applicationFrame].size.width
  14. #define APP_HEIGHT [[UIScreen mainScreen]applicationFrame].size.height
  15. //获取系统版本
  16. #define IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]
  17. #define CurrentSystemVersion [[UIDevice currentDevice] systemVersion]
  18. #define isIOS4 ([[[UIDevice currentDevice] systemVersion] intValue]==4)
  19. #define isIOS5 ([[[UIDevice currentDevice] systemVersion] intValue]==5)
  20. #define isIOS6 ([[[UIDevice currentDevice] systemVersion] intValue]==6)
  21. #define isAfterIOS4 ([[[UIDevice currentDevice] systemVersion] intValue]>4)
  22. #define isAfterIOS5 ([[[UIDevice currentDevice] systemVersion] intValue]>5)
  23. #define isAfterIOS6 ([[[UIDevice currentDevice] systemVersion] intValue]>6)
  24. //获取当前语言
  25. #define CurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0])
  26.  
  27. //判断是否 Phone 4/5/6 是否是iPad
  28. #define Phone4 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO)
  29. #define Phone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
  30. #define Phone6 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(750, 1334), [[UIScreen mainScreen] currentMode].size) : NO)
  31. #define Phone6Plus ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242,2208), [[UIScreen mainScreen] currentMode].size) : NO)
  32. #define isPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  33. //判断是真机还是模拟器
  34. #if TARGET_OS_IPHONE
  35. //iPhone Device
  36. #endif
  37. #if TARGET_IPHONE_SIMULATOR
  38. //iPhone Simulator
  39. #endif
  40. //----------------------系统设备相关----------------------------
  41.  
  42. //----------------------内存相关----------------------------
  43. //使用ARC和不使用ARC
  44. #if __has_feature(objc_arc)
  45. //compiling with ARC
  46. #else
  47. // compiling without ARC
  48. #endif
  49. //释放一个对象
  50. #define SAFE_DELETE(P) if(P) { [P release], P = nil; }
  51. #define SAFE_RELEASE(x) [x release];x=nil
  52. //----------------------内存相关----------------------------
  53.  
  54. //----------------------图片相关----------------------------
  55. //读取本地图片
  56. #define LOADIMAGE(file,ext) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:file ofType:ext]]
  57. //定义UIImage对象
  58. #define IMAGE(A) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:A ofType:nil]]
  59. //定义UIImage对象
  60. #define ImageNamed(_pointer) [UIImage imageNamed:_pointer]
  61. //可拉伸的图片
  62. #define ResizableImage(name,top,left,bottom,right) [[UIImage imageNamed:name] resizableImageWithCapInsets:UIEdgeInsetsMake(top,left,bottom,right)]
  63. #define ResizableImageWithMode(name,top,left,bottom,right,mode) [[UIImage imageNamed:name] resizableImageWithCapInsets:UIEdgeInsetsMake(top,left,bottom,right) resizingMode:mode]
  64. //建议使用前两种宏定义,性能高于后者
  65. //----------------------图片相关----------------------------
  66.  
  67. //----------------------颜色相关---------------------------
  68. // rgb颜色转换(16进制->10进制)
  69. #define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
  70. // 获取RGB颜色
  71. #define RGBA(r,g,b,a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]
  72. #define RGB(r,g,b) RGBA(r,g,b,1.0f)
  73. //背景色
  74. #define BACKGROUND_COLOR [UIColor colorWithRed:242.0/255.0 green:236.0/255.0 blue:231.0/255.0 alpha:1.0]
  75. //清除背景色
  76. #define CLEARCOLOR [UIColor clearColor]
  77. //----------------------颜色相关--------------------------
  78.  
  79. //----------------------其他----------------------------
  80. //方正黑体简体字体定义
  81. #define FONT(F) [UIFont fontWithName:@"FZHTJW--GB1-0" size:F]
  82. //file
  83. //读取文件的文本内容,默认编码为UTF-8
  84. #define FileString(name,ext) [[NSString alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:(name) ofType:(ext)] encoding:NSUTF8StringEncoding error:nil]
  85. #define FileDictionary(name,ext) [[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:(name) ofType:(ext)]]
  86. #define FileArray(name,ext) [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:(name) ofType:(ext)]]
  87. //G-C-D
  88. #define BACK(block) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block)
  89. #define MAIN(block) dispatch_async(dispatch_get_main_queue(),block)
  90. //Alert
  91. #define ALERT(msg) [[[UIAlertView alloc] initWithTitle:nil message:msg delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil] show]
  92.  
  93. //由角度获取弧度 有弧度获取角度
  94. #define degreesToRadian(x) (M_PI * (x) / 180.0)
  95. #define radianToDegrees(radian) (radian*180.0)/(M_PI)
  96. //----------------------其他-------------------------------
  97.  
  98. //----------------------视图相关----------------------------
  99. //设置需要粘贴的文字或图片
  100. #define PasteString(string) [[UIPasteboard generalPasteboard] setString:string];
  101. #define PasteImage(image) [[UIPasteboard generalPasteboard] setImage:image];
  102.  
  103. //得到视图的left top的X,Y坐标点
  104. #define VIEW_TX(view) (view.frame.origin.x)
  105. #define VIEW_TY(view) (view.frame.origin.y)
  106.  
  107. //得到视图的right bottom的X,Y坐标点
  108. #define VIEW_BX(view) (view.frame.origin.x + view.frame.size.width)
  109. #define VIEW_BY(view) (view.frame.origin.y + view.frame.size.height )
  110.  
  111. //得到视图的尺寸:宽度、高度
  112. #define VIEW_W(view) (view.frame.size.width)
  113. #define VIEW_H(view) (view.frame.size.height)
  114. //得到frame的X,Y坐标点
  115. #define FRAME_TX(frame) (frame.origin.x)
  116. #define FRAME_TY(frame) (frame.origin.y)
  117. //得到frame的宽度、高度
  118. #define FRAME_W(frame) (frame.size.width)
  119. #define FRAME_H(frame) (frame.size.height)
  120. //----------------------视图相关----------------------------
  121.  
  122. //---------------------打印日志--------------------------
  123. //Debug模式下打印日志,当前行,函数名
  124. #if DEBUG
  125. #define DLog(FORMAT, ...) fprintf(stderr,"\nfunction:%s line:%d content:%s\n", __FUNCTION__, __LINE__, [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
  126. #else
  127. #define NSLog(FORMAT, ...) nil
  128. #endif
  129. //Debug模式下打印日志,当前行,函数名 并弹出一个警告
  130. #ifdef DEBUG
  131. # define WDLog(fmt, ...) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"%s\n [Line %d] ", __PRETTY_FUNCTION__, __LINE__] message:[NSString stringWithFormat:fmt, ##__VA_ARGS__] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alert show]; }
  132. #else
  133. # define NSLog(...)
  134. #endif
  135. //打印Frame
  136. #define LogFrame(frame) NSLog(@"frame[X=%.1f,Y=%.1f,W=%.1f,H=%.1f",frame.origin.x,frame.origin.y,frame.size.width,frame.size.height)
  137. //打印Point
  138. #define LogPoint(point) NSLog(@"Point[X=%.1f,Y=%.1f]",point.x,point.y)
  139. //---------------------打印日志--------------------------
  140. #endif

iOS开发常见的宏定义(实用)的更多相关文章

  1. iOS开发经常使用宏定义

    iOS开发经常使用宏定义 iOS开发中经常须要获取屏幕宽度高度,为view设置颜色,为imgagView设置图片等,我们都可定义一些宏,随时都可拿来使用,方便开发 <span style=&qu ...

  2. iOS常用宏定义--实用

    在这里给大家分享一些常用的宏定义,喜欢的小伙伴可以直接在项目中使用(持续更新)!为了大家使用方便,请点击GitHub - 宏定义头文件下载 ! 1.获取屏幕宽度与高度 #define SCREEN_W ...

  3. iOS开发常见BUG和一些小技巧(ps:耐心看完,很实用)

    [385][scrollView不接受点击事件,是因为事件传递失败] // // MyScrollView.m // Created by beyond on 15/6/6. // Copyright ...

  4. IOS 程序员开发最常用宏定义

    网上对IOS的宏定义比较多,我总结了一些最常用的宏,后续还会继续补上. 1.首次启动判断: #define First_Launched @"firstLaunch" 2.ios7 ...

  5. ios开发之常用宏的定义

    有些时候,我们需要将代码简洁化,这样便于读代码.我们可以将一些不变的东东抽取出来,将变化的东西作为参数.定义为宏,这样在写的时候就简单多了. 下面例举了一些常用的宏定义和大家分享: 1. 判断设备的操 ...

  6. iOS 7:漫谈#define 宏定义(转)

    iOS :漫谈#define 宏定义 #define宏定义在C系开发中可以说占有举足轻重的作用.底层框架自不必说,为了编译优化和方便,以及跨平台能力,宏被大量使用,可以说底层开发离开define将寸步 ...

  7. ios开发-常见的项目文件介绍

    一.项目文件结构示意图 二.文件介绍 1.products文件夹:主要用于mac电脑开发的可执行文件,ios开发用不到这个文件 2.frameworks文件夹主要用来放依赖的框架 3.test文件夹是 ...

  8. iOS开发 常见错误

    一.NSAppTransportSecurity 错误提示:NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL ...

  9. iOS 7:漫谈#define 宏定义

           #define宏定义在C系开发中可以说占有举足轻重的作用.底层框架自不必说,为了编译优化和方便,以及跨平台能力,宏被大量使用,可以说底层开发离开define将寸步难行.而在更高层级进行开 ...

随机推荐

  1. jsoup获取文章内容

    jsoup爬取文章内容 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws Se ...

  2. 深入学习c++--多线程编程(二)【当线程间需要共享非const资源】

    1. 遇到的问题 #include <iostream> #include <thread> #include <chrono> #include <futu ...

  3. Redis的特性及运用

    Redis特性 一个产品的使用场景肯定是需要根据产品的特性,先列举一下Redis的特点: 读写性能优异 持久化 数据类型丰富 单线程 数据自动过期 发布订阅 分布式 这里我们通过几个场景,不同维度说下 ...

  4. npm 全局安装路径 在哪里

    注意:在Admin下,需要把隐藏文件显示出来,才能找到对应的文件路径.

  5. 使用jsPlumb插件实现动态连线功能

    这周去看了两天的羽毛球亚锦赛,工作有提前晚上加班做一些,但是技术文章却拉下了. 这段时间一直在寻找可以实现前端元素动态连线的功能,找了好几个库,考虑过用d3或者原生svg和canvas来实现,最后和同 ...

  6. xadmin自定义菜单、增加功能、富文本编辑器

    xadmin功能:https://www.cnblogs.com/derek1184405959/p/8682250.html#blogTitle7

  7. eNSP配置基本与高级访问控制列表

    首先我们进行基本的acl控制 拓扑图如下所示 首先我们对路由器进行基本ip配置 并在路由器上设置ospf协议 添加相邻的网段 在路由器上运行了ospf协议后 使用display ip route-ta ...

  8. JAVA 读取xml格式的数据

    <?xml version="1.0" encoding="UTF-8"?> <column-enums> <type name= ...

  9. Qt 编译出错“undefined reference to `vtable for”

    1. 有时,如果将某个类改为继承自QObject类(以前不继承自该类),编译时会出错. 解决: clean Project, run qmake, rebulid都运行一遍,好了! 因为qmake生成 ...

  10. [转帖]使用Nginx转发TCP/UDP数据

    使用Nginx转发TCP/UDP数据 https://www.cnblogs.com/guigujun/p/8075620.html 编译安装Nginx 从1.9.0开始,nginx就支持对TCP的转 ...