http://my.oschina.net/leejan97/blog/354904
宏定義在很多方面都會使用,例如定義高度、判斷iOS系統、工具類,還有諸如檔案路徑、服務端api接口文檔。為了對宏能夠快速定位和了解其功能,我們最好在定義的時候将其放入特定的頭檔案中,下面我抛磚引玉,對一些常用的宏進行分類、分檔案定義,希望對大家有所幫助。
定義尺寸類的宏
<a href="http://my.oschina.net/leejan97/blog/354904">?</a>
<code>DimensMacros.h</code>
<code>//狀态欄高度</code>
<code>#define STATUS_BAR_HEIGHT 20</code>
<code>//NavBar高度</code>
<code>#define NAVIGATION_BAR_HEIGHT 44</code>
<code>//狀态欄 + 導航欄 高度</code>
<code>#define STATUS_AND_NAVIGATION_HEIGHT ((STATUS_BAR_HEIGHT) + (NAVIGATION_BAR_HEIGHT))</code>
<code>//螢幕 rect</code>
<code>#define SCREEN_RECT ([UIScreen mainScreen].bounds)</code>
<code>#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)</code>
<code>#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)</code>
<code>#define CONTENT_HEIGHT (SCREEN_HEIGHT - NAVIGATION_BAR_HEIGHT - STATUS_BAR_HEIGHT)</code>
<code>//螢幕分辨率</code>
<code>#define SCREEN_RESOLUTION (SCREEN_WIDTH * SCREEN_HEIGHT * ([UIScreen mainScreen].scale))</code>
<code>//廣告欄高度</code>
<code>#define BANNER_HEIGHT 215</code>
<code>#define STYLEPAGE_HEIGHT 21</code>
<code>#define SMALLTV_HEIGHT 77</code>
<code>#define SMALLTV_WIDTH 110</code>
<code>#define FOLLOW_HEIGHT 220</code>
<code>#define SUBCHANNEL_HEIGHT 62</code>
2.定義沙盒目錄檔案的宏
<code>PathMacros.h</code>
<code>//檔案目錄</code>
<code>#define kPathTemp NSTemporaryDirectory()</code>
<code>#define kPathDocument [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]</code>
<code>#define kPathCache [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0]</code>
<code>#define kPathSearch [kPathDocument stringByAppendingPathComponent:@"Search.plist"]</code>
<code>#define kPathMagazine [kPathDocument stringByAppendingPathComponent:@"Magazine"]</code>
<code>#define kPathDownloadedMgzs [kPathMagazine stringByAppendingPathComponent:@"DownloadedMgz.plist"]</code>
<code>#define kPathDownloadURLs [kPathMagazine stringByAppendingPathComponent:@"DownloadURLs.plist"]</code>
<code>#define kPathOperation [kPathMagazine stringByAppendingPathComponent:@"Operation.plist"]</code>
<code>#define kPathSplashScreen [kPathCache stringByAppendingPathComponent:@"splashScreen"]</code>
<code>#endif</code>
3.工具類的宏
<code>UtilsMacros.h</code>
<code>//Log utils marco</code>
<code>#define ALog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);</code>
<code>#ifdef DEBUG</code>
<code>#define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);</code>
<code>#else</code>
<code>#define DLog(...)</code>
<code>#define ULog(...)</code>
<code>//#define ULog(fmt, ...) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"%s\n [Line %d] ", __PRETTY_FUNCTION__, __LINE__] message:[NSString stringWithFormat:fmt, ##__VA_ARGS__] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alert show]; }</code>
<code>//System version utils</code>
<code>#define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)</code>
<code>#define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)</code>
<code>#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)</code>
<code>#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)</code>
<code>#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)</code>
<code>// 擷取RGB顔色</code>
<code>#define RGBA(r,g,b,a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]</code>
<code>#define RGB(r,g,b) RGBA(r,g,b,1.0f)</code>
<code>#define IsPortrait ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)</code>
<code>#define IsNilOrNull(_ref) (((_ref) == nil) || ([(_ref) isEqual:[NSNull null]]))</code>
<code>//角度轉弧度</code>
<code>#define DEGREES_TO_RADIANS(d) (d * M_PI / 180)</code>
<code>//大于等于7.0的ios版本</code>
<code>#define iOS7_OR_LATER SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")</code>
<code>//大于等于8.0的ios版本</code>
<code>#define iOS8_OR_LATER SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")</code>
<code>//iOS6時,導航VC中view的起始高度</code>
<code>#define YH_HEIGHT (iOS7_OR_LATER ? 64:0)</code>
<code>//擷取系統時間戳</code>
<code>#define getCurentTime [NSString stringWithFormat:@"%ld", (long)[[NSDate date] timeIntervalSince1970]]</code>
4.通知Notification相關的宏
<code>NotificationMacros.h</code>
<code>//系統Notification定義</code>
<code>#define TNCancelFavoriteProductNotification @"TNCancelFavoriteProductNotification" //取消收藏時</code>
<code>#define TNMarkFavoriteProductNotification @"TNMarkFavoriteProductNotification" //标記收藏時</code>
<code>#define kNotficationDownloadProgressChanged @"kNotficationDownloadProgressChanged" //下載下傳進度變化</code>
<code>#define kNotificationPauseDownload @"kNotificationPauseDownload" //暫停下載下傳</code>
<code>#define kNotificationStartDownload @"kNotificationStartDownload" //開始下載下傳</code>
<code>#define kNotificationDownloadSuccess @"kNotificationDownloadSuccess" //下載下傳成功</code>
<code>#define kNotificationDownloadFailed @"kNotificationDownloadFailed" //下載下傳失敗</code>
<code>#define kNotificationDownloadNewMagazine @"kNotificationDownloadNewMagazine"</code>
服務端API接口的宏
<code>APIStringMacros.h</code>
<code>//////////////////////////////////////////////////////////////////////////////////////////////////</code>
<code>//接口名稱相關</code>
<code>//Debug狀态下的測試API</code>
<code>#define API_BASE_URL_STRING @"http://boys.test.companydomain.com/api/"</code>
<code>//Release狀态下的線上API</code>
<code>#define API_BASE_URL_STRING @"http://www.companydomain.com/api/"</code>
<code>//接口</code>
<code>#define GET_CONTENT_DETAIL @"channel/getContentDetail" //擷取内容詳情(含上一個和下一個)</code>
<code>#define GET_COMMENT_LIST @"comment/getCommentList" //擷取評論清單</code>
<code>#define COMMENT_LOGIN @"comment/login" //擷取評論清單</code>
<code>#define COMMENT_PUBLISH @"comment/publish" //釋出評論</code>
<code>#define COMMENT_DELETE @"comment/delComment" //删除評論</code>
<code>#define LOGINOUT @"common/logout" //登出</code>
還有很多其他類型的宏,此處不一一列舉
建立一個import所有宏相關的檔案Macros.h
<code>Macros.h</code>
<code>#import "UtilsMacros.h"</code>
<code>#import "APIStringMacros.h"</code>
<code>#import "DimensMacros.h"</code>
<code>#import "NotificationMacros.h"</code>
<code>#import "SharePlatformMacros.h"</code>
<code>#import "StringMacros.h"</code>
<code>#import "UserBehaviorMacros.h"</code>
<code>#import "PathMacros.h"</code>
在xcode項目的pch檔案中,導入Macros.h檔案
<code>XcodeProjectName-Prefix.pch</code>
<code>#ifdef __OBJC__</code>
<code> </code><code>#import <UIKit/UIKit.h></code>
<code> </code><code>#import <Foundation/Foundation.h></code>
<code> </code><code>#import </code><code>"Macros.h"</code>
如何聯系我:【萬裡虎】www.bravetiger.cn
【QQ】3396726884 (咨詢問題100元起,幫助解決問題500元起)
【部落格】http://www.cnblogs.com/kenshinobiy/