前言

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

Application

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

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

  • Frame相关

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

#define FRAME_ORIGIN(aFrame)              ((aFrame).origin)
#define FRAME_X(aFrame) ((aFrame).origin.x)
#define FRAME_Y(aFrame) ((aFrame).origin.y) #define FRAME_SIZE(aFrame) ((aFrame).size)
#define FRAME_HEIGHT(aFrame) ((aFrame).size.height)
#define FRAME_WIDTH(aFrame) ((aFrame).size.width) /*修改frame对象的x、y、width、height属性值*/
#define FRAME_CHANGE_X(aFrame,x) CGRectMake(x, (aFrame).origin.y, (aFrame).size.width, (aFrame).size.height)
#define FRAME_CHANGE_Y(aFrame,y) CGRectMake((aFrame).origin.x, y, (aFrame).size.width, (aFrame).size.height)
#define FRAME_CHANGE_WIDTH(aFrame,w) CGRectMake((aFrame).origin.x, (aFrame).origin.y, w, (aFrame).size.height)
#define FRAME_CHANGE_HEIGHT(aFrame,h) CGRectMake((aFrame).origin.x, (aFrame).origin.y, (aFrame).size.width, h) // 向左移动offset位移后得到frame对象
#define FRAME_MOVE_Left(aFrame,offset) CGRectMake((aFrame).origin.x-(offset), (aFrame).origin.y, (aFrame).size.width, (aFrame).size.height)
// 向右移动offset位移后得到frame对象
#define FRAME_MOVE_Right(aFrame,offset) CGRectMake((aFrame).origin.x+(offset), (aFrame).origin.y, (aFrame).size.width, (aFrame).size.height)
// 向上移动offset位移后得到frame对象
#define FRAME_MOVE_Up(aFrame,offset) CGRectMake((aFrame).origin.x, (aFrame).origin.y-(offset), (aFrame).size.width, (aFrame).size.height)
// 向下移动offset位移后得到frame对象
#define FRAME_MOVE_Down(aFrame,offset) CGRectMake((aFrame).origin.x, (aFrame).origin.y+(offset), (aFrame).size.width, (aFrame).size.height)

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

#define VIEW_BOUNDS(aView)       ((aView).bounds)

#define VIEW_FRAME(aView)        ((aView).frame)

#define VIEW_ORIGIN(aView)       ((aView).frame.origin)
#define VIEW_X(aView) ((aView).frame.origin.x)
#define VIEW_Y(aView) ((aView).frame.origin.y) #define VIEW_SIZE(aView) ((aView).frame.size)
#define VIEW_HEIGHT(aView) ((aView).frame.size.height) // 视图高度
#define VIEW_WIDTH(aView) ((aView).frame.size.width) // 视图宽度

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

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

屏幕坐标、尺寸相关

// 状态栏占用高度
#define StateBarHeight 20.f
// 状态栏底部y坐标
#define OffsetStateBarHeight ((DEVICE_OS_VERSION_VALUE >= 7.0)? StateBarHeight : 0.f) // 顶部状态栏占用高度
#define TopNavBarHeight 40.f
// 顶部导航栏底部y坐标
#define OffsetTopNavBarHeight (OffsetStateBarHeight + TopNavBarHeight) // 底部导航栏占用高度
#define BottomTabBarHeight 40.f // 屏幕高度
#define ScreenHeight [[UIScreen mainScreen] bounds].size.height
// 屏幕宽度
#define ScreenWidth [[UIScreen mainScreen] bounds].size.width // 屏幕可操作高度
#define MainHeight ((DEVICE_OS_VERSION_VALUE >= 7.0)? ScreenHeight : (ScreenHeight - StateBarHeight))
// 屏幕可操作宽度
#define MainWidth ScreenWidth // 去除上下导航栏剩余中间视图高度
#define ContentHeight (MainHeight -OffsetTopNavBarHeight -BottomTabBarHeight)

设备系统相关

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

字体相关

  • 字体
#define FONT_SIZE(f)            [UIFont systemFontOfSize:(f)]
#define FONT_BOLD_SIZE(f) [UIFont boldSystemFontOfSize:(f)]
#define FONT_ITALIC_SIZE(f) [UIFont italicSystemFontOfSize:(f)]
  • 大小屏字体自动切换
    有的应用希望有一个好的用户体验会在不同的屏幕上适配不同大小字体,这时就可以使用以下的宏定义来实现。但是如果应用中字体大小不能做到全局统一,就不要使用以下的宏定义来实现字体大小适配。这个就看你所开发的具体情况了
#define IS_SmallScreen (IS_iPhone5 || IS_iPhone4)

#define MaxFontSize    (IS_SmallScreen ? 21.f : 25.f )
#define LagerFontSize (IS_SmallScreen ? 17.f : 19.f )
#define BigFontSize (IS_SmallScreen ? 15.f : 17.f )
#define NormalFontSize (IS_SmallScreen ? 13.f : 15.f )
#define SmallFontSize (IS_SmallScreen ? 11.f : 13.f )
#define MinFontSize (IS_SmallScreen ? 9.f : 11.f )

颜色相关

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

字符串相关

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

Image相关

#define IMG_Name(imgName)        [UIImage imageNamed:(imgName)]

#define IMG_ImgWidth(img)        ((img).size.width)
#define IMG_ImgHeight(img) ((img).size.height)

校验相关

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

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

#define NSLOG_Str(mark,str)       NSLog(@"##%@##--str:%@--",(mark),(str))
#define NSLOG_Int(mark,int) NSLog(@"##%@##--int:%ld--",(mark),(int))
#define NSLOG_Float(mark,float) NSLog(@"##%@##--float:%f--",(mark),(float))
#define NSLOG_Bool(mark,bool) NSLog(@"##%@##--bool:%@--",(mark),(bool) ? @"YES" : @"NO")
#define NSLOG_Point(mark,point) NSLog(@"##%@##--x:%f--y:%f--",(mark),(point).x,(point).y)
#define NSLOG_Size(mark,size) NSLog(@"##%@##--width:%f--height:%f--",(mark),(size).width,(size).height)
#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)
#define NSLog(FORMAT, ...) fprintf(stderr,"[%s:%d行] %s\n",[[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String], __LINE__, [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
#else
#define NSLog(FORMAT, ...) nil
#endif


接口相关

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

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. code_analyzer(代码分析助手)

    软件名: code_analyzer 使用c语言 pcre正则库分析源码文件,包括文件中的头文件.宏定义.函数. 用途: 无聊时,可以用来打发下时间. 演示: 对于本源程序的分析结果如下: ##### ...

  2. Xcode7真机调试iOS应用程序

    金田 近日苹果发布的新的Xcode7带来了许多特性,比如:swift语言比以前运行更快.功能更强.代码具有更高的可读性.Xcode的测试功能可以帮助用户记录应用程序的行为等,还有我们今天要讲到的Xco ...

  3. wpa_cli和wpa_supplicant使用,配置无线AP名和密码,静态ip地址

    配置静态ip方法分享:通过串口命令行输入如下命令: 1. 添加无线网络接入点(AP) 及其 密码:# wpa_cli -p /data/misc/wpa_supplicantwpa_cli v0.5. ...

  4. lightoj 1013 dp

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1013 #include <cstdio> #include <cst ...

  5. Apache HBase 2015年发展回顾与未来展望

    编者按:高可用架构推出 2015 年度回顾系列文章,分享在架构领域具有典型意义的年度案例,本文由张虔熙分享.转载请注明来自高可用架构公众号「ArchNotes」.   张虔熙,Hulu 网,专注于分布 ...

  6. 域名解析中A记录、CNAME、MX记录、NS记录的区别和联系

    可以看出加了www.和不加www之后的区别. 可以看出域名解析中有几种记录,A记录,CNAME CNAME记录是域名指向另一个域名A记录是域名指向IP地址 A记录 又称IP指向,用户可以在此设置子域名 ...

  7. IT项目经理应具备的十大软技能

    现在,企业对IT部项目经理的要求越来越多.如果你认为IT项目成员只需要技术性能力,那可就错了. 据IT招聘公司调查发现,几年人们对项目管理软技能的兴趣明显浓厚起来.许多企业尽量避免把IT部门看成只是成 ...

  8. JS中timestamp日期类型的转换

    在JS中获取timestamp:var timestamp=Math.round(new Date().getTime()/1000); 在JS中将timestamp转换为Date: Date.pro ...

  9. 谈NOT IN和Exists

    表1: test001 select * from test001

  10. Oracle MERGE INTO的使用方法

    非常多时候我们会出现例如以下情境,假设一条数据在表中已经存在,对其做update,假设不存在,将新的数据插入.假设不使用Oracle提供的merge语法的话,可能先要上数据库select查询一下看是否 ...