iOS開發中,直接在pch檔案中導入宏定義。在做項目的時候,直接拿過來使用,可以大幅度提高開發速度。
下面是 個人總結的一些宏定義。如果大家有其他的常用的宏定義,歡迎添加。我會定期更新這個blog…..
話不多說,直接上幹貨
// 在宏的參數前加上一個#,宏的參數會自動轉換成c語言的字元串
#define MRKeyPath(objc,keyPath) @(((void)objc.keyPath, #keyPath))
//** 加載xib ***********************************************************************************
#define LoadNib(x) [[NSBundle mainBundle] loadNibNamed:@(x) owner:nil options:nil][0]
//** 沙盒路徑 ***********************************************************************************
#define PATH_OF_APP_HOME NSHomeDirectory()
#define PATH_OF_TEMP NSTemporaryDirectory()
#define PATH_OF_DOCUMENT [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
//** DEBUG LOG *********************************************************************************
#ifdef DEBUG
#define MRLog( s, ... ) NSLog( @"< %@:(%d) > %@", [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __LINE__, [NSString stringWithFormat:(s), ##__VA_ARGS__] )
#else
#define MRLog( s, ... )
#endif
//** 獲得目前的 年 月 日 時 分 秒 *****************************************************************************
#define CurrentSec [[NSCalendar currentCalendar] component:NSCalendarUnitSecond fromDate:[NSDate date]]
#define CurrentMin [[NSCalendar currentCalendar] component:NSCalendarUnitMinute fromDate:[NSDate date]]
#define CurrentHour [[NSCalendar currentCalendar] component:NSCalendarUnitHour fromDate:[NSDate date]]
#define CurrentDay [[NSCalendar currentCalendar] component:NSCalendarUnitDay fromDate:[NSDate date]]
#define CurrentMonth [[NSCalendar currentCalendar] component:NSCalendarUnitMonth fromDate:[NSDate date]]
#define CurrentYear [[NSCalendar currentCalendar] component:NSCalendarUnitYear fromDate:[NSDate date]]
//** 角度轉換成弧度 ******************************************************************************************
#define ANGEL(x) (x)/180.0 * M_PI
//* Frame (宏 x, y, width, height)**************************************************************************
// App Frame
#define Application_Frame [[UIScreen mainScreen] applicationFrame]
// App Frame Height&Width
#define App_Frame_Height [[UIScreen mainScreen] applicationFrame].size.height
#define App_Frame_Width [[UIScreen mainScreen] applicationFrame].size.width
// MainScreen Height&Width
#define Main_Screen_Height [[UIScreen mainScreen] bounds].size.height
#define Main_Screen_Width [[UIScreen mainScreen] bounds].size.width
// View 坐标(x,y)和寬高(width,height)
#define ViewxPos(v) (v).frame.origin.x
#define ViewyPos(v) (v).frame.origin.y
#define ViewWidth(v) (v).frame.size.width
#define ViewHeight(v) (v).frame.size.height
#define MinFrameX(v) CGRectGetMinX((v).frame)
#define MinFrameY(v) CGRectGetMinY((v).frame)
#define MidFrameX(v) CGRectGetMidX((v).frame)
#define MidFrameY(v) CGRectGetMidY((v).frame)
#define MaxFrameX(v) CGRectGetMaxX((v).frame)
#define MaxFrameY(v) CGRectGetMaxY((v).frame)
// 系統控件預設高度
#define kStatusBarHeight (20.f)
#define kTopBarHeight (44.f)
#define kBottomBarHeight (49.f)
#define kCellDefaultHeight (44.f)
#define kEnglishKeyboardHeight (216.f)
#define kChineseKeyboardHeight (252.f)
/* ****************************************************************************************************************** */
#pragma mark - Funtion Method (宏 方法)
// PNG JPG 圖檔路徑
#define GetImagePathFromBundle(NAME) [[NSBundle mainBundle] pathForResource:[NSString stringWithUTF8String:NAME] ofType:nil]
#define GetExtPathFromBundle(NAME, EXT) [[NSBundle mainBundle] pathForResource:(NAME) ofType:(EXT)]
// 顔色(RGB)
#define RgbColor(r, g, b) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1]
#define RgbColor(r, g, b, a) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)]
#define RandomColor [UIColor colorWithRed:arc4random_uniform(256) / 255.0 green:arc4random_uniform(256) / 255.0 blue:arc4random_uniform(256) / 255.0 alpha:1]
// View 圓角和加邊框
#define ViewBorderRadius(View, Radius, Width, Color)\
[View.layer setCornerRadius:(Radius)];\
[View.layer setMasksToBounds:YES];\
[View.layer setBorderWidth:(Width)];\
[View.layer setBorderColor:[Color CGColor]]
// 目前版本
#define FSystemVersion ([[[UIDevice currentDevice] systemVersion] floatValue])
#define DSystemVersion ([[[UIDevice currentDevice] systemVersion] doubleValue])
#define SSystemVersion ([[UIDevice currentDevice] systemVersion])
// 目前語言
#define CURRENTLANGUAGE ([[NSLocale preferredLanguages] objectAtIndex:0])
// 是否Retina屏
#define isRetina ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? \
CGSizeEqualToSize(CGSizeMake(640, 960), \
[[UIScreen mainScreen] currentMode].size) : \
NO)
// 是否iPhone5
#define isiPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? \
CGSizeEqualToSize(CGSizeMake(640, 1136), \
[[UIScreen mainScreen] currentMode].size) : \
NO)
// 是否iPad
#define isPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
// UIView - viewWithTag
#define VIEWWITHTAG(_OBJECT, _TAG) [_OBJECT viewWithTag : _TAG]
// 本地化字元串
/** NSLocalizedString宏做的其實就是在目前bundle中查找資源檔案名“Localizable.strings”(參數:鍵+注釋) */
#define LocalString(x, ...) NSLocalizedString(x, nil)
/** NSLocalizedStringFromTable宏做的其實就是在目前bundle中查找資源檔案名“xxx.strings”(參數:鍵+檔案名+注釋) */
#define AppLocalString(x, ...) NSLocalizedStringFromTable(x, @"someName", nil)