在项目中经常用到的一些宏定义,我基本都是将宏分成几个部分,尺寸宏、系统宏、文字宏、工具类宏、定义沙盒目录文件的宏、 通知Notification相关的宏、服务端API接口的宏,还有一个管理的宏。

1、 定义尺寸类的宏


#ifndef DimensMacros_h
 #define DimensMacros_h
 
//状态栏高度
20
高度
44
//状态栏+导航栏高度
 #define STATUS_AND_NAVIGATION_HEIGHT ((STATUS_BAR_HEIGHT) + (NAVIGATION_BAR_HEIGHT))
 
  
 
 
#endif /* DimensMacros_h *

2、系统宏


#ifndef SystemHeader_h
#define SystemHeader_h

// 手机型号
#define phoneModel [[UIDevice currentDevice] model]

//手机系统版本
#define phoneVersion [[UIDevice currentDevice] systemVersion]

// 当前应用软件版本  比如:1.0.1
#define appCurVersion [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]

// 当前应用版本号码   int类型
#define appCurVersionNum [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]

// 手机唯一标示(需要修改)
#define phoneUUID [[UIDevice currentDevice].identifierForVendor UUIDString]

// 获取当前语言
#define LRCurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0])

//判断是否为iPhone
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_IPHONE ([[[UIDevice currentDevice] model] isEqualToString:@"iPhone"])

//判断是否为iPad
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPAD ([[[UIDevice currentDevice] model] isEqualToString:@"iPad"])

//判断是否为ipod
#define IS_IPOD ([[[UIDevice currentDevice] model] isEqualToString:@"iPod touch"])

/** 设备是否为iPhone 4/4S 分辨率320x480,像素640x960,@2x */
#define iPhone4 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO)
/** 设备是否为iPhone 5C/5/5S 分辨率320x568,像素640x1136,@2x */
#define iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
/** 设备是否为iPhone 6 分辨率375x667,像素750x1334,@2x */
#define iPhone6 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(750, 1334), [[UIScreen mainScreen] currentMode].size) : NO)
/** 设备是否为iPhone 6 Plus 分辨率414x736,像素1242x2208,@3x */
#define iPhone6P ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2208), [[UIScreen mainScreen] currentMode].size) : NO)

 
屏幕
 #define SCREEN_RECT ([UIScreen mainScreen].bounds)
 #define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
 #define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
 #define CONTENT_HEIGHT (SCREEN_HEIGHT - NAVIGATION_BAR_HEIGHT - STATUS_BAR_HEIGHT)
 
//屏幕分辨率
 #define SCREEN_RESOLUTION (SCREEN_WIDTH * SCREEN_HEIGHT * ([UIScreen mainScreen].scale))

//大于等于7.0的ios版本
#define IOS_VERSION_7_OR_LATER    ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
//判断 iOS 8 或更高的系统版本
#define IOS_VERSION_8_OR_LATER (([[[UIDevice currentDevice] systemVersion] floatValue] >=8.0)? (YES):(NO))
//iOS6时,导航VC中view的起始高度
#define YH_HEIGHT (iOS7_OR_LATER ? 64:0)

//获取系统时间戳
#define getCurentTime [NSString stringWithFormat:@"%ld", (long)[[NSDate date] timeIntervalSince1970]]

// 检查系统版本
#define SYSTEM_VERSION_EQUAL_TO(v)                  ([[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v)              ([[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v)                ([[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)    ([[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)


#endif /* SystemHeader_h */





3、工具类的宏 


#ifndef UtilsHeader_h
#define UtilsHeader_h

/*
 * log输出
 * 项目开发中,我们会在许多地方加上Log,但是发布的时候又不想用这些Log,我们也不可能一个一个的删除,所以自定义Log是必然的!
 */
#ifdef DEBUG
#define LOG(...) NSLog(__VA_ARGS__)
#else
#define LOG(...)
#endif

// 判断是不是iOS系统,如果是iOS系统在真机和模拟器输出都是YES
#if TARGET_OS_IPHONE
#endif
#if (TARGET_IPHONE_SIMULATOR)
// 在模拟器的情况下
#else
// 在真机情况下
#endif

 
// 获取RGB颜色
#define RGBA(r,g,b,a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]
#define RGB(r,g,b) RGBA(r,g,b,1.0f)
#define UIColorFromRGB(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]
// 设置随机颜色
#define LRRandomColor [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0]
// clear背景颜色
#define LRClearColor [UIColor clearColor]


// 判断是字符串否为空
 #define kStringIsEmpty(str) ([str isKindOfClass:[NSNull class]] || str == nil || [str length] < 1 ? YES : NO )
// 判断数组是否为空
#define kArrayIsEmpty(array) (array == nil || [array isKindOfClass:[NSNull class]] || array.count == 0)
// 判断字典是否为空
 #define kDictIsEmpty(dic) (dic == nil || [dic isKindOfClass:[NSNull class]] || dic.allKeys == 0)
// 判断是否为空对象
#define kObjectIsEmpty(_object) (_object == nil \
|| [_object isKindOfClass:[NSNull class]] \
|| ([_object respondsToSelector:@selector(length)] && [(NSData *)_object length] == 0) \
|| ([_object respondsToSelector:@selector(count)] && [(NSArray *)_object count] == 0))

// 角度转弧度/弧度转角度
#define M_PI        3.14159265358979323846264338327950288  
#define LRDegreesToRadian(x) (M_PI * (x) / 180.0)
#define LRRadianToDegrees(radian) (radian*180.0)/(M_PI)

// 弱引用/强引用
#define LRWeakSelf(type)  __weak typeof(type) weak##type = type;
#define LRStrongSelf(type)  __strong typeof(type) type = weak##type;


// 读取本地图片
#define LOADIMAGE(file,ext) [UIImage imageWithContentsOfFile:[NSBundle mainBundle]pathForResource:file ofType:ext]
// 定义UIImage对象
#define IMAGE(A) [UIImage imageWithContentsOfFile:[NSBundle mainBundle] pathForResource:A ofType:nil]
// 定义UIImage对象(性能没有上边两种高)
#define ImageNamed(_pointer) [UIImage imageNamed:[UIUtil imageName:_pointer]


// GCD
#define BACK(block) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block)
#define MAIN(block) dispatch_async(dispatch_get_main_queue(),block)


#endif /* UtilsHeader_h */




4、定义沙盒目录文件的宏


#ifndef PathHeader_h
#define PathHeader_h

//文件的目录
// 获取沙盒Document路径
#define kDocumentPath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
// 获取沙盒temp路径
#define kTempPath NSTemporaryDirectory()
// 获取沙盒Cache路径
#define kCachePath [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]
// 获取自定义plist路径
#define kDataPath                 [kPathDocument stringByAppendingPathComponent:@"data.plist"]

#endif /* PathHeader_h */




5、通知Notification相关的宏

#ifndef NotificationHeader_h
#define NotificationHeader_h


// 获取通知中心
#define LRNotificationCenter [NSNotificationCenter defaultCenter]
 

#define TNCancelFavoriteProductNotification     @"TNCancelFavoriteProductNotification"     //取消收藏时
#define TNMarkFavoriteProductNotification       @"TNMarkFavoriteProductNotification"       //标记收藏时

#define kNotficationDownloadProgressChanged     @"kNotficationDownloadProgressChanged"     //下载进度变化
#define kNotificationPauseDownload              @"kNotificationPauseDownload"              //暂停下载
#define kNotificationStartDownload              @"kNotificationStartDownload"              //开始下载

#define kNotificationDownloadSuccess            @"kNotificationDownloadSuccess"            //下载成功
#define kNotificationDownloadFailed             @"kNotificationDownloadFailed"             //下载失败
#define kNotificationDownloadNewMagazine        @"kNotificationDownloadNewMagazine"


#endif /* NotificationHeader_h */





 6、服务端API接口的宏

#ifndef APIStringHeader_h
#define APIStringHeader_h

#ifdef DEBUG
//Debug状态下的测试API
#define API_BASE_URL_STRING     @""

#else
//Release状态下的线上API
#define API_BASE_URL_STRING     @""

#endif

//接口
#define UPLOAD_FILE             @"/upload/file"            //上传图片
#define LOGIN                   @"/login"                  //登录
#define LOGINOUT                @"/logout"                 //登出

#endif /* APIStringHeader_h */



 7、描述文字的宏

#ifndef TextHeader_h
#define TextHeader_h

#define LoadingData @"正在加载中..."
#define LoadinSuccess @"登录成功"
#define LoadoutSuccess @"退出成功"

#endif /* TextHeader_h */




8、创建一个import所有宏相关的文件Macros.h

#ifndef Header_h
#define Header_h
 
#import "UIView+Extension.h"
#import "NSString+Extension.h"
#import "UIImage+ResizeImage.h"

#import "PathHeader.h"
#import "UtilsHeader.h"
#import "DimensMacros.h"
#import "TextHeader.h"
#import "APIStringHeader.h"

#import "SystemHeader.h"

#endif



最后在xcode项目的pch文件中,导入Macros.h文件

#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "SVProHUD.h"
#import "EaseUI.h"
#import <Hyphenate/Hyphenate.h>

#import "Header.h"
#endif


以上是我在项目中用到的宏,如果有我没提到大家还认为不错的宏可以给我留言说下 大家共同学习。