前言

今天将一些简化工程代码的宏定义拿出来分享一下,自定义一些宏可以有效的简化代码,提高编码效率。

Application

  1. #define APPLICATION [UIApplication sharedApplication]
  2. #define APPDLE (AppDelegate*)[APPLICATION delegate]

布局相关
在纯代码的工程中,由于oc语法本身并不是很简洁的特性,控件布局会占有很大篇幅的代码,如果将一些位置、大小、中心、间隙等写成简短明了的宏定义,就会大大增加代码的可读性。

  • Frame相关

传参frame对象,获取frame的相关属性值

  1. #define FRAME_ORIGIN(aFrame) ((aFrame).origin)
  2. #define FRAME_X(aFrame) ((aFrame).origin.x)
  3. #define FRAME_Y(aFrame) ((aFrame).origin.y)
  4. #define FRAME_SIZE(aFrame) ((aFrame).size)
  5. #define FRAME_HEIGHT(aFrame) ((aFrame).size.height)
  6. #define FRAME_WIDTH(aFrame) ((aFrame).size.width)
  7. /*修改frame对象的x、y、width、height属性值*/
  8. #define FRAME_CHANGE_X(aFrame,x) CGRectMake(x, (aFrame).origin.y, (aFrame).size.width, (aFrame).size.height)
  9. #define FRAME_CHANGE_Y(aFrame,y) CGRectMake((aFrame).origin.x, y, (aFrame).size.width, (aFrame).size.height)
  10. #define FRAME_CHANGE_WIDTH(aFrame,w) CGRectMake((aFrame).origin.x, (aFrame).origin.y, w, (aFrame).size.height)
  11. #define FRAME_CHANGE_HEIGHT(aFrame,h) CGRectMake((aFrame).origin.x, (aFrame).origin.y, (aFrame).size.width, h)
  12. // 向左移动offset位移后得到frame对象
  13. #define FRAME_MOVE_Left(aFrame,offset) CGRectMake((aFrame).origin.x-(offset), (aFrame).origin.y, (aFrame).size.width, (aFrame).size.height)
  14. // 向右移动offset位移后得到frame对象
  15. #define FRAME_MOVE_Right(aFrame,offset) CGRectMake((aFrame).origin.x+(offset), (aFrame).origin.y, (aFrame).size.width, (aFrame).size.height)
  16. // 向上移动offset位移后得到frame对象
  17. #define FRAME_MOVE_Up(aFrame,offset) CGRectMake((aFrame).origin.x, (aFrame).origin.y-(offset), (aFrame).size.width, (aFrame).size.height)
  18. // 向下移动offset位移后得到frame对象
  19. #define FRAME_MOVE_Down(aFrame,offset) CGRectMake((aFrame).origin.x, (aFrame).origin.y+(offset), (aFrame).size.width, (aFrame).size.height)

传参view对象,获取view的frame、bounds相关属性值

  1. #define VIEW_BOUNDS(aView) ((aView).bounds)
  2. #define VIEW_FRAME(aView) ((aView).frame)
  3. #define VIEW_ORIGIN(aView) ((aView).frame.origin)
  4. #define VIEW_X(aView) ((aView).frame.origin.x)
  5. #define VIEW_Y(aView) ((aView).frame.origin.y)
  6. #define VIEW_SIZE(aView) ((aView).frame.size)
  7. #define VIEW_HEIGHT(aView) ((aView).frame.size.height) // 视图高度
  8. #define VIEW_WIDTH(aView) ((aView).frame.size.width) // 视图宽度

其实本来以下两个宏按照语义想要写成VIEW_Right_X(aView)、VIEW_Bottom_Y(aView),但是由于XCode的自动提示,写成以下形式就会在自动提示列表中与VIEW_X(aView) 、VIEW_Y(aView) 两个宏并列,更方便选择

  1. #define VIEW_X_Right(aView) ((aView).frame.origin.x + (aView).frame.size.width) // 视图右边界x坐标
  2. #define VIEW_Y_Bottom(aView) ((aView).frame.origin.y + (aView).frame.size.height) // 视图底部y坐标
  • Center相关
  1. #define VIEW_CENTER(aView) ((aView).center)
  2. #define VIEW_CENTER_X(aView) ((aView).center.x)
  3. #define VIEW_CENTER_Y(aView) ((aView).center.y)
  • 间距
    有人会说定义这样一个宏并不是简化代码反而增加了代码,确实如此。我们在布局控件的时候经常会涉及+、-一些间距值,当我们回过头阅读那成百上千行代码中+、-的那些值的时候,我们就很难去猜测它们的意义。如果用这样一个宏去表示,就大大增加了代码的可读性。
  1. #define Space_(space) (space) // 表示整形、浮点型间距

屏幕坐标、尺寸相关

  1. // 状态栏占用高度
  2. #define StateBarHeight 20.f
  3. // 状态栏底部y坐标
  4. #define OffsetStateBarHeight ((DEVICE_OS_VERSION_VALUE >= 7.0)? StateBarHeight : 0.f)
  5. // 顶部状态栏占用高度
  6. #define TopNavBarHeight 40.f
  7. // 顶部导航栏底部y坐标
  8. #define OffsetTopNavBarHeight (OffsetStateBarHeight + TopNavBarHeight)
  9. // 底部导航栏占用高度
  10. #define BottomTabBarHeight 40.f
  11. // 屏幕高度
  12. #define ScreenHeight [[UIScreen mainScreen] bounds].size.height
  13. // 屏幕宽度
  14. #define ScreenWidth [[UIScreen mainScreen] bounds].size.width
  15. // 屏幕可操作高度
  16. #define MainHeight ((DEVICE_OS_VERSION_VALUE >= 7.0)? ScreenHeight : (ScreenHeight - StateBarHeight))
  17. // 屏幕可操作宽度
  18. #define MainWidth ScreenWidth
  19. // 去除上下导航栏剩余中间视图高度
  20. #define ContentHeight (MainHeight -OffsetTopNavBarHeight -BottomTabBarHeight)

设备系统相关

  • 设备
    这只列出了目前已有的iPhone屏幕尺寸
  1. // 6P、6sP
  2. #define IS_iPhone6Plus ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2208), [[UIScreen mainScreen] currentMode].size) : NO)
  3. // 6、6s
  4. #define IS_iPhone6 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(750, 1334), [[UIScreen mainScreen] currentMode].size) : NO)
  5. // 5、5s
  6. #define IS_iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
  7. // 3g、4、4s
  8. #define IS_iPhone4 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO)
  1. // 是否是Retina屏幕
  2. #define IS_Retina ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? [[UIScreen mainScreen] currentMode].size.width > 320 : NO)
  • 系统相关
  1. // app版本号
  2. #define DEVICE_APP_VERSION (NSString *)[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]
  3. // app Build版本号
  4. #define DEVICE_APP_BUILD (NSString *)[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]
  5. // 系统版本号(string)
  6. #define DEVICE_OS_VERSION [[UIDevice currentDevice] systemVersion]
  7. // 系统版本号(float)
  8. #define DEVICE_OS_VERSION_VALUE [DEVICE_OS_VERSION floatValue]

字体相关

  • 字体
  1. #define FONT_SIZE(f) [UIFont systemFontOfSize:(f)]
  2. #define FONT_BOLD_SIZE(f) [UIFont boldSystemFontOfSize:(f)]
  3. #define FONT_ITALIC_SIZE(f) [UIFont italicSystemFontOfSize:(f)]
  • 大小屏字体自动切换
    有的应用希望有一个好的用户体验会在不同的屏幕上适配不同大小字体,这时就可以使用以下的宏定义来实现。但是如果应用中字体大小不能做到全局统一,就不要使用以下的宏定义来实现字体大小适配。这个就看你所开发的具体情况了
  1. #define IS_SmallScreen (IS_iPhone5 || IS_iPhone4)
  2. #define MaxFontSize (IS_SmallScreen ? 21.f : 25.f )
  3. #define LagerFontSize (IS_SmallScreen ? 17.f : 19.f )
  4. #define BigFontSize (IS_SmallScreen ? 15.f : 17.f )
  5. #define NormalFontSize (IS_SmallScreen ? 13.f : 15.f )
  6. #define SmallFontSize (IS_SmallScreen ? 11.f : 13.f )
  7. #define MinFontSize (IS_SmallScreen ? 9.f : 11.f )

颜色相关

  • 系统颜色
  1. #define COLOR_Clear [UIColor clearColor]
  2. #define COLOR_White [UIColor whiteColor]
  3. #define COLOR_Black [UIColor blackColor]
  4. #define COLOR_Red [UIColor redColor]
  5. #define COLOR_DarkGray [UIColor darkGrayColor]
  6. #define COLOR_LightGray [UIColor lightGrayColor]
  7. #define COLOR_GrayColor [UIColor grayColor]
  8. #define COLOR_Green [UIColor greenColor]
  9. #define COLOR_BlueColor [UIColor blueColor]
  10. #define COLOR_Cyan [UIColor cyanColor]
  11. #define COLOR_Yellow [UIColor yellowColor]
  12. #define COLOR_Magenta [UIColor magentaColor]
  13. #define COLOR_Orange [UIColor orangeColor]
  14. #define COLOR_Purple [UIColor purpleColor]
  15. #define COLOR_Brown [UIColor brownColor]
  • 颜色转换
  1. #define RGBCOLOR(r,g,b) [UIColor colorWithRed:(r)/255.f green:(g)/255.f blue:(b)/255.f alpha:1.f]
  2. #define RGBACOLOR(r,g,b,a) [UIColor colorWithRed:(r)/255.f green:(g)/255.f blue:(b)/255.f alpha:(a)]
  3. #define HexCOLOR(HexStr) [UIColor colorWithHexString:HexStr]

字符串相关

  1. #define StrOfInterger(interger) [NSString stringWithFormat:@"%ld",(long)(interger)]
  2. #define StrOfFloat(float) [NSString stringWithFormat:@"%f",(float)]

Image相关

  1. #define IMG_Name(imgName) [UIImage imageNamed:(imgName)]
  2. #define IMG_ImgWidth(img) ((img).size.width)
  3. #define IMG_ImgHeight(img) ((img).size.height)

校验相关

  1. #define STRINGHASVALUE(str) (str && [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]].length > 0)
  2. #define IsCanUseString(str) ((str != nil) && ![str isKindOfClass:[NSNull class]] && [str isKindOfClass:[NSString class]] && [str length] > 0 )
  3. #define IsCanUseArray(arr) ( arr && (arr != nil) && ![arr isKindOfClass:[NSNull class]] )
  4. #define IsCanUseDic(dic) ( dic && (dic != nil) && ![dic isKindOfClass:[NSNull class]] )
  5. #define IsCanUseObj(obj) ( obj && (obj != nil) && ![obj isKindOfClass:[NSNull class]] )
  6. #define IsNullClass(class) [class isKindOfClass:[NSNull class]]

打印相关
mark(NSString)为打印内容标题

  1. #define NSLOG_Str(mark,str) NSLog(@"##%@##--str:%@--",(mark),(str))
  2. #define NSLOG_Int(mark,int) NSLog(@"##%@##--int:%ld--",(mark),(int))
  3. #define NSLOG_Float(mark,float) NSLog(@"##%@##--float:%f--",(mark),(float))
  4. #define NSLOG_Bool(mark,bool) NSLog(@"##%@##--bool:%@--",(mark),(bool) ? @"YES" : @"NO")
  5. #define NSLOG_Point(mark,point) NSLog(@"##%@##--x:%f--y:%f--",(mark),(point).x,(point).y)
  6. #define NSLOG_Size(mark,size) NSLog(@"##%@##--width:%f--height:%f--",(mark),(size).width,(size).height)
  7. #define NSLOG_Frame(mark,frame) NSLog(@"##%@##--x:%f--y:%f--width:%f--height:%f--",(mark),(frame).origin.x,(frame).origin.y,(frame).size.width,(frame).size.height)
  1. #define NSLog(FORMAT, ...) fprintf(stderr,"[%s:%d行] %s\n",[[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String], __LINE__, [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
  1. #else
  1. #define NSLog(FORMAT, ...) nil
  1. #endif


接口相关

  1. // DEBUG 模式为程序员调试模式, Release是发布模式是面向客户的,因为在Release模式下程序比DEBUG模式更优化,运行更快
  1. #if DEBUG
  1. #define NN_URL(url) [@"http://api.niuniuhaoguanjia.net/" stringByAppendingString:url]
  1. #define HGJ_URL(url) [@"http://api.58haoguanjia.net/" stringByAppendingString:url]
  1. #else
  1. #define NN_URL(url) [@"http://api.niuniuhaoguanjia.net/" stringByAppendingString:url]
  1. #define HGJ_URL(url) [@"http://api.58haoguanjia.net/" stringByAppendingString:url]
  1. #endif
  1.  

iOS开发中常用的宏的更多相关文章

  1. 第三十篇、iOS开发中常用的宏

    //字符串是否为空 #define kStringIsEmpty(str) ([str isKindOfClass:[NSNull class]] || str == nil || [str leng ...

  2. iOS开发中常用的数学函数

    iOS开发中常用的数学函数 /*---- 常用数学公式 ----*/ //指数运算 3^2 3^3 NSLog(,)); //result 9 NSLog(,)); //result 27 //开平方 ...

  3. iOS开发中常用的分类方法---UIImage+Category

    在开发中使用分类对原有的系统类进行方法扩展,是增强系统原有类功能的常见做法. /** * 自由拉伸一张图片 * * @param name 图片名字 * @param left 左边开始位置比例 值范 ...

  4. iOS开发中常用的手势---边缘手势

    说明:以下方法是开发中使用的方法,有什么不对的或者好的方法,请多多指教! 此处的边缘手势是用来控制左侧抽屉视图的弹出以及收回. 添加手势 : 页面上有多个手势时需要遵循 UIGestureRecogn ...

  5. iOS开发中常用到的宏定义

    1.首次启动判断: #define First_Launched @"firstLaunch" 2.ios7系统判断: #define IsIOS7 ([[[UIDevice cu ...

  6. iOS开发中 常用枚举和常用的一些运算符(易错总结)

    1.色值的随机值: #define kColorValue arc4random_uniform(256)/255.0 // arc4random_uniform(256)/255.0; 求出0.0~ ...

  7. ios开发中常用的也是最基本的mysql语句

    MySQL常用基本SQL语句小结——(转) sql语言不经常用,每次再用都隔好久的时间,以致最基本的都想不起来了,只好转一篇记着= - 找的时候方便 SQL分类:  DDL—数据定义语言(CREATE ...

  8. iOS开发中常用的设计模式

    常用的设计模式(一)代理模式应用场景:当一个类的某些功能需要由别的类来实现,但是又不确定具体会是哪个类实现.优势:解耦合敏捷原则:开放-封闭原则实例:tableview的 数据源delegate,通过 ...

  9. iOS开发中常用的单例

    定义:一个类的对象,无论在何时创建.无论创建多少次,创建出来的对象都是同一个对象. 使用场景:当有一些数据需要共享给别的类的时候,就可以把这些数据保存在单例对象中.   关键代码: + (instan ...

随机推荐

  1. Android中SharedPreferences使用方法介绍

    一.Android SharedPreferences的简介 SharedPreferences是一种轻型的Android数据存储方式,它的本质是基于XML文件存储key-value键值对数据,通常用 ...

  2. Mysql分页查询

    取前5条数据 select * from table_name limit 0,5 或 select * from table_name limit 5 取第11条到第15条数据,共5条 select ...

  3. pygame安装

    进入这个网站 http://www.pygame.org/wiki/Compilation 可以选择不同系统的安装方法 其中ubuntu的安装命令是 #这是python .X #install dep ...

  4. 【模拟】Codeforces 710A King Moves

    题目链接: http://codeforces.com/problemset/problem/710/A 题目大意: 国际象棋标准8X8棋盘,国王能往周围8个方向走.输入国王的位置,输出当前国王能往几 ...

  5. bzoj 1560 [JSOI2009]火星藏宝图(DP)

    1560: [JSOI2009]火星藏宝图 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 647  Solved: 309[Submit][Status ...

  6. [Theano] Theano初探

    1. Theano用来干嘛的? Theano was written at the LISA lab to support rapid development of efficient machine ...

  7. UVALive 7148 LRIP 14年上海区域赛K题 树分治

    题意 n个点组成一棵树, 带有点权. 求最长不降的路径的长度, 且路径上最大值最小值之差不超过D. 显然是树分治, 但是分治之后如何维护答案呢. 假设当前重心为g, 分别记录g出发不降路径的长度,以及 ...

  8. Heavy Transportation

    题目大意: 雨果的沉重运输是快乐的,当浮空运输出现故障时候他可以扩展业务, 但他需要一个聪明的人告诉他是否真的是一种把他的客户构建了巨型钢起重机的地方需要的所有街道都可以承受重量(这句是直接有道翻译的 ...

  9. PostgreSQL和GreenPlum数据库的区别

    PostgreSQL   PostgreSQL是以加州大学伯克利分校计算机系开发的 POSTGRES,现在已经更名为POSTGRES,版本 4.2为基础的对象关系型数据库管理系统(ORDBMS).Po ...

  10. c# mongo 返回指定的列

    var query= db.GetCollection<Merchant>("merchant").Find(Query<Merchant>.EQ(m =& ...