在我们日常的项目中,合理的使用宏定义,会大大减少我们的代码量,以及代码的可读性,为方便读者使用,总结如下:

pragma mark - Application相关

  1. ///=============================================================================
  2. /// @name Application
  3. ///=============================================================================
  4. #define APPLICATION [UIApplication sharedApplication]
  5. #define APPDLE (AppDelegate*)[APPLICATION delegate]
  6. #define kKeyWindow [UIApplication sharedApplication].keyWindow
  7. #define kUserDefaults [NSUserDefaults standardUserDefaults]
  8. #define kNotificationCenter [NSNotificationCenter defaultCenter]
  9. //获取temp
  10. #define kPathTemp NSTemporaryDirectory()
  11. //获取沙盒 Document
  12. #define kPathDocument [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
  13. //获取沙盒 Cache
  14. #define kPathCache [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]
  15. ///=============================================================================

pragma mark - Frame相关

  1. ///=============================================================================
  2. /// @name Frame相关
  3. ///=============================================================================
  4. // 控件尺寸比例
  5. #define kScreenRate (kScreenWidth/375.f)
  6. // 实际尺寸
  7. #define kSuitSize(size) kScreenRate * (size)
  8. ///=============================================================================

pragma mark - 屏幕坐标、尺寸相关

  1. ///=============================================================================
  2. /// @name 屏幕坐标、尺寸相关
  3. ///=============================================================================
  4. //判断是否iPhone X
  5. #define IS_iPhoneX ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) : NO)
  6. // 屏幕高度
  7. #define kScreenHeight [[UIScreen mainScreen] bounds].size.height
  8. // 屏幕宽度
  9. #define kScreenWidth [[UIScreen mainScreen] bounds].size.width
  10. // 状态栏高度
  11. #define kStatusBarHeight (IS_iPhoneX ? 44.f : 20.f)
  12. // 顶部导航栏高度
  13. #define kNavigationBarHeight 44.f
  14. // 顶部安全距离
  15. #define kSafeAreaTopHeight (IS_iPhoneX ? 88.f : 64.f)
  16. // 底部安全距离
  17. #define kSafeAreaBottomHeight (IS_iPhoneX ? 34.f : 0.f)
  18. // Tabbar高度
  19. #define kTabbarHeight 49.f
  20. // 去除上下导航栏剩余中间视图高度
  21. #define ContentHeight (kScreenHeight - kSafeAreaTopHeight - kSafeAreaBottomHeight - kTabbarHeight)
  22. ///=============================================================================

pragma mark - 系统相关

  1. ///=============================================================================
  2. /// @name 系统相关
  3. ///=============================================================================
  4. // app版本号
  5. #define DEVICE_APP_VERSION (NSString *)[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]
  6. // app Build版本号
  7. #define DEVICE_APP_BUILD (NSString *)[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]
  8. // 系统版本号(string)
  9. #define DEVICE_OS_VERSION [[UIDevice currentDevice] systemVersion]
  10. // 系统版本号(float)
  11. #define DEVICE_OS_VERSION_VALUE [DEVICE_OS_VERSION floatValue]
  12. ///=============================================================================

pragma mark - 字体、颜色相关

  1. ///=============================================================================
  2. /// @name 字体、颜色相关
  3. ///=============================================================================
  4. #define FONT_SIZE(f) [UIFont systemFontOfSize:(f)]
  5. #define FONT_BOLD_SIZE(f) [UIFont boldSystemFontOfSize:(f)]
  6. #define FONT_ITALIC_SIZE(f) [UIFont italicSystemFontOfSize:(f)]
  7. #define RGBCOLOR(r,g,b) [UIColor colorWithRed:(r)/255.f green:(g)/255.f blue:(b)/255.f alpha:1.f]
  8. #define RGBACOLOR(r,g,b,a) [UIColor colorWithRed:(r)/255.f green:(g)/255.f blue:(b)/255.f alpha:(a)]
  9. #define RandomColor [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0]
  10. #define ColorWithHex(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]
  11. ///=============================================================================

pragma mark - NSLog相关

  1. ///=============================================================================
  2. /// @name NSLog相关
  3. ///=============================================================================
  4. #ifdef DEBUG
  5. #define MLLog(...) NSLog(@"%s 第%d行 \n %@\n\n",__func__,__LINE__,[NSString stringWithFormat:__VA_ARGS__])
  6. #else
  7. #define NSLog(...)
  8. #endif
  9. ///=============================================================================

pragma mark - 判断数据是否为空

  1. ///=============================================================================
  2. /// @name 判断数据是否为空
  3. ///=============================================================================
  4. #define kStringIsEmpty(str) ([str isKindOfClass:[NSNull class]] || str == nil || [str length] < 1 ? YES : NO )
  5. #define kArrayIsEmpty(array) (array == nil || [array isKindOfClass:[NSNull class]] || array.count == 0)
  6. #define kDictIsEmpty(dic) (dic == nil || [dic isKindOfClass:[NSNull class]] || dic.allKeys == 0)
  7. ///=============================================================================

iOS----------常见宏定义的更多相关文章

  1. iOS 使用宏定义函数和代码块

    iOS使用宏定义函数和代码块 今天在开发过程中碰到一个问题:就是父类中要向外发送通知,然后子类中或者其他类中来接收它.当然一般是把它写到类方法中去,但是有个问题,就是如果调用的类不是它的子类,就不能直 ...

  2. iOS常用宏 定义

    总结了iOS开发过程中的一些常用宏,以后会陆陆续续添加进来. 字符串是否为空 1   #define kStringIsEmpty(str) ([str isKindOfClass:[NSNull c ...

  3. iOS重用宏定义

    iOS 多快好省的宏(转) 原文地址:http://my.oschina.net/yongbin45/blog/150149 // 字符串: #ifndef nilToEmpty #define ni ...

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

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

  5. iOS常用宏定义大全

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

  6. iOS之宏定义#define

    最基本的宏定义用法 #define aaa bbb 表示用aaa替换bbb的内容. 宏作用范围 宏的作用范围是在当前文件内, 如果需要作用于其他类(如在类b调用类a已定义宏),那么需要在类b引入类a的 ...

  7. iOS常用宏定义

    转发:https://www.douban.com/note/486674206/ #ifndef MacroDefinition_h#define MacroDefinition_h //----- ...

  8. 【编程基础】C语言常见宏定义

    我们在使用C语言编写程序的时候,常常会使用到宏定义以及宏编译指令,有的可能比较常用,有的可能并不是很常用,是不是所有的C语言宏定义以及宏指令你都清楚呢? 指令 用途详细介绍 # 空指令,无任何效果 # ...

  9. iOS define 宏定义 和 const定义常量区别

    const   const 是c++中的修饰符.  c++中常用来定义常量,修饰左值. #define 宏定义语句, 在预处理阶段直接做文本替换,不做类型检查. 它们之间的最大区别: 1.  对于co ...

  10. (转)iOS 常用宏定义

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

随机推荐

  1. Gem::LoadError: Specified 'sqlite3' for database adapter, but the gem is not loaded

    解决办法: 指定sqlite3的版本为1.3.13: gem 'sqlite3', '~> 1.3.13' 然后运行bundle update

  2. Redis结合Lua脚本实现高并发原子性操作

    从 2.6版本 起, Redis 开始支持 Lua 脚本 让开发者自己扩展 Redis … 案例-实现访问频率限制: 实现访问者 $ip 在一定的时间 $time 内只能访问 $limit 次. 非脚 ...

  3. [Swift]LeetCode729. 我的日程安排表 I | My Calendar I

    Implement a MyCalendar class to store your events. A new event can be added if adding the event will ...

  4. [Swift]LeetCode890. 查找和替换模式 | Find and Replace Pattern

    You have a list of words and a pattern, and you want to know which words in words matches the patter ...

  5. js 里面的键盘事件对应的键码

    js 里面的键盘事件经常用到,所以收集了键盘事件对应的键码来分享下:keyCode 8 = BackSpace BackSpacekeyCode 9 = Tab TabkeyCode 12 = Cle ...

  6. VirtualBox虚拟机克隆迁移步骤

    VirtualBox是常用的虚拟机管理软件,和VMware一样,用的很多.在使用过程中,有的时候需要对虚拟机进行迁移.比如我们原来的服务器,使用的win10操作系统,上面利用VirtualBox安装了 ...

  7. 【机器学习】--线性回归中soft-max从初始到应用

    一.前述 Soft-Max是做多分类的,本身是哪个类别的概率大,结果就为对应的类别.为什么称之为Soft判别,原因是归一化之后的概率选择最大的作为结果,而不是只根据分子. 二.原理 sigmod函数: ...

  8. 机器学习 GBDT+xgboost 决策树提升

    目录 xgboost CART(Classify and Regression Tree) GBDT(Gradient Boosting Desicion Tree) GB思想(Gradient Bo ...

  9. 10.Django ModelForm

    ModelForm 1.ModeForm简单验证 from django.db import models # Create your models here. class UserInfo(mode ...

  10. 如何判断DataSet里有多少个DataTable

    dataset.table.count sda.fill(ds,"table"); //这里是在ds里新建了一个表,叫table,注意是新建,多次执行会报错,实际使用时,可以用co ...