有些时候,我们需要将代码简洁化,这样便于读代码。我们可以将一些不变的东东抽取出来,将变化的东西作为参数。定义为宏,这样在写的时候就简单多了。
下面例举了一些常用的宏定义和大家分享:
1. 判断设备的操作系统是不是ios7
1.#define IOS7 ( [[[UIDevice currentDevice].systemVersion doubleValue] >= 7.0] )
2. 判断当前设备是不是iPhone5
1.#define kScreenIphone5 (([[UIScreen mainScreen] bounds].size.height)>=568)
3.获取当前屏幕的高度
1.#define kMainScreenHeight ([UIScreen mainScreen].applicationFrame.size.height)
4.获取当前屏幕的宽度
1.#define kMainScreenWidth ([UIScreen mainScreen].applicationFrame.size.width)
5.获得RGB颜色
1.#define SMSColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
6..自定义Log
#ifdef DEBUG
#define SMSLog(...) NSLog(__VA_ARGS__)
#else
#define SMSLog(...)
#endif
7.单例
01.// @interface
02.#define singleton_interface(className)
03.+ (className *)shared##className;
04.
05.
06.// @implementation
07.#define singleton_implementation(className)
08.static className *_instance;
09.+ (id)allocWithZone:(struct _NSZone *)zone
10.{
11.static dispatch_once_t onceToken;
12.dispatch_once(&onceToken, ^{
13._instance = [super allocWithZone:zone];
14.});
15.return _instance;
16.}
17.+ (className *)shared##className
18.{
19.static dispatch_once_t onceToken;
20.dispatch_once(&onceToken, ^{
21._instance = [[self alloc] init];
22.});
23.return _instance;
24.}
//用于判断是否已经登陆
#define isLongin [kDocumentPath stringByAppendingString:@"/islongIn.plist"]
#define controllerw [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask,YES).firstObject stringByAppendingString:@"/controllerw"]
#define cancelOrderpath [kDocumentPath stringByAppendingString:@"/cancelOrderpath"]
//用于临时保存当前的位置信息
#define localAddressInfo [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask,YES).firstObject stringByAppendingString:@"/localAddressInfo"]
#define userInfoo [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask,YES).firstObject stringByAppendingString:@"/userInfo.pliist"]
//保存历史联系人
#define hisLianxiren [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask,YES).firstObject stringByAppendingString:@"/hisLiawedwednddewdf"]
//保存订单编号
#define saveorderNO [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask,YES).firstObject stringByAppendingString:@"/orderNO"]
//保存订单类型
#define saveorderNOtype [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask,YES).firstObject stringByAppendingString:@"/orderNOtype"]
//保存登陆返回信息的地址
#define klongInInfoPath [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask,YES).firstObject stringByAppendingString:@"/localInfo.plist"]
//保存历史搜索记录到本地
#define saveHistorySearchRecord [kDocumentPath stringByAppendingString:@"/HistorySearchRecord.plist"]
//临时保存选定地点到本地
#define saveTemporarySearchRecord [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask,YES).firstObject stringByAppendingString:@"/TemporarySearchRecordlocalInfo.plist"]
//起点坐标
#define savestarTemporarySearchRecord [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask,YES).firstObject stringByAppendingString:@"/starpoint.plist"]
#define ColorGray [UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1]
#define kNavigationBarText self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:kRGBColor(255, 212, 10),NSFontAttributeName : [UIFont boldSystemFontOfSize:18]};
#define kTintColor [UIColor colorWithRed:253/255.0 green:208/255.0 blue:11/255.0 alpha:1.0]
/** 导航栏题目文字大小 */
#define kNaviTitleFontSize 20.0
/** 导航栏题目文字颜色 */
#define kNaviTitleColor [UIColor colorWithRed:250/255.0 green:190/255.0 blue:1/255.0 alpha:1.0]
#define kViewBackGroundColor [UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1.0]
//通过RGB设置颜色
#define kRGBColor(R,G,B) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:1.0]
#define kWindowH [UIScreen mainScreen].bounds.size.height //应用程序的屏幕高度
#define kWindowW [UIScreen mainScreen].bounds.size.width //应用程序的屏幕宽度
#define kAppDelegate ((AppDelegate*)([UIApplication sharedApplication].delegate))
#define kStoryboard(StoryboardName) [UIStoryboard storyboardWithName:StoryboardName bundle:nil]
//通过Storyboard ID 在对应Storyboard中获取场景对象
#define kVCFromSb(storyboardId, storyboardName) [[UIStoryboard storyboardWithName:storyboardName bundle:nil] \
instantiateViewControllerWithIdentifier:storyboardId]
//移除iOS7之后,cell默认左侧的分割线边距
#define kRemoveCellSeparator \
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{\
cell.separatorInset = UIEdgeInsetsZero;\
cell.layoutMargins = UIEdgeInsetsZero; \
cell.preservesSuperviewLayoutMargins = NO; \
}
//Docment文件夹目录
#define kDocumentPath NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject
#define SCREEN_FRAME ([UIScreen mainScreen].applicationFrame)
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
#define ColorWhite [UIColor colorWithRed:255.0 green:255.0 blue:255.0 alpha:1]
#define ColorGray [UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1]
#define ColorOrange [UIColor colorWithRed:254/255.0 green:216/255.0 blue:0.0 alpha:1]
#define ColorBlue [UIColor colorWithRed:58/255.0 green:225/255.0 blue:1.0 alpha:1]
#define ColorBlack [UIColor colorWithRed:17/255.0 green:17/255.0 blue:7/255.0 alpha:1]
#define ColorLine [UIColor colorWithRed:220/255.0 green:220/255.0 blue:220/255.0 alpha:1]
#define ColorText [UIColor colorWithRed:200/255.0 green:200/255.0 blue:200/255.0 alpha:1]
#define ColorTextSelect [UIColor colorWithRed:248/255.0 green:152/255.0 blue:0/255.0 alpha:1]
#define ColorBigText [UIColor colorWithRed:254/255.0 green:216/255.0 blue:3/255.0 alpha:1]
#define ColorBlackText [UIColor colorWithRed:45/255.0 green:45/255.0 blue:45/255.0 alpha:1]
#define Font(a) [UIFont fontWithName:@"Helvetica" size:a]
#define sysFont(a) [UIFont systemFontOfSize:a]
#define ISLOGIN @"loginStyle"
#define loca [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask,YES).firstObject stringByAppendingString:@"/local.plist"]
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_RETINA ([[UIScreen mainScreen] scale] >= 2.0)
#define SCREEN_WIDTHh ([[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHTt ([[UIScreen mainScreen] bounds].size.height)
#define SCREEN_MAX_LENGTH (MAX(SCREEN_WIDTHh, SCREEN_HEIGHTt))
#define SCREEN_MIN_LENGTH (MIN(SCREEN_WIDTH, SCREEN_HEIGHTt))
#define IS_IPHONE_4_OR_LESS (IS_IPHONE && SCREEN_MAX_LENGTH < 568.0)
#define IS_IPHONE_5 (IS_IPHONE && SCREEN_MAX_LENGTH == 568.0)
#define IS_IPHONE_6 (IS_IPHONE && SCREEN_MAX_LENGTH == 667.0)
#define IS_IPHONE_6P (IS_IPHONE && SCREEN_MAX_LENGTH == 736.0)
#endif