iOS开发 宏定义

2017-03-27  本文已影响17人  LearningCoding

在iOS开发中,合理的使用宏定义可以使我们少些好多代码

// MainScreen
#define Main_Screen      [[UIScreen mainScreen] bounds]
// MainScreen Height&Width
#define Main_Screen_Height     Main_Screen.size.height
#define Main_Screen_Width      Main_Screen.size.width

// 系统控件默认高度
#define StatusBarHeight        (20.f)
#define TopBarHeight           (44.f)
#define BottomBarHeight        (49.f)

// 颜色(RGB)
#define RGBCOLOR(r, g, b)       [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1]
#define RGBACOLOR(r, g, b, a)   [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)]
#define colorWith0xColor(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]

// min到max的随机数
#define Random(min, max)    arc4random() % (max - min + 1) + min

// 在DEBUG模式下打印
#ifdef DEBUG
    #define wlog(...)   NSLog(@"%s 第%d行 \n %@\n\n",__func__,__LINE__,[NSString stringWithFormat:__VA_ARGS__])
#elif RELEACE
    #define wlog(...)   
#else
    #define wlog(...)
#endif

// 预处理判断使用
#define DLEVEL 10
#if DLEVEL == 0
    #define STACK 0
#elif DLEVEL == 1
    #define STACK 1
#else
    #define STACK DLEVEL
#endif

// 预编辑 判断版本号
#ifdef _IPHONE_11_0
#endif

// 弱引用、强引用
#define WeakSelf(type)  __weak typeof(type) weak##type = type;
#define StrongSelf(type)  __strong typeof(type) type = weak##type;

// 给view设置圆角和边框
#define ViewBorderRadius(View, Radius, Width, Color) \
[View.layer setCornerRadius:(Radius)];\
[View.layer setMasksToBounds:YES];\
[View.layer setBorderWidth:(Width)];\
[View.layer setBorderColor:[Color CGColor]];

C预定义宏
DATE //进行预处理的日期(“Mmm dd yyyy”形式的字符串文字)
FILE // 代表当前源代码文件名的字符串文字
LINE // 代表当前源代码中的行号的整数常量
TIME // 源文件编译时间,格式微“hh:mm:ss”
func // 当前所在函数名

上一篇 下一篇

猜你喜欢

热点阅读