iOS常用宏定义总结

iOS开发者都知道,我们在开发过程中会经常用到一些比较常用宏定义,比如手机屏幕宽高呀、系统版本等等的,会在项目中的好多位置用到。下面呢是本人在开发过程中用到的一些宏定义的总结,也有一些是从别人那里学习过来的,希望帮助到想要统一使用宏定义的朋友。代码中可能会有一些错误或者异常以及不足之处,也希望大家多多评论补充或者直接联系我,在这里先谢过了,小子会及时更新上去。

usage 使用

  1. 直接将下面拷贝到pch文件或者项目总头文件中
  2. 到github下载文件,直接拖到项目中使用。

代码如下


/*** iOS常用宏定义小总结********************************************************************|***| 1. 重写DEBUG模式下的NSLog***| 2. APP屏幕大小相关***| 3. GCD -- 多线程   dispatch***| 4. catch缓存文件夹和Documents文件夹***| 5. 图片  UIImage***| 6. App Version / Info Plist  系统版本比较等***| 7. rgb颜色转换 UIColor***| 8. 字体 UIFont  只列举一种,其他的换名称自己可定义***| 9. 角度转弧度 弧度转角度***| 10. 单例 singleton***| 11. iOS系统内部常用宏定义**********************************************************************/
#ifdef __OBJC__   //保证 #ifdef 中的宏定义只会在 OC 的代码中被调用, 否则,一旦引入 C/C++ 的代码或者框架,就会出错///////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                               //
//  1. NSLog  重写NSLog,Debug模式下打印日志和当前行数  release模式下不打印
//                                                                                               //
///////////////////////////////////////////////////////////////////////////////////////////////////#ifdef  DEBUG
#define NSLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
#else
#define NSLog(...)
#endif///////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                               //
//  2. App 屏幕相关   Frame   Bounds
//                                                                                               //
/////////////////////////////////////////////////////////////////////////////////////////////////////NavBar高度
#ifndef kNavigationBar_HEIGHT
#define kNavigationBar_HEIGHT 44
#endif//获取屏幕 宽度、高度
#ifndef kScreenHeight
#define kScreenHeight      [[UIScreen mainScreen] bounds].size.height
#endif#ifndef kScreenWidth
#define kScreenWidth       [[UIScreen mainScreen] bounds].size.width
#endif//不同屏幕尺寸字体适配(375,667是因为目前苹果开发一般用IPHONE6做中间层 如果不是则根据实际情况修改)
#ifndef kScreenWidthRatio
#define kScreenWidthRatio  (kScreenWidth / 375.0)
#endif#ifndef kScreenHeightRatio
#define kScreenHeightRatio (kScreenHeight / 667.0)
#endif//根据传入的值算出乘以比例之后的值
#define AdaptedWidth(x)    ceilf((x) * kScreenWidthRatio)
#define AdaptedHeight(x)   ceilf((x) * kScreenHeightRatio)#ifndef MAIN_BOUNDS
#define MAIN_BOUNDS [[UIScreen mainScreen] bounds]
#endif//判断是那种设备
/*4  4s*/
#ifndef IPHONE4
#define IPHONE4  ([[UIScreen mainScreen] bounds].size.height==480)
#endif/*5  5s*/
#ifndef IPHONE5S
#define IPHONE5S ([[UIScreen mainScreen] bounds].size.height==568)
#endif/*6  6s 7*/
#ifndef IPHONE6
#define IPHONE6  ([[UIScreen mainScreen] bounds].size.height==667)
#endif/*6plus  6splus 7plus*/
#ifndef IPHONE6P
#define IPHONE6P ([[UIScreen mainScreen] bounds].size.height==736)
#endif// 是否iPad
#ifndef isPad
#define isPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#endif///////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                               //
//  3. GCD -- 多线程   dispatch
//                                                                                               //
///////////////////////////////////////////////////////////////////////////////////////////////////#define XH_GCD_MAIN(__BLOCK__)         dispatch_async(dispatch_get_main_queue(), __BLOCK__)#define XH_GCD_ASYNC(__BLOCK__)        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), __BLOCK__)#define XH_OPERATION_MAIN(__BLOCK__)   [[NSOperationQueue mainQueue] addOperationWithBlock:__BLOCK__]///////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                               //
//   4. catch缓存文件夹和Documents文件夹
//                                                                                               //
///////////////////////////////////////////////////////////////////////////////////////////////////#ifndef XH_USER_DEFAULT
#define XH_USER_DEFAULT [NSUserDefaults standardUserDefaults]
#endif#ifndef XH_CACHE_DIR
#define XH_CACHE_DIR      [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]
#endif#ifndef XH_DOCUMENTS_DIR
#define XH_DOCUMENTS_DIR  [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
#endif///////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                               //
//  5. App Version / Info Plist                                                                      //
//                                                                                               //
///////////////////////////////////////////////////////////////////////////////////////////////////
//设备(手机)版本号
#ifndef IOS_VERSION_S
#define IOS_VERSION_S ([[UIDevice currentDevice] systemVersion])              //str
#endif//设备(手机)版本号
#ifndef IOS_VERSION_F
#define IOS_VERSION_F ([[[UIDevice currentDevice] systemVersion] floatValue]) //float
#endif//系统类型   eg: iOS10.0.1
#ifndef XH_OSTYPE
#define XH_OSTYPE [NSString stringWithFormat:@"iOS%@", [[UIDevice currentDevice] systemVersion]]
#endif//app 版本号
#ifndef XH_BUNDLE_SHORT_VERSION_STRING
#define XH_BUNDLE_SHORT_VERSION_STRING [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]
#endif//build版本号
#ifndef XH_BUNDLE_VERSION_STRING
#define XH_BUNDLE_VERSION_STRING [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]
#endif//app 版本号 + build版本号
#ifndef XH_APP_VERSION_STRING
#define XH_APP_VERSION_STRING   [NSString stringWithFormat:@"%@ (%@)", XH_BUNDLE_SHORT_VERSION_STRING, XH_BUNDLE_VERSION_STRING]
#endif//bundleID
#ifndef XH_BUNDLE_IDENTIFIER
#define XH_BUNDLE_IDENTIFIER (NSString *)([[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleIdentifier"])
#endif//设备名
#ifndef XH_DEVICE_MACHINE_NAME
#define XH_DEVICE_MACHINE_NAME (NSString *)(NSStringMachineNameFromCurrentDevice())
#endif#ifndef XH_SCREEN_SCALE
#define XH_SCREEN_SCALE ([[UIScreen mainScreen] respondsToSelector:NSSelectorFromString(@"scale")] ? [[UIScreen mainScreen] scale] : 1.f)
#endif#ifndef XH_DEBUG_APP_CONFIG_STRING
#define XH_DEBUG_APP_CONFIG_STRING [NSString stringWithFormat:@"App Version: '%@' System Version: '%@' Machine Name '%@' Bundle Identifier: '%@'", XH_APP_VERSION_STRING, XH_DEVICE_SYSTEM_VERSION, XH_DEVICE_MACHINE_NAME, XH_BUNDLE_IDENTIFIER]
#endif//检查系统版本
//相同版本
#define SYSTEM_VERSION_EQUAL_TO(__VERSION__) ([[[UIDevice currentDevice] systemVersion] compare:__VERSION__ options:NSNumericSearch] == NSOrderedSame)//大于该系统版本
#define SYSTEM_VERSION_GREATER_THAN(__VERSION__) ([[[UIDevice currentDevice] systemVersion] compare:__VERSION__ options:NSNumericSearch] == NSOrderedDescending)//大于等于系统该版本
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(__VERSION__) ([[[UIDevice currentDevice] systemVersion] compare:__VERSION__ options:NSNumericSearch] != NSOrderedAscending)//小于该系统版本
#define SYSTEM_VERSION_LESS_THAN(__VERSION__) ([[[UIDevice currentDevice] systemVersion] compare:__VERSION__ options:NSNumericSearch] == NSOrderedAscending)//小于等于系统该版本
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(__VERSION__) ([[[UIDevice currentDevice] systemVersion] compare:__VERSION__ options:NSNumericSearch] != NSOrderedDescending)///////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                               //
//   6. 图片  UIImage                                                                                //
//                                                                                               //
/////////////////////////////////////////////////////////////////////////////////////////////////////读取本地图片 (文件名,后缀名)
#define XH_LOADIMAGE(__FILENAME__,__EXTENSION__) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:__FILENAME__ ofType:__EXTENSION__]]//定义UIImage对象 (文件名)
#define XH_IMAGE(__IMGNAME__) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:__IMGNAME__ ofType:nil]]///////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                               //
//   7. rgb颜色转换 UIColor                                                                            //
//                                                                                               //
///////////////////////////////////////////////////////////////////////////////////////////////////// rgb颜色转换(16进制->10进制)
#define XH_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]//透明背景色
#ifndef XH_CLEARCOLOR
#define XH_CLEARCOLOR [UIColor clearColor]
#endif#define XH_RGBACOLOR(r,g,b,a) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)]
#define XH_RGBCOLOR(r,g,b) RGBACOLOR(r,g,b,1.0)///////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                               //
//   8. 字体 UIFont                                                                                    //
//                                                                                               //
/////////////////////////////////////////////////////////////////////////////////////////////////////方正黑体简体字体定义
#define XH_FONT(__SIZE__) [UIFont fontWithName:@"FZHTJW--GB1-0" size:__SIZE__]///////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                               //
//   9. 角度转弧度 弧度转角度                                                                             //
//                                                                                               //
///////////////////////////////////////////////////////////////////////////////////////////////////
#define XH_ANGLE_TO_RADIAN(__ANGLE__) (M_PI * (__ANGLE__) / 180.0)#define XH_RADIAN_TO_ANGLE(__RADIAN__) (__RADIAN__ * 180.0) / (M_PI)
///////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                               //
//   10. 单例 singleton                                                                                 //
//                                                                                               //
///////////////////////////////////////////////////////////////////////////////////////////////////
// .h文件
#define singleton_interface(class) + (instancetype)shared##class;// .m文件
#define singleton_implementation(class) \
static class *_instance; \
\
+ (id)allocWithZone:(struct _NSZone *)zone \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [super allocWithZone:zone]; \
}); \
\
return _instance; \
} \
\
+ (instancetype)shared##class \
{ \
if (_instance == nil) { \
_instance = [[class alloc] init]; \
} \
\
return _instance; \
}
///////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                               //
// 11. 其他系统常用宏定义                                                                               //
//                                                                                               //
///////////////////////////////////////////////////////////////////////////////////////////////////
//判断是真机还是模拟器
#if TARGET_OS_IPHONE
//iPhone Device code
#endif#if TARGET_IPHONE_SIMULATOR
//iPhone Simulator code
#endif#endif

email:1286090267@qq.com(扣扣)

宏定义文件github地址,下载即可直接使用

iOS常用宏定义总结 --Objective-C相关推荐

  1. iOS - 常用宏定义

    iOS中的常用宏定义 此篇博客为博主转载经典文章,非常感谢原创的优秀资源! 为方便常看和使用,在此我将原文中的宏定义在此以代码片形式展示出来,具体内容如下: #ifndef MacroDefiniti ...

  2. iOS - 常用宏定义和PCH文件知识点整理

    (一)PCH文件操作步骤演示: 第一步:图文所示: 第二步:图文所示: (二)常用宏定义整理: (1)常用Log日志宏(输出日志详细可定位某个类.某个函数.某一行) //=============== ...

  3. iOS常用宏定义大全

    宏定义与常量的区别 宏:只是在预处理器里进行文本替换,不做任何类型检查,宏能定义代码,const不能,多个宏编译时间相对较长,影响开发效率,调试过慢,const只会编译一次,缩短编译时间. 所以在使用 ...

  4. (转)iOS 常用宏定义

    #ifndef MacroDefinition_h #define MacroDefinition_h //-------------------获取设备大小--------------------- ...

  5. c 语言常用宏定义 模板

    常用宏定义 1.防止一个头文件被重复包含 #ifndef COMDEF_H #define COMDEF_H //头文件内容 #endif 2.重新定义一些类型,防止由于各种平台和编译器的不同,而产生 ...

  6. C语言宏定义(常用宏定义)

    C语言常用宏定义 常用宏定义 数值相关的宏定义 字符相关的宏定义 byte相关的宏定义 bit相关的宏定义 数组与结构体相关的宏定义 对齐的宏定义 常用宏定义 数值相关的宏定义 闰年的判断 ,年份可以 ...

  7. iOS日常工作之常用宏定义大全

    前言: 在工作中, 很多小伙伴都会在PCH文件定义一些常用的宏,但是又怕写这些简单的宏浪费时间,又有时候忘记怎么定义了怎么办?本人在工作中也是如此.所以在这里给大家分享一些常用的宏定义,喜欢的小伙伴可 ...

  8. C/C++常用宏定义,注意事项,宏中#和##的用法

    总结下宏和函数的不同之处,以供大家写代码时使用,这段总结摘自<C和指针>一书. 当然宏定义非常重要的,它可以帮助我们防止出错,提高代码的可移植性和可读性等. 下面列举一些成熟软件中常用得宏 ...

  9. iOS 通用宏定义 高效全局宏汇总

    最近在搭建新项目,为了方便开发,常会用到一些宏定义,梳理了之前项目中用到,又查漏补缺挑选了一些网络上比较不错的,总结了一份分享给大家. ////define.h//MiAiApp////Created ...

  10. VC宏定义 及常用宏定义说明

    1. 宏定义的格式 宏定义的一般格式是: #define  标识符  字符串 其中,标识符和字符串之间用空格隔开.标识符又称宏名,为了区别于一般变量,通常用英文大写字母表示:字符串又称宏体,可以是常 ...

最新文章

  1. 比特币如何实现—《区块链历史链条》2
  2. 22.2. 表管理(Table)
  3. mass Framework pagination插件
  4. CTFshow 文件包含 web81
  5. 移植madplay到jz2440【学习笔记】
  6. jsonp跨域请求响应结果处理函数(python)
  7. 4.openSession() 、 getCurrentSession()与session上下文
  8. Cacti监控mysql数据库server实现过程
  9. 分布式 和 集群的区别
  10. 嵌入式系统中断实验c语言,中断的实验现象
  11. linux Shell学习笔记第二天
  12. AD09画pcb板时遇到的问题
  13. 商业需求分析(BRD)模板
  14. 前端面试题(JavaScript基础篇)
  15. 微信小程序 下拉刷新 性能优化 参考饿了么小程序首页列表加载
  16. uniapp——ios端和android端微信分享,通过打开appStore和应用宝商店下载
  17. 《权力的游戏》Python探索性分析
  18. v18.02 鸿蒙内核源码分析(源码结构) | 内核文件各自含义 | 百篇博客分析HarmonyOS源码
  19. Nvidia TX2+rplidar+autolabor pro1实现自主导航机器人
  20. 编程找出四位整数abcd 中满足下述关系的数:(ab+cd)(ab+cd)=abcd

热门文章

  1. MFC开发wps演示
  2. Ruby中对应PHP的hex2bin和bin2hex方法
  3. fortran和python的效率_Fortran数值计算真的比C快吗,经测试是一样快的,是不是很多人都在以讹传讹?...
  4. Python图书馆管理系统
  5. [diy-windows系统] Windows下dism 集成系统补丁、驱动
  6. GNS3 中使用 Docker 容器
  7. 《浪潮之巅》笔记之七
  8. 《JAVA程序设计教程(第7版)英文版》pdf 附下载链接
  9. STM32F1开发指南笔记46----字库原理及汉字库创建
  10. csv用excel打开后乱码的解决方法