宏定义在C系开发中可以说占有举足轻重的作用。使用宏定义的好处是不言自明的,在节省工作量的同时,代码可读性大大增加。
#pragma mark -尺寸设置
/// 屏幕宽度
#define Screen_width ([UIScreen mainScreen].bounds.size.width)
/// 屏幕高度
#define Screen_height ([UIScreen mainScreen].bounds.size.height)
#define TabbarHeight 49
#define SearchBarHeight 45
#define StatusBarHeight 20
#define NavigationHeight 44
#pragma mark -对象管理
/// 当前app delegate
#define APPDelegate ((AppDelegate *)[UIApplication sharedApplication].delegate)
/// window对象
#define kWindow [UIApplication sharedApplication].keyWindow
/// 内存管理释放对象
#define Safe_release(x) {if (x){[x release];x = nil;}}
/// block self
#define SelfWeak __weak typeof(self) weakSelf = self
#define SelfStrong __strong __typeof__(self) strongSelf = weakSelf
#pragma mark -系统信息
/// 系统语言
#define kCurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0])
/// 当前版本号
#define GetCurrentVersion ([[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"])
/// iOS系统版本
#define iOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]
/// IOS6的系统
#define ISIOS6 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 6.0)
/// IOS7的系统
#define ISIOS7 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
/// IOS8的系统
#define ISIOS8 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
///系统版本号
#define CurrentSystemVersion ([[UIDevice currentDevice] systemVersion])
/// 是否高清屏
#define isRetina ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO)
/// 是否iPhone5
#define iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
/// 是否iPad
#define isPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#pragma mark - 其他
/// 调试模式下打印信息
#ifdef DEBUG
# define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
# define DLog(...)
#endif
#if TARGET_OS_IPHONE
//iPhone Device
#endif
#if TARGET_IPHONE_SIMULATOR
//iPhone Simulator
#endif
/// ARC
#if __has_feature(objc_arc)
//compiling with ARC
#else
// compiling without ARC
#endif
/// G-C-D
#define GCDBackBlock(block) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block)
#define GCDMainBlock(block) dispatch_async(dispatch_get_main_queue(),block)
#pragma mark - degrees/radian functions
/// 角度获取弧度
#define DegreesToRadian(x) (M_PI * (x) / 180.0)
/// 弧度获取角度
#define RadianToDegrees(radian) (radian*180.0)/(M_PI)
#pragma mark - 字体颜色
/// 字体大小
#define kFontSize(size) [UIFont systemFontOfSize:(size)]
/// 粗字体大小
#define kFontBoldSize(size) [UIFont boldSystemFontOfSize:(size)]
/// 设置颜色
#define UIColorRGB(R,G,B) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:1.0]
/// 设置颜色示例:UIColorHex(0x26A7E8)
#define UIColorHex(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]
/// 设置颜色与透明度示例:UIColorHEX_Alpha(0x26A7E8, 0.5)
#define UIColorHex_Alpha(rgbValue, al) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:al]
/// 随机颜色
#define UIColorRandom [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0]
#pragma mark -系统功能
/// 发送邮件
#define MailSend(email) ([[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"mailto://%@",email]])
/// 拨打电话(不会返回应用)
#define PhoneCall(phone) ([[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",phone]])
/// 拨打电话(会返回应用)
#define PhoneCallAuto(phone) ([[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"telprompt://%@",phone]])
/// 发送短信
#define SMSSend(phone) ([[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"sms://%@",phone]])
/// 打开网址
#define SafariOpen(url) ([[UIApplication sharedApplication] openURL:[NSURL URLWithString:url])
#pragma mark - 通知/存取
/// 获取通知中心
#define kNotificationCenter [NSNotificationCenter defaultCenter]
/// 获取存储实例
#define kUserDefaults [NSUserDefaults standardUserDefaults]
#pragma mark - 网络加载提示符
/// 是否显示加载
#define kNetworkActivityIndicator(show) [UIApplication = show
/// 显示加载
#define kNetworkActivityIndicatorShow kNetworkActivityIndicator(YES)
/// 隐藏加载
#define kNetworkActivityIndicatorHide kNetworkActivityIndicator(NO)
#pragma mark - 判断是真机还是模拟器
/// 判断是不是iOS系统,如果是iOS系统在真机和模拟器输出都是YES
#if TARGET_OS_IPHONE
#endif
#if (TARGET_IPHONE_SIMULATOR)
/// 在模拟器的情况下
#else
/// 在真机情况下
#endif
#pragma mark - 沙盒目录文件
/// 获取temp
#define kPathTemp NSTemporaryDirectory()
/// 获取沙盒 Document
#define kPathDocument [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
/// 获取沙盒 Cache
#define kPathCache [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]
#pragma mark - 有效性判断
/// 有效字符
#define isValidString(str) (([str isKindOfClass:[NSNull class]] || str == nil || [str length] < 1) ? NO : YES)
/// 有效数组
#define isValidArray(array) ((array == nil || [array isKindOfClass:[NSNull class]] || array.count == 0) ? NO : YES)
/// 有效字典
#define isValidDictionary(dic) ((dic == nil || [dic isKindOfClass:[NSNull class]] || dic.allKeys == 0) ? NO : YES)
#pragma mark - 本地文件加载
/// 加载图片
#define kLoadImage(file,type) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:file ofType:type]]
/// 加载数组文件信息
#define kLoadArray(file,type) [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:file ofType:type]]
/// 加载字典文件信息
#define kLoadDict(file,type) [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:file ofType:type]]