首先,我们先来创建一个宏定义,如下
#define LABEL(x,y,w,h) [[UILabel alloc]initWithFrame:CGRectMake(x, y, w, h)];
创建完成后,我们要看两个很重要的位置,LABEL(x,y,w,h)和CGRectMake(x, y, w, h),我们可以看到,括号里的x,y,w,h,为四个变量参数,也就是说,带参数的宏定义,括号的内容必须要一直,参数对应,这样的话:
LABEL(0, 0, 50, 50)
实际执行的错操作:
[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
效果:
UILabel *page_numberL;
page_numberL = LABEL(0, 0, 50, 50);
这样我们就不需要再写好多墨迹的代码,例如:
[UILabel alloc]initWithFrame:CGRectMake
我们直接被一个
LABEL
代替了
推荐几个常用的宏定义
//打印log
#define DLog(fmt, ...) NSLog((@"[FileName:%s] + " "[Method:%s] + " "[Line:%d] >>>>>>>>" fmt), __FILE__, __FUNCTION__, __LINE__, ##__VA_ARGS__);
#define debugLog( s, ... ) printf("class: <%p %s:(%d) > method: %s \n%s\n", self, [[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String], __LINE__, __PRETTY_FUNCTION__, [[NSString stringWithFormat:(s), ##__VA_ARGS__] UTF8String] )
#define debugMethod() NSLog(@" %s ,%d",__FUNCTION__,__LINE__);
#define debugReleaseMark() NSLog(@"Release %s ,%d",__FUNCTION__,__LINE__);
//字符串拼接
#define StringFormat(fmt, ...) [NSString stringWithFormat:fmt, __VA_ARGS__]
//屏幕宽度和高度
#define SCREEN_WIDTH ([[UIScreen mainScreen]bounds].size.width)
#define SCREEN_HEIGHT ([[UIScreen mainScreen]bounds].size.height)
//获取iOS版本
#define IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]