为了方便开发,经常会用到一些宏定义。梳理了之前项目中用到的,又查漏补缺挑选了一些网络上比较不错的,总结了一份分享给大家。
//1,***获取系统对象
#define kApplication [UIApplication sharedApplication]
#define kUserDefaults [NSUserDefaults standardUserDefaults]
#define kNotificationCenter [NSNotificationCenter defaultCenter]
//2,***获取屏幕宽高
#define kScreenWidth ([[UIScreen mainScreen] bounds].size.width)
#define kScreenHeight [[UIScreen mainScreen] bounds].size.height
#define kScreen_Bounds [UIScreen mainScreen].bounds
//3,***根据ip6的屏幕来拉伸
#define kRealWidth(with) ((with)*(KScreenWidth/375.0f))
#define kRealHeight(height) ((height)*(kScreenHeight/667.0f))
//4,***强弱引用(执行完A ## B后变为AB(连接A和B并去掉空格)
#define kWeakSelf(type) __weak typeof(type) weak##type = type;
#define kStrongSelf(type) __strong typeof(type) type = weak##type;
//5,***View圆角和加边框
#define ViewBorderRadius(View, Radius, Width, Color)\
\
[View.layer setCornerRadius:(Radius)];\
[View.layer setMasksToBounds:YES];\
[View.layer setBorderWidth:(Width)];\
[View.layer setBorderColor:[Color CGColor]]
//6,***View圆角
#define ViewRadius(View, Radius)\
\
[View.layer setCornerRadius:(Radius)];\
[View.layer setMasksToBounds:YES]
//7,***颜色#define UIColorWithRGBA(r,g,b,a) [UIColor colorWithRed:(r) / 255.0f green:(g) / 255.0f blue:(b) / 255.0f alpha:(a)]
#define UIColorWithRGBValue(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
//随机色生成
#define kRandomColor UIColorWithRGBA(arc4random_uniform(256),arc4random_uniform(256),arc4random_uniform(256),1)
//8,***字体
#define BoldSystemFont(fontSize) [UIFont boldSystemFontOfSize:fontSize]
#define SystemFont(fontSize) [UIFont systemFontOfSize:fontSize]
#define FONT(fontName, fontSize) [UIFont fontWithName:(fontName) size:(fontSize)]
//9,***当前系统版本(iOS版本)
#define CurrentSystemVersion [[UIDevice currentDevice].systemVersion doubleValue]
//10,***发送通知
#define KPostNotification(name,obj) [[NSNotificationCenter defaultCenter] postNotificationName:name object:obj];
//11,***DEBUG 模式下打印日志,当前行
#ifdef DEBUG
#define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
#define DLog(...)
#endif